【发布时间】:2008-12-26 15:23:06
【问题描述】:
嘿,我正在为我的智能手机做一个小应用程序,使用 Windows Mobile 6。我正在尝试获取所有当前正在运行的 processec,但方法 CreateToolhelp32Snapshot 总是返回 -1。所以现在我卡住了。我尝试调用 GetLastError() 方法时出错,但该方法返回 0 值。 这是我的代码的 sn-p。
private const int TH32CS_SNAPPROCESS = 0x00000002;
[DllImport("toolhelp.dll")]
public static extern IntPtr CreateToolhelp32Snapshot(uint flags,
uint processid);
public static Process[] GetProcesses()
{
ArrayList procList = new ArrayList();
IntPtr handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if ((int)handle > 0)
{
try
{
PROCESSENTRY32 peCurr;
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
// get byte array to pass to API call
byte[] peBytes = pe32.ToByteArray();
// get the first process
int retval = Process32First(handle, peBytes);
【问题讨论】:
-
"if ((int)handle > 0)" 不正确;你应该使用“如果(句柄!= -1)”。您是否说 CreateToolhelp32Snapshot 总是返回 -1 字面意思是真的,或者您是根据您发布的代码说的吗?您可能会得到一个未通过测试的有效句柄。
标签: c# .net compact-framework windows-ce