How to use ::TerminateProcess to terminate a process?
I only know the HWND as an input, first I use GetWindowThreadProcessId to get the process ID. But then how can I convert the process ID as the first input parameter of TerminateProcess which is HANDLE?
Code:
DWORD dwProcessID = 0;
::GetWindowThreadProcessId(hWndGot, &dwProcessID);
Thanks in advance!
Re: How to use ::TerminateProcess to terminate a process?
Use OpenProcess function.
Re: How to use ::TerminateProcess to terminate a process?
Quote:
Originally Posted by
fantasy1215
I only know the HWND as an input, ...
But then how can I convert the process ID as the first input parameter of TerminateProcess which is HANDLE?
Did you read about using TerminateProcess in MSDN?
Doesn't any "clean" way to close a process work?
If the HWND you refer to is the handle of the main window - why not just PostMessage WM_CLOSE to it?
Re: How to use ::TerminateProcess to terminate a process?
Quote:
Originally Posted by
VictorN
Did you read about using TerminateProcess in MSDN?
Doesn't any "clean" way to close a process work?
If the HWND you refer to is the handle of the main window - why not just PostMessage WM_CLOSE to it?
It's kind of you to remind me of closing the process in a 'clean' way. Because the HWND is not the main window of the process, so I have to use TerminateProcess.
So I should use the code below to TerminateProcess? for I'm not sure. Thanks.
Code:
HANDLE hProcess = NULL;
hProcess = ::OpenProcess(0, 0, dwProcessID);
if (hProcess)
{
::TerminateProcess(hProcess, 0);
::CloseHandle(hProcess);
}
Re: How to use ::TerminateProcess to terminate a process?
Quote:
Originally Posted by
fantasy1215
It's kind of you to remind me of closing the process in a 'clean' way. Because the HWND is not the main window of the process, so I have to use TerminateProcess.
Not really. Given a window handle use GetWindowThreadProcessID to retrieve its processId. From there you can use the ModuleTest code in the following link to politely close the process.
http://www.codeguru.com/forum/showth...ght=ModuleTest
Re: How to use ::TerminateProcess to terminate a process?
Thanks for your quick reply.
But I can't find the ModuleTest in the link(http://msdn.microsoft.com/en-us/libr...22(VS.85).aspx). Sorry!
Re: How to use ::TerminateProcess to terminate a process?
Yes, you can terminate process by this way, you need only to use PROCESS_TERMINATE as first OpenProcess parameter.
Re: How to use ::TerminateProcess to terminate a process?
Quote:
Originally Posted by
fantasy1215
Sorry, my mistake. I've updated the link above.