VC 通过进程ID获得主窗口句柄 10
对话框程序有一个按钮点击按钮时关闭QQ.exe我不知道哪里有问请帮我改改并说下为什么谢谢了!typedefstructtagWNDINFO{DWORDdwProcessI...
对话框程序有一个按钮 点击按钮时关闭QQ.exe 我不知道哪里有问请帮我改改 并说下为什么 谢谢了!
typedef struct tagWNDINFO
{
DWORD dwProcessId;
HWND hWnd;
HWND Dll_hwnd;
} WNDINFO, *LPWNDINFO;
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
DWORD dwProcessId;
GetWindowThreadProcessId(hWnd, &dwProcessId);
LPWNDINFO pInfo = (LPWNDINFO)lParam;
if( (dwProcessId == pInfo->dwProcessId) && (hWnd != pInfo->Dll_hwnd) )
//确认找到的句柄不是DLL窗口的句柄
//如果找到的是DLL窗口的句柄则返回FALSE以便继续查找宿主的窗口句柄
{
pInfo->hWnd = hWnd;
TerminateProcess(hWnd,0);
return FALSE;
}
return TRUE;
}
void CMy123Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
PROCESSENTRY32* info=new PROCESSENTRY32;
// HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
info->dwSize = sizeof(PROCESSENTRY32);
char proName[] = {"QQ.exe"};
HANDLE h = NULL;
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
MessageBox("调用失败");
}
BOOL bMore = ::Process32First(hProcessSnap, info);
BOOL terminate = FALSE;
while( bMore != FALSE)
{
if(strcmp(proName,info->szExeFile) == 0)
{
h=OpenProcess(PROCESS_TERMINATE, FALSE, info->th32ProcessID);
EnumWindows(EnumWindowsProc,NULL);
/* if(h==NULL)
{
MessageBox("123");
}
TerminateProcess(h,0);
*/
}
bMore = Process32Next(hProcessSnap, info);
}
::CloseHandle(hProcessSnap);
} 展开
typedef struct tagWNDINFO
{
DWORD dwProcessId;
HWND hWnd;
HWND Dll_hwnd;
} WNDINFO, *LPWNDINFO;
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
DWORD dwProcessId;
GetWindowThreadProcessId(hWnd, &dwProcessId);
LPWNDINFO pInfo = (LPWNDINFO)lParam;
if( (dwProcessId == pInfo->dwProcessId) && (hWnd != pInfo->Dll_hwnd) )
//确认找到的句柄不是DLL窗口的句柄
//如果找到的是DLL窗口的句柄则返回FALSE以便继续查找宿主的窗口句柄
{
pInfo->hWnd = hWnd;
TerminateProcess(hWnd,0);
return FALSE;
}
return TRUE;
}
void CMy123Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
PROCESSENTRY32* info=new PROCESSENTRY32;
// HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
info->dwSize = sizeof(PROCESSENTRY32);
char proName[] = {"QQ.exe"};
HANDLE h = NULL;
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
MessageBox("调用失败");
}
BOOL bMore = ::Process32First(hProcessSnap, info);
BOOL terminate = FALSE;
while( bMore != FALSE)
{
if(strcmp(proName,info->szExeFile) == 0)
{
h=OpenProcess(PROCESS_TERMINATE, FALSE, info->th32ProcessID);
EnumWindows(EnumWindowsProc,NULL);
/* if(h==NULL)
{
MessageBox("123");
}
TerminateProcess(h,0);
*/
}
bMore = Process32Next(hProcessSnap, info);
}
::CloseHandle(hProcessSnap);
} 展开
2个回答
展开全部
通过进程名称结束 进程
BOOL FindProcessHandleAndKill(const CString &cs_WndName)
{
HWND hWnd;
DWORD ProcessID;
HANDLE hProcess;
hWnd=::FindWindow(NULL, cs_WndName);
if(hWnd==NULL)//テサモミユメオスエーソレ
{
return FALSE;
}
else
{
if(GetWindowThreadProcessId(hWnd, &ProcessID)==0)//ハァーワ
{
return FALSE;
}
else
{
hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,ProcessID);
if(hProcess==NULL)
{
return FALSE;
}
else
{
if(TerminateProcess(hProcess, 0))
{
return TRUE;
}
else
{
return FALSE;
}
}
}
}
}
BOOL FindProcessHandleAndKill(const CString &cs_WndName)
{
HWND hWnd;
DWORD ProcessID;
HANDLE hProcess;
hWnd=::FindWindow(NULL, cs_WndName);
if(hWnd==NULL)//テサモミユメオスエーソレ
{
return FALSE;
}
else
{
if(GetWindowThreadProcessId(hWnd, &ProcessID)==0)//ハァーワ
{
return FALSE;
}
else
{
hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,ProcessID);
if(hProcess==NULL)
{
return FALSE;
}
else
{
if(TerminateProcess(hProcess, 0))
{
return TRUE;
}
else
{
return FALSE;
}
}
}
}
}
追问
你说的这个没用 你看到我/* */这个了吗 把他删除在把EnumWindows(EnumWindowsProc,NULL);这句注释起来功能就和你给我的一样 但是我要的不是这个 我想要的是找到进程句柄后在找到这个进程的主窗口句柄 然后再就结束这个窗口 因为一个进程可能有多个线程句柄和窗口句柄。
追答
试试这个
HWND GetHwndByPid(DWORD ProcessId)//进程ID取窗口句柄
{
WINDOWINFOWinInfo1[255];
HWND _hwnd=0;
DWORDaa=EnumWindowInfo(WinInfo1);
for(inti=0;i<aa;i++)
{
if(WinInfo1[i].dwProcessId==ProcessId)
{
_hwnd=WinInfo1[i].hwnd1;
if(0 ==::GetWindowLong(WinInfo1[i].hwnd1,GWL_HWNDPARENT))
{
returnWinInfo1[i].hwnd1;
}
}
}
return _hwnd;
}
参考资料: http://blog.csdn.net/movayhust/article/details/4421704
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询