當我運行我的程序時發生了一些奇怪的事情。當我使用VS 2010中的「Start Without Debugging」選項運行它時,OpenProcess像往常一樣返回進程句柄,但是當我在Windows資源管理器中運行我的程序時,OpenProcess總是返回0?我調用了GetLastError,並在兩種情況下返回6(INVALID_HANDLE_VALUE)。我正在使用Windows XP SP3 有人可以幫我嗎?這是我寫的代碼:與OpenProcess卡住,總是返回0
HANDLE GetProcessHandle(TCHAR* szProcessName)
{
//Get the snapshot of all processes in the system
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
if (hSnap == INVALID_HANDLE_VALUE)
{
return INVALID_HANDLE_VALUE;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
//Get the information of the first process
if (!Process32First(hSnap, &pe32))
{
CloseHandle(hSnap);
return INVALID_HANDLE_VALUE;
}
//Loop through all processes
do
{
if (_tcscmp(szProcessName, pe32.szExeFile) == 0)
{
//Got the process ID
CloseHandle(hSnap);
printf("sz = %s; exe = %s; pid = %d\n", szProcessName, pe32.szExeFile, pe32.th32ProcessID);
//Error here, correct PID was found in both case
return OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
}
}
while (Process32Next(hSnap, &pe32));
CloseHandle(hSnap);
return INVALID_HANDLE_VALUE;
}
你確定*你去OpenProcess嗎?您的代碼將錯誤代碼作爲HANDLE值返回,並返回INVALID_HANDLE_VALUE很多。如果您將該函數作爲句柄傳遞給函數... –
INVALID_HANDLE_VALUE == -1,而不是0.我確定我調用了正確的函數! – Name