I made a code to get the processhandle of an application, but it won't work. It always outputs 0x30, no matter what application I'm checking the handle for.
I'll paste the code below:
This code will put the handle of a process in hProcess (I declared hProcess as HANDLE at the beginning of the program)Code:PROCESSENTRY32 ProcessStruct;
ProcessStruct.dwSize = sizeof(PROCESSENTRY32);
//Making a snapshot and checking for error
HANDLE hProcessSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0);
if(hProcessSnapshot == INVALID_HANDLE_VALUE)
{
cout << "error1 at making snapshot";
// Something went wrong at making a snapshot
// todo: Making some code if error
return 0;
}
Process32First(hProcessSnapshot, &ProcessStruct);
if(Process32First(hProcessSnapshot, &ProcessStruct) == false)
{
cout << "error2 at making snapshot";
// Error,
// Todo: error handling
}
if(strcmp(ProcessStruct.szExeFile,"filename.exe") != 0)
{
while(true)
{
if(Process32Next(hProcessSnapshot, &ProcessStruct) == false)
{
cout << "error3 while looking for handle";
// Error handling
// todo!
return 0;
}
if(strcmp(ProcessStruct.szExeFile, "filename.exe") == 0)
{
hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessStruct.th32ProcessID);
break;
return 0;
}
}
}
Of course you have to change filename.exe to the app you want the handle off.
That app has to be running, or it will give error3 while looking for handle.
I hope anyone can find my mistake, becouse I've been staring at this code for several days now without finding the problem
Thanks in advance
Eddie