嘿,即时通讯使用Windows Mobile 6为我的智能手机做一个小应用程序。我尝试获取所有当前正在运行的processec,但方法CreateToolhelp32Snapshot总是返回-1。所以现在我卡住了。我试图通过调用GetLastError()方法来获取错误,但该方法返回0值。 这是我的代码片段。P /在Compact Framework中调用CreateToolhelp32Snapshot失败
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)”不正确;你应该使用“if(handle!= -1)”。您的声明是否CreateToolhelp32Snapshot始终返回-1字面为true,或者您是否根据您的代码声明它为已发布?你可能会得到一个不合格的句柄,而这个句柄不合格。 – atzz 2008-12-26 16:12:06