const char DESTPROC[19] = "cmd.exe";
DWORD pid = ProcessToPID(DESTPROC);
返回为0就是没有
- DWORD ProcessToPID(const char *InputProcessName)
-
{
-
DWORD aProcesses[1024], cbNeeded, cProcesses;
-
unsigned int i;
-
HANDLE hProcess = NULL;
-
HMODULE hMod = NULL;
-
char szProcessName[MAX_PATH] = "UnknownProcess";
-
-
AddPrivilege(SE_DEBUG_NAME);
-
-
// 计算目前有多少进程, aProcesses[]用来存放有效的进程PIDs
-
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
-
{
-
return 0;
-
}
-
-
cProcesses = cbNeeded / sizeof(DWORD);
-
// 按有效的PID遍历所有的进程
-
for ( i = 0; i < cProcesses; i++ )
-
{
-
// 打开特定PID的进程
-
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
-
PROCESS_VM_READ,
-
FALSE, aProcesses[i]);
-
// 取得特定PID的进程名
-
if ( hProcess )
-
{
-
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
-
{
-
GetModuleBaseName( hProcess, hMod,
-
szProcessName, sizeof(szProcessName) );
-
//将取得的进程名与输入的进程名比较,如相同则返回进程PID
-
if(!stricmp(szProcessName, InputProcessName))
-
{
-
CloseHandle( hProcess );
-
return aProcesses[i];
-
}
-
}
-
}//end of if ( hProcess )
-
}//end of for
-
//没有找到相应的进程名,返回0
-
CloseHandle( hProcess );
-
return 0;
-
}
也可以枚举得到所有进程的应用程序名,然后和知道应用程序名比较判断。
- DWORD GetProcessidFromName(LPCTSTR name)
-
{
-
PROCESSENTRY32 pe;
-
DWORD id=0;
-
HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
-
pe.dwSize=sizeof(PROCESSENTRY32);
-
if(!Process32First(hSnapshot,&pe))
-
return 0;
-
while(1)
-
{
-
pe.dwSize=sizeof(PROCESSENTRY32);
-
if(Process32Next(hSnapshot,&pe)==FALSE)
-
break;
-
if(strcmp(pe.szExeFile,name)==0)
-
{
-
id=pe.th32ProcessID;
-
-
break;
-
}
-
-
-
}
-
CloseHandle(hSnapshot);
-
return id;
-
}
阅读(9964) | 评论(0) | 转发(1) |