The process that ends the program when it does not know the window handle of the program

Source: Internet
Author: User

If you just judge the name of the EXE (do not need to judge the path), you can omit enumprocess, because the pe32.szexefile already have the name of the EXE, because there is no path, so the function enumprocess to get the full path. This is a little simpler.

BOOL CALLBACK Enumwinproc (HWND hwnd, LPARAM LPARAM)
{
DWORD Dwid;
GetWindowThreadProcessId (hwnd, &DWID);
if (Dwid = = (DWORD) lParam) {
PostMessage (hwnd, wm_quit, 0, 0);
return FALSE;
}
return TRUE;
}
void Killprogram (LPCSTR exename)
{
Char *file;
HANDLE Hprocesssnap;
PROCESSENTRY32 pe32;
if (! EXEName | | ! Exename[0])
Return
File = STRRCHR (exename, ' \ \ ');
if (file!=0) exename = file+1;
Hprocesssnap = CreateToolhelp32Snapshot (
th32cs_snapprocess, 0);
if (Hprocesssnap = = (HANDLE)-1) return;
memset (&pe32, 0, sizeof (PE32));
pe32.dwsize = sizeof (PROCESSENTRY32);
if (Process32First (Hprocesssnap, &pe32))
{
do {
memset (&me32, 0, sizeof (ME32));
me32.dwsize = sizeof (ME32);
File = STRRCHR (pe32.szexefile, ' \ \ ');
File = file? File+1:pe32.szexefile;
if (Strcmpi (file,exename) ==0) {
EnumWindows ((Wndenumproc) Enumwinproc,
PE32.TH32PROCESSID);
}
}
while (Process32Next (HPROCESSSNAP,&PE32));
}
CloseHandle (HPROCESSSNAP);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.