CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2008
    Posts
    98

    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!

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: How to use ::TerminateProcess to terminate a process?

    Use OpenProcess function.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to use ::TerminateProcess to terminate a process?

    Quote Originally Posted by fantasy1215 View Post
    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

  4. #4
    Join Date
    Jan 2008
    Posts
    98

    Re: How to use ::TerminateProcess to terminate a process?

    Quote Originally Posted by VictorN View Post
    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!

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to use ::TerminateProcess to terminate a process?

    Quote Originally Posted by fantasy1215 View Post
    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.

  6. #6
    Join Date
    Jan 2008
    Posts
    98

    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!

  7. #7
    Join Date
    Jul 2002
    Posts
    2,543

    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.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to use ::TerminateProcess to terminate a process?

    Quote Originally Posted by fantasy1215 View Post
    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!
    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
  •  





Click Here to Expand Forum to Full Width

Featured