I am writing an application for Windows Mobile 5/6 that needs to enumerate all running processes on the system (in order to find the main window handle of another program). I found example code on MSDN here: http://msdn2.microsoft.com/en-us/library/aa446560.aspx. Half the time it works and half the time it gives me the "Unable to create snapshot" exception.

Well, it turns out the problem has to do with flags being sent to the CreateToolhelp32Snapshot function in toolhelp.dll. I found a great blog post on how to fix it here: http://nativemobile.blogspot.com/2006/01/k-jam-and-toolhelp.html. The author did this to fix a bug for K-Jam (HTC Wizard) phones, but the problem happens at least also on my Sprint Mogul (HTC Mogul/Titan) phone and I suspect other WM5 or WM6 devices.

As a side note, for some reason the developer of the MSDN sample code stores the PID of the process in a property he calls "handle." Do not be confused as I was at first. This is NOT the window handle (hWnd). It is actually the PID of the process! And he stores it in an IntPtr to make it even more confusing (even though he always uses it as the casted "int" value). To get the window handle (if you need it), you have to use the managed .Net "Process" class like this:

Dim p As ToolHelp.Process = ToolHelp.Process.GetProcesses(0)
Dim hWnd as IntPtr = Process.GetProcessById(p.handle.toInt32).MainWindowHandle