0
点赞
收藏
分享

微信扫一扫

WINCE 获取正在运行的程序的进程列表名


公司的一个项目开发WINCE5.0设备,我需要获取当前一个程序是否正在运行,然而C#的.Net能使用的进程列表获取方式在WINCE下并不支持,找了各种方式尝试,终于解决,现分享经验如下:

参考网址:​​https://www.codeproject.com/Articles/36841/Compact-Framework-Process-class-that-supports-full​​

将下载好的ProcessCE.cs类放置在WINCE的项目中,然后在你需要的代码地方使用该类的方法函数

使用教程:

 

Using the code

The ​​ProcessCE​​​ class is straightforward and easy to use. Download ProcessCE.zip and add the containing ProcessCE.cs file to your project. Then, add the following ​​using​​ statement to the source file where you want to use the class:

Hide   Copy Code

using

To enumerate running processes:

Hide   Copy Code

ProcessInfo[] list = ProcessCE.GetProcesses();

foreach (ProcessInfo item in list)
{
Debug.WriteLine("Process item: " + item.FullPath);
if (item.FullPath == @"\Windows\iexplore.exe")
item.Kill();
}

// Sample output:
// Process item: \Windows\nk.exe
// Process item: \Windows\filesys.exe
// Process item: \Windows\device.exe
// Process item: \Windows\services.exe
// Process item: \Windows\gwes.exe
// Process item: \Windows\shell32.exe
// Process item: \Windows\poutlook.exe
// Process item: \Program Files\Geso\Discovery\TerranovaWakeUpWCE.exeforeach (ProcessInfo item in list)
{
Debug.WriteLine("Process item: " + item.FullPath);
if (item.FullPath == @"\Windows\iexplore.exe")
item.Kill();
}

// Sample output:
// Process item: \Windows\nk.exe
// Process item: \Windows\filesys.exe
// Process item: \Windows\device.exe
// Process item: \Windows\services.exe
// Process item: \Windows\gwes.exe
// Process item: \Windows\shell32.exe
// Process item: \Windows\poutlook.exe
// Process item: \Program Files\Geso\Discovery\TerranovaWakeUpWCE.exe

To check if a process is running:

Hide   Copy Code

bool result = ProcessCE.IsRunning(@"\Windows\iexplore.exe"); result = ProcessCE.IsRunning(@"\Windows\iexplore.exe");

To find a Process ID (PID):

Hide   Copy Code

IntPtr pid = ProcessCE.FindProcessPID(@"\Windows\iexplore.exe");

if (pid == IntPtr.Zero)
throw new Exception("Process not found."); pid = ProcessCE.FindProcessPID(@"\Windows\iexplore.exe");

if (pid == IntPtr.Zero)
throw new Exception("Process not found.");

To terminate a process:

Hide   Copy Code

bool result = ProcessCE.FindAndKill(@"\Windows\iexplore.exe");  result = ProcessCE.FindAndKill(@"\Windows\iexplore.exe");

​ProcessCE.FindAndKill()​​​ will return ​​false​​​ if the EXE file was not found. If terminating the process fails, it will throw a ​​Win32Exception​​.

Points of interest

This code is for use with Windows CE and Windows Mobile only. The code was tested on Windows Mobile 6.1 (Windows CE 5.2). Windows CE 4 should be no problem.

Tip: A code example to enumerate processes that can be found on several websites was crashing randomly with Win32 error 8 on several devices. This is an out of memory message. The solution is to add the ​​TH32CS_SNAPNOHEAPS​​​ flag to the ​​CreateToolhelp32Snapshot()​​ call:

Hide   Copy Code

CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0)

 | TH32CS_SNAPNOHEAPS, 0)

 

举报

相关推荐

0 条评论