在.NET Compact Framework 的进程管理需要调用win32的API,也就是P/Invoke,在msnd上提供了这一P/Invoke的源代码, Creating a Microsoft .NET Compact Framework-based Process Manager Application,由于在技术交流群里有不同的人问同样的问题,因此打算写一下。
Win32 API
关键的API 如下:
private const int TH32CS_SNAPPROCESS = 0x00000002;
[DllImport("toolhelp.dll")]
public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
[DllImport("toolhelp.dll")]
public static extern int CloseToolhelp32Snapshot(IntPtr handle);
[DllImport("toolhelp.dll")]
public static extern int Process32First(IntPtr handle, byte[] pe);
[DllImport("toolhelp.dll")]
public static extern int Process32Next(IntPtr handle, byte[] pe);
[DllImport("coredll.dll")]
private static extern IntPtr OpenProcess(int flags, bool fInherit, int PID);
private const int PROCESS_TERMINATE = 1;
[DllImport("coredll.dll")]
private static extern bool TerminateProcess(IntPtr hProcess, uint ExitCode);
[DllImport("coredll.dll")]
private static extern bool CloseHandle(IntPtr handle);
private const int INVALID_HANDLE_VALUE = -1;
[DllImport("toolhelp.dll")]
public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
[DllImport("toolhelp.dll")]
public static extern int CloseToolhelp32Snapshot(IntPtr handle);
[DllImport("toolhelp.dll")]
public static extern int Process32First(IntPtr handle, byte[] pe);
[DllImport("toolhelp.dll")]
public static extern int Process32Next(IntPtr handle, byte[] pe);
[DllImport("coredll.dll")]
private static extern IntPtr OpenProcess(int flags, bool fInherit, int PID);
private const int PROCESS_TERMINATE = 1;
[DllImport("coredll.dll")]
private static extern bool TerminateProcess(IntPtr hProcess, uint ExitCode);
[DllImport("coredll.dll")]
private static extern bool CloseHandle(IntPtr handle);
private const int INVALID_HANDLE_VALUE = -1;
调用上面的API就能就能轮询出所有的进程,可以杀死指定进程了。
Structures
调用这些API需要下面的结构体 PROCESSENTRY32,结构体的转换是P/Invoke里面最难的部分,我曾经写过一篇这样的文章(.NET Compact Framework 下Win32 API P/Invoke 的使用),还是有很多需要学习和改进的地方,欢迎指教。
PROCESSENTRY32 implementation
Wrapped Class
Process class
Client
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Please enter the process name.");
return;
}
string processName = args[0].ToUpper() + ".EXE";
Process[] processes = Process.GetProcesses();
foreach (Process proc in processes)
{
if (proc.ProcessName.ToUpper() == processName)
{
proc.Kill();
Console.WriteLine("{0} was killed.", processName);
break;
}
}
}
{
if (args.Length == 0)
{
Console.WriteLine("Please enter the process name.");
return;
}
string processName = args[0].ToUpper() + ".EXE";
Process[] processes = Process.GetProcesses();
foreach (Process proc in processes)
{
if (proc.ProcessName.ToUpper() == processName)
{
proc.Kill();
Console.WriteLine("{0} was killed.", processName);
break;
}
}
}
这是一个简单的杀进程例子,要取出进程信息,读Process 的attribute就可以了,十分简单。
参考文献
Creating a Microsoft .NET Compact Framework-based Process Manager Application
上述代码来源于msnd,msnd的源代码
本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2009/01/13/Windows-Mobile-Compact-Framework-Process.html,如需转载请自行联系原作者