Testing WMI from the command line

wmic is a command line utility that can be used to test WMI queries. Unfortunately it uses a syntax that's slightly different from WQL.

So instead of

select * from win32_share

to get a list of shares, in wmic this would be:




for a more concise listing. SHARE is an alias for win32_share.

To see a list of all available alias, run wmic /?

To get a list of shares available on a remote computer (e.g., remotecomputer1):

wmic /node:remotecomputer1 SHARE LIST BRIEF

The /node switch specifies the computer against which the query will be run. There's a /user switch and a /password switch in case the user needs to logon.

Adding a TrackBar to a Windows Forms StatusStrip

StatusStrip is a ToolStrip, one of the oh-so-versatile WinForms 2.0 innovations introduced with the .Net 2.0 Framework. I wanted to add a TrackBar to the StatusStrip which isn't supported out of the box but, fortunately, WinForms2.0 ships with a ToolStripControlHost that can be used to wrap any Windows Forms control for embedding within a ToolStrip.

The basic idea is to derive from ToolStripControlHost (e.g., ToolStripTrackBar : ToolStripControlHost), expose the properties and values of the underlying control, pass through events via OnSubscribeControlEvents and OnUnsubscribeControlEvents.

One stumbling block was designer support; since I was setting up handling of the scroll event in the control's Load event, the WinForms designer would crash while trying to execute it when scrolled in design mode. A way around this was to detect when the ToolStripTrackBar is running in design mode (via Control.Site != null !Control.Site.DesignMode)

A Thread-safe C# Event Invocation Pattern

It has been pointed out by several expert developers that the recommended event invocation pattern suffers from a race condition on the event's invocation list.

The workaround in the link above does, in the strictest sense, address this issue. Invocation lists are immutable. If a separate thread were to remove a handler from the original invocation list while the publisher (OnEvent...) was executing this would create a new Invocation list (one without the removed handler) while the publisher would continue using it's copied reference to the original.

Unfortunately this may not address the underlying issue: while an event is being published its list of handlers should not be modified. C# provides a way to deal with this via event accessors. By combining event accessors and basic synchronization (via lock), it's possible to make a truly thread-safe event invocation pattern:

public class EventSource
private EventHandler testEventHandlers;

public event EventHandler TestEvent
lock (testEventHandlers) { testEventHandlers += value; }
lock (testEventHandlers) { testEventHandlers -= value; }

public void OnTestEvent()
lock (testEventHandlers)
if (testEventHandlers != null)
testEventHandlers(this, new EventArgs());

Making Dynamic Help Faster

Dynamic Help is a panel in Visual Studio that displays links to help relevant to whatever action you've recently taken. So if you click on the word Form in the derivation statement (so that it refers to the System.Windows.Forms.Form class) then several links to documentation about the Form class will show up.

It's a great way to learn about what's available while you're working; a sort of "learning in the background". Unfortunately the out of the box configuration can be a little slow. To speed things up:

  1. Reduce the number of links displayed. Go to Tools -> Options -> Environment -> Help -> Dynamic Help then check "Limit number of links per category" and enter a number smaller than 10 in the box on the right. I use 3.
  2. Reduce the number of categories.
  3. Reduce the number of Topic types.
  4. Try some of the other options.

For step 1, 3 has turned out to be the magic number for me. So I leave everything else turned on.

Windows Desktop Search Tips

I absolutely LOVE Windows Desktop Search (integrated into Vista, add-on for XP).

Some of my favorite queries are:

kind:email from:"firstname lastname" - search for all emails from a certain person
kind:email sent:yesterday keyword1 keyword2... - search for an email sent yesterday about keyword1, keyword2, ....

and a recent favorite:

kind:email has:attachment

All of these properties along with the search syntax are included in the "Rules for advanced searches" page of Windows Desktop Search's help.

Edit-and-Continue for C#

One of the many wonderful innovations in C# 2.0 was the introduction of support for Edit-and-Continue. I'm finding it incredibly useful when refactoring legacy code particularly for an operation made up of many sub-steps.

While testing the refactored version of the operation by stepping through it in the debugger I'll encounter a section of code that's missing some logic or is putting data in the wrong place.

Before C# 2.0 this situation meant Detach, Terminate, re-compile, restart, re-attach (for Windows Services at least), then resume.

With C# 2.0 and beyond you can edit the code while stepping through it in the debugger. The IL is dynamically updated (or if it has already been JITed, native instructions are updated).

This is a huge time savings. Given a sufficiently convoluted execution environment the Detach, Terminate, Recompile, Restart, Re-attach and Resume cycle can take several minutes. With Edit-and-Continue I get to skip it entirely until encountering a need to perform an operation that can't be done during Edit-and-Continue (e.g., adding a method).

Review of the Tritton AX PC Pro

I was recently in the market for Surround Sound headphones.  I've wanted to get back into PC gaming, after a multi-year X-Box 360 only hiatus, but wasn't willing to give up positional audio.  Serious fans of shooters know that positional audio gives you an edge, especially in multiplayer scenarios where framerates may drop but the crackle of gunfire can still reveal the position of an attacker.

The first thing that I appreciate about the AX PC Pro is the packaging!  Finally, a product that takes LESS time to open than it does to install!

The top of the box is held in place by a small insert (on the right, above).  Once retracted the top unfolds upwards and to the left revealing a form fitting (but not sealed) plastic package (below).

The left and right sides of the packaging separate easily.

The headset itself (below) is incredibly comfortable.  The ear pads are designed to cover the entire ear.  For me it took a little positioning to find the "sweet spot".  My jaw tends to bump up against the bottom of the ear pad.  Because of this I found it most comfortable to wear the pads higher up on my head.

A detachable microphone and an extra set of ear pads (on the left, above) are included.  The headset must be connected to the computer via USB.  I was initially leery of USB since 5.1 surround sound is usually transmitted via coax or optical cables.  It turns out that USB makes the whole process a lot simpler.  The mini plug line out on my laptop is on the right side making it somewhat less than ideal since I'm right handed and like to keep the mouse close to the keyboard.  There are USB plugs on both sides of the laptop, so I the USB approach keeps the connections out of the way of the mouse.

If you're in the market for surround sound headphones be sure to get a set that supports 5.1 (or 7.1) audio.  There will be multiple speaker drivers in each cup; at least 1 for each audio channel (Front Left, Front Right, Rear Left, Rear Right, Center and SubWoofer).  There are a LOT of products out there that claim to reproduce surround sound with 2 speakers and signal processing.  I have yet to see such a product reproduce positional accuracy, which is encoded into the 5.1 stream,  sufficiently precise for gaming using only 2 speakers.

What about the performance impact of using USB?  In my admittedly informal tests (F.E.A.R 2 Project Origin) I noticed no performance impact whatsoever.  I got the same frame rate on the built in F.E.A.R 2 diagnostic before and after installing the USB drivers.

The positional accuracy of this headset is amazing.  F.E.A.R 2 is filled with segments where mobs attack from all angles; above you, below you, behind you.  Apparitions appear out of nowhere.  With the AX PC Pro the crackle of gunfire or the wail of a ghost sounds like it came from "out there".  I find myself suppressing the urge to turn around or duck because the positional audio is just that good.

One final note about USB.  It's essentially a separate sound card.  So you'll have to redirect audio (via the sound control panel) to the USB sound card to hear audio through the headset.

Virtual Memory basics on Windows

Each process in Windows gets a virtual address space.

Process -> Virtual Page -> Physical Page (via a page map).

This virtual address space is backed by the combination of physical memory and page file(s). So the virtual address space can be larger than the total amount of physical memory in the system.

The portion of a process's virtual memory that resides in physical memory is the processes working set.

When a thread attempts to use (not just requests) more physical memory than the system has available then Windows frees up memory by moving some pages out of physical memory (into the page file).

Utilities Roundup - Windows Support Tools

While looking through the list of commands included in Windows Support Tools I've come across some great utilities. They've also added some much needed documentation. I especially like the Overview. Two subsections on the Overview page really stand out: "Corresponding UI" and "Concepts"!

diruse.exe - Similar to du on unix/linux, a command line utility for discovering how much disk space is used by directories. More flexibile than explorer. It's maximum-size option is a treat way to find directories taking up a lot of space without having to manually traverse the directory tree.

pageheap.exe - This is also in the Windows SDK/platform SDK. It's used to track down heap corruption and/or memory leaks. Pageheap flags an application. Once flagged, it's heap allocations and deallocations go through an extra software layer to help track down heap corruption.

Heap corruption occurs when you write pass an allocation (e.g., you malloc or new 50 bytes then write more than 50 bytes, possibly corrupting memory in use by some other allocation).

It's got 2 modes; the slower but more thorough mode actually places a non-accessible block of memory after every allocation. So if you try to write past the 50 bytes you requested the application will immediately fault with an access violation.

The faster but less thorough mode surrounds your allocations with a certain pattern then checks that pattern when the application frees the allocation. If anything has changed, you know you've got heap corruption somewhere.

msizap.exe - This can be used to delete any Windows Installer information about a failed install. If an installation fails during a critical point it can be very difficult to clean up because Windows Installer won't uninstall a program it thinks is currently being uninstalled. I'd use this only if the Windows Installer Cleanup utility failed to clean up/remove an application from the Windows Installer database.

Since this app can potentially blow away the entire Windows Installer database, which might make a *lot* of programs unusable, I'd be careful with it.

setx.exe - Ever wanted a way to read the value of a registry key in a batch file without having to write a program? Setx can be used to do that. Ostensibly its purpose is to set user or system environment variables but a side effect of its ability to get values from the registry makes it useful for reading registry values in a .bat file! For example:

setx tzone -k
> t.out

echoes "Extracted value is: Eastern Standard Time" to t.out. This could easily be piped to find or findstr (instead of being redirected to t.out) for use in subsequent .bat commands.