URGENT: How can a kill an application I start myself?

Hi
I want to run an application and then with another command be able to kill that application.
Whats the easiest way to do this?
Thanks.
[164 byte] By [Deniz] at [2007-11-18 20:32:50]
# 1 Re: URGENT: How can a kill an application I start myself?
Open and close an Application ...
HANDLE hProc ;
DWORD dwRet ;

STARTUPINFO si;
::ZeroMemory(&si,sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
::ZeroMemory(&si,sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);

hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, piVol.dwProcessId) ;
if(hProc == NULL){
if(CreateProcess(NULL,"NEWAPPLICATION.exe" , NULL,
NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&piVol))
{
::CloseHandle(piVol.hThread);
::CloseHandle(piVol.hProcess);
}
}
else{
// TerminateAppEnum() posts WM_CLOSE to all windows whose PID
// matches your process's.
EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) piVol.dwProcessId) ;

// Wait on the handle. If it signals, great. If it times out,
// then you kill it.
if(WaitForSingleObject(hProc, 0)!=WAIT_OBJECT_0)
dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED);
else
dwRet = TA_SUCCESS_CLEAN ;

CloseHandle(hProc) ;
}

BOOL CALLBACK TerminateAppEnum(HWND hwnd, LPARAM lParam)
{
DWORD dwID ;

GetWindowThreadProcessId(hwnd, &dwID) ;

if(dwID == (DWORD)lParam)
{
PostMessage(hwnd, WM_CLOSE, 0, 0) ;
}

return TRUE ;

}
Hope, this helps.!
newnick at 2007-11-11 1:12:31 >
# 2 Re: URGENT: How can a kill an application I start myself?
Originally posted by Deniz
Hi

I want to run an application and then with another command be able to kill that application.

Whats the easiest way to do this?

Thanks.

The simplest way:

HWND hWnd = FindWindowEx(
HWND hwndParent, // handle to parent window
HWND hwndChildAfter, // handle to a child window
LPCTSTR lpszClass, // pointer to class name
LPCTSTR lpszWindow // pointer to window name
);

::PostMessage(hWnd, WM_CLOSE, 0, 0);
Alin at 2007-11-11 1:13:31 >
# 3 Re: URGENT: How can a kill an application I start myself?
Simply take a look at the following FAQs:

How can I start a process ? ( http://www.dev-archive.com/forum/showthread.php?s=&threadid=231233)
How can I kill a process ? ( http://www.dev-archive.com/forum/showthread.php?s=&threadid=231236)
Andreas Masur at 2007-11-11 1:14:28 >