嘿,即時通訊使用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