代码
int GetTargetProcessId()
{
PROCESSENTRY32 pe32;
pe32.dwSize=sizeof(pe32);
DWORD ProcessId;
HANDLE hHandle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hHandle==INVALID_HANDLE_VALUE)
{
printf("fail to call\n");
exit(-1);
}
int TargetProcessId;
const wchar_t *target=L"notepad.exe";
BOOL bMore=::Process32First(hHandle,&pe32);
while(bMore)
{
printf("the name is %ws ,the id is %d\n",pe32.szExeFile,pe32.th32ProcessID);
if((wcscmp(pe32.szExeFile,target))==0)
{
TargetProcessId=pe32.th32ProcessID;
return TargetProcessId;
}
bMore=::Process32Next(hHandle,&pe32);
}
return -1;
}
int main()
{
HANDLE hTargetHandle;
int TargetProcessId=GetTargetProcessId();
//DWORD TargetProcessId=3304;
hTargetHandle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,TargetProcessId);
if(hTargetHandle==INVALID_HANDLE_VALUE);
{
printf("openprocess fail\n");
printf("the TargetProcessId is: %d\n",TargetProcessId);
DWORD d=GetLastError();
printf("the result of getlast is: %d\n",d);
system("PAUSE");
exit(-1);
}
我运行记事本后再运行这个程序,找到了记事本的进程的id,用openprocess,却失败了,getlasterror的结果是127,找不到制定的程序
希望大神指点