[Windows CE/Mobile] Get Process List C#

Some monthes ago i found article on RSDN that explains how to enumerate process for windows, there was like 5 or 6 methods, one of them says to use ToolHelp api for that, and i asked my self “is it possible to use ToolHelph API on Windows CE, Windows Mobile?”… 10 minutes on MSDN and we got answer, yes we can!
So for that we need 4 API function from toolhelp.dll:

CreateToolhelp32Snapshot  -  This function takes a snapshot of the processes, heaps, modules, and threads used by the processes

CloseToolhelp32Snapshot  -  This function closes a handle to a snapshot.

Process32First  -  This function retrieves information about the first process encountered in a system snapshot.

Process32Next  -  This function retrieves information about the next process recorded in a system snapshot.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ToolHelpApi
{
public class ProcEntry
{
public string ExeName;
public uint ID;
}

public class ProcessEnumerator
{
#region Constants
private const uint TH32CS_SNAPPROCESS = 0x00000002;
private const int MAX_PATH = 260;
#endregion

#region Structs
public struct PROCESSENTRY
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public uint th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string szExeFile;
uint th32MemoryBase;
uint th32AccessKey;
}
#endregion

#region P/Invoke
[DllImport("toolhelp.dll")]
private static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processID);

[DllImport("toolhelp.dll")]
private static extern int CloseToolhelp32Snapshot(IntPtr snapshot);

[DllImport("toolhelp.dll")]
private static extern int Process32First(IntPtr snapshot, ref PROCESSENTRY processEntry);

[DllImport("toolhelp.dll")]
private static extern int Process32Next(IntPtr snapshot, ref PROCESSENTRY processEntry);
#endregion

#region public Methods
public static bool Enumerate(ref List<ProcEntry> list)
{
if (list == null)
{
return false;
}
list.Clear();

IntPtr snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot_ == IntPtr.Zero)
{
return false;
}

PROCESSENTRY entry_ = new PROCESSENTRY();
entry_.dwSize = (uint)Marshal.SizeOf(entry_);
if (Process32First(snapshot_, ref entry_) == 0)
{
CloseToolhelp32Snapshot(snapshot_);
return false;
}

do
{
ProcEntry procEntry = new ProcEntry();
procEntry.ExeName = entry_.szExeFile;
procEntry.ID = entry_.th32ProcessID;
list.Add(procEntry);
entry_.dwSize = (uint)Marshal.SizeOf(entry_);
}
while (Process32Next(snapshot_, ref entry_) != 0);

CloseToolhelp32Snapshot(snapshot_);

return true;
}
public static bool KillProcess(uint procID)
{
try
{
System.Diagnostics.Process proc_ = System.Diagnostics.Process.GetProcessById((int)procID);
proc_.Kill();
proc_.Dispose();
return true;
}
catch (ArgumentException)
{
return false; //process does not exist
}
catch (Exception)
{
return false; //cannot kill process (perhaps its system process)
}
}
#endregion

}
}

You can download sources here

6 Responses to “[Windows CE/Mobile] Get Process List C#”

  1. 1
    Mr.Pohoda Says:

    Hi, I’ve been trying to run your process enumerator. In emulator everything works fine but in ym device the list of processes is always empty. I’ve found out that this condition is always true (line 71, ProcessEnumerator.cs):
    if (Process32First(snapshot_, ref entry_) == 0)
    Would you have any suggestions? toolhelp.dll is in my Windows directory and the system is Windows Mobile 6.5
    Thanks in advance

  2. 2
    Camel Says:

    I have the sameproblem:
    i’m searching the whole internet for this problem and studying the last 12hrs.

    it is working under:
    wm5/wm6/wm6.5 emulator.
    but it doesn’t work on my pda (there is a windows 6.5 installed)

    is it possible to link the toolhelp.dll static inside, that it don’t use the internal toolhelp.dll ?

    because i think that this is the problem of the reason, becaue it is working on all kind of emulators.
    some ideas ?

  3. 3
    Oskar Says:

    For me it is great but I works on 6.1 and lower. Thanks for that, it was wat I looking for.

  4. 4
    martial Says:

    for it to work on my winmobile 6.1 I had to add the constant :
    private const uint TH32CS_SNAPNOHEAPS = 0×40000000;

    and use it in:
    IntPtr snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0);

    see http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesnative/thread/e91d845d-d51e-45ad-8acf-737e832c20d0/

  5. 5
    Michael B Says:

    Thank you for the code, it works perfectly on Windows Mobile 2003 (running on an industrial pocket PC).

  6. 6
    MyungSeok Says:

    Thank you for the code, it works perfectly on Windows CE.
    I Love you!!

Leave a Reply