CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2002
    Location
    Princeton, NewJersey, US
    Posts
    817

    Terminate an application...!

    Hello ALL.,
    How do I termintate an application(say.."Media Player") which is opened using CreateProcess(...) ?

    Thanks in advance...!
    GOOD DAY..!

    NICK.

    E-MAIL : [email protected]

  2. #2
    Join Date
    Oct 2003
    Posts
    31
    Have you tried sending WM_QUIT to it using PostMessage()?

  3. #3
    Join Date
    Jul 2002
    Location
    Princeton, NewJersey, US
    Posts
    817
    Thanks a million Hobbieman for your quick response..

    Would you please write me a small example.?

    I already mentioned that e.g; Media player is opened using CreateProcess(..) and I would like to terminate MediaPlayer.
    GOOD DAY..!

    NICK.

    E-MAIL : [email protected]

  4. #4
    Join Date
    May 2003
    Posts
    347
    Hi


    Do a FindWindow to that Media player.

    it will give you CWnd,after that send a message to that Wnd with WM_QUIT.

    Hope this helps

    Regards

  5. #5
    Join Date
    Dec 2002
    Posts
    1,050
    Available in the MSDN with sample code that you can easily take
    out and change for your needs.
    HOWTO: Terminate an Application "Cleanly" in Win32 Q178893

  6. #6
    Join Date
    Jul 2002
    Location
    Princeton, NewJersey, US
    Posts
    817
    Thanks Spicy Kids ..for your reply..

    Well.., partially u are correct..
    But, in case there more than 1 Media Player opened... (One is manually & other is from my program using CreateProcess(..) )then according to you, it will close all the Media players.. Right.?

    But, I would like to close the only Media Player opened by my program using CreateProcess...

    any hint.?
    GOOD DAY..!

    NICK.

    E-MAIL : [email protected]

  7. #7
    Join Date
    Aug 1999
    Location
    Wisconsin
    Posts
    507
    When you create a process with CreateProcess(), it returns
    the Process ID of the created process in the
    "Process Information" structire. You can use
    that to distinguish between multiple instances
    of the Media Player.

  8. #8
    Join Date
    Jul 2002
    Location
    Princeton, NewJersey, US
    Posts
    817
    I open the application like the following code :
    Code:
    STARTUPINFO si;
    	::ZeroMemory(&si,sizeof(STARTUPINFO));
    	si.cb = sizeof(STARTUPINFO);
    	PROCESS_INFORMATION pi;    
    	::ZeroMemory(&si,sizeof(STARTUPINFO));
    	si.cb = sizeof(STARTUPINFO);
    	
    	if(bVolFlag == FALSE){
    		
    
    	if(CreateProcess(NULL,"D:\Program Files\\Windows Media Player\\wmplayer.exe" , NULL,
    				NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi))
    	{
    		//	::CloseHandle(pi.hThread);
    		//	::CloseHandle(pi.hProcess);
    	}
    Closing here ...
    Code:
    PostThreadMessage(pi.dwThreadId, WM_CLOSE, 0, 0);
    It doesn't work..
    GOOD DAY..!

    NICK.

    E-MAIL : [email protected]

  9. #9
    Join Date
    Jul 2002
    Location
    Princeton, NewJersey, US
    Posts
    817
    I also tried with the code given HERE referred by mdmd ..(Thanks to him)..

    But, failed...

    Plz , help me out..
    GOOD DAY..!

    NICK.

    E-MAIL : [email protected]

  10. #10
    Join Date
    Dec 2002
    Posts
    1,050
    Are you using the process ID or the thread ID with the articles
    code ? I'm using your CreateProcess code, with a different
    windows media exe name, and the articles code without
    modification. It closes the media player OK.

    Code:
    if(CreateProcess( "C:\\Program Files\\Windows Media Player\\mplayer2.exe",NULL , NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi))
    	{
    		//	::CloseHandle(pi.hThread);
    		//	::CloseHandle(pi.hProcess);
    	}
    
    TerminateApp( pi.dwProcessId , 1000 );

  11. #11
    Join Date
    Aug 1999
    Location
    Wisconsin
    Posts
    507
    Here is one suggestion, which should work:

    Since you have the process ID (in pi.dwProcessID)
    use that to call OpenProcess(), which will give you
    a handle to your process.

    Next, call TerminateProcess() giving it the handle to the
    process, which should terminate it.

    A bit drastic, but it will work.

  12. #12
    Join Date
    Jul 2002
    Location
    Princeton, NewJersey, US
    Posts
    817
    Thanks a Million Kochhar for your suggestions...

    Here is the code I already tried...
    Code:
    #define TA_FAILED 0
    #define TA_SUCCESS_CLEAN 1
    #define TA_SUCCESS_KILL 2
    #define TA_SUCCESS_16 3
    
    DWORD CGlobalTestDlg::TerminateApp(DWORD dwPID, DWORD dwTimeout)
    {
    	
    	  HANDLE   hProc ;
          DWORD   dwRet ;
    
          // If we can't open the process with PROCESS_TERMINATE rights,
          // then we give up immediately.
          hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE,
             dwPID);
    
          if(hProc == NULL)
          {
             return TA_FAILED ;
          }
          // Wait on the handle. If it signals, great. If it times out,
          // then you kill it.
          if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0)
             dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED);
          else
             dwRet = TA_SUCCESS_CLEAN ;
    
          CloseHandle(hProc) ;
    
          return dwRet ;
    
    }
    But, no success.. hProc is NULL here..where do I go wrong..?
    Last edited by newnick; October 23rd, 2003 at 01:35 AM.
    GOOD DAY..!

    NICK.

    E-MAIL : [email protected]

  13. #13
    Join Date
    Dec 2002
    Posts
    1,050
    Why did you remove this line ? If you use the unmodified code
    it should close all the windows and terminate the process of the
    WMPLAYER process id that you pass to TerminateApp
    Code:
    EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ;

  14. #14
    Join Date
    Jul 2002
    Location
    Princeton, NewJersey, US
    Posts
    817
    Thanks mdmd.. & everone for your patience..

    Unmodified code works fine...
    GOOD DAY..!

    NICK.

    E-MAIL : [email protected]

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