Saturday, January 31, 2009

Process.GetProcesses() on the Compact Framework

Process.GetProcesses() is not supported on the Compact Framework 3.5 and often this is a desirable thing to do. But luckily there are workarounds.

You can P/Invoke the required toolhelp.lib function Process32Next passing in a PROCESSENTRY32 struct and a Toolhelp32Snapshop.

The signature looks like this:
BOOL WINAPI Process32Next(
HANDLE hSnapshot,
LPPROCESSENTRY32 lppe
);
There is a fair bit of code required to get this working. You could implement this yourself or use an implementation from the Software Development Framework from OpenNETCF to save you a few hours of pain.

Coding this API looks like so:
var processes = OpenNETCF.ToolHelp.ProcessEntry.GetProcesses();

foreach (OpenNETCF.ToolHelp.ProcessEntry process in processes)
{
Console.WriteLine(process.ExeFile);
}
The call to Toolhelp.ProcessEntry.GetProcesses() returns an array of ProcessEntry objects. You can then kill the process, get the process id, get the associated modules, get the base address, thread count etc..

2 comments:

Anonymous said...

I am having problems finding OpenNETCF.toolhelp.dll

Anonymous said...

Gracias por la ayuda.