Hello ALL.,
How do I termintate an application(say.."Media Player") which is opened using CreateProcess(...) ?
Thanks in advance...!
Printable View
Hello ALL.,
How do I termintate an application(say.."Media Player") which is opened using CreateProcess(...) ?
Thanks in advance...!
Have you tried sending WM_QUIT to it using PostMessage()?
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.
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
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
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.?
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.
I open the application like the following code :
Closing here ...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);
}
It doesn't work..Code:PostThreadMessage(pi.dwThreadId, WM_CLOSE, 0, 0);
I also tried with the code given HERE referred by mdmd ..(Thanks to him)..
But, failed...
Plz , help me out..
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 );
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.
Thanks a Million Kochhar for your suggestions...
Here is the code I already tried...
But, no success..:( hProc is NULL here..where do I go wrong..?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 ;
}
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) ;
Thanks mdmd.. & everone for your patience..
Unmodified code works fine...:)