|
-
September 2nd, 2010, 04:21 AM
#1
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!
I thought the code would do the right thing as I expected, but nothing happens, it's really awful!
-
September 2nd, 2010, 04:35 AM
#2
Re: How to use ::TerminateProcess to terminate a process?
Use OpenProcess function.
-
September 2nd, 2010, 04:50 AM
#3
Re: How to use ::TerminateProcess to terminate a process?
 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?
Victor Nijegorodov
-
September 2nd, 2010, 08:06 PM
#4
Re: How to use ::TerminateProcess to terminate a process?
 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);
}
I thought the code would do the right thing as I expected, but nothing happens, it's really awful!
-
September 2nd, 2010, 08:44 PM
#5
Re: How to use ::TerminateProcess to terminate a process?
 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
Last edited by Arjay; September 3rd, 2010 at 01:17 PM.
-
September 2nd, 2010, 09:25 PM
#6
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!
I thought the code would do the right thing as I expected, but nothing happens, it's really awful!
-
September 3rd, 2010, 11:50 AM
#7
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.
-
September 3rd, 2010, 01:18 PM
#8
Re: How to use ::TerminateProcess to terminate a process?
 Originally Posted by fantasy1215
Sorry, my mistake. I've updated the link above.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|