C语言中如何获得当前进程句柄,谢谢!
1个回答
展开全部
DWORD PID;
PID = GetProcessPID(L"test.exe");
//获取进程pid函数
DWORD GetProcessPID(LPCTSTR pName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe)){
if (!_tcsicmp(pe.szExeFile, pName)) {
CloseHandle(hSnapshot);
return pe.th32ProcessID;
}
}
return NULL;
}
//由进程pid获取进程句柄,置顶
HWND hh = GetProcessTopWindows(PID);
HWND GetProcessTopWindows(DWORD dwPID){
count = 0;
EnumWindows((WNDENUMPROC)EnumWndProc, (LPARAM)dwPID);
if(count > 0) {
//cout << "窗口句柄是:" << nhWnd[count -1] << endl;//输出句柄,可用spy++工具核对
return (nhWnd[count -1]);
}
return NULL;
}
别忘了添加对应头文件。
PID = GetProcessPID(L"test.exe");
//获取进程pid函数
DWORD GetProcessPID(LPCTSTR pName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe)){
if (!_tcsicmp(pe.szExeFile, pName)) {
CloseHandle(hSnapshot);
return pe.th32ProcessID;
}
}
return NULL;
}
//由进程pid获取进程句柄,置顶
HWND hh = GetProcessTopWindows(PID);
HWND GetProcessTopWindows(DWORD dwPID){
count = 0;
EnumWindows((WNDENUMPROC)EnumWndProc, (LPARAM)dwPID);
if(count > 0) {
//cout << "窗口句柄是:" << nhWnd[count -1] << endl;//输出句柄,可用spy++工具核对
return (nhWnd[count -1]);
}
return NULL;
}
别忘了添加对应头文件。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询