|
-
October 22nd, 2003, 11:55 PM
#1
Terminate an application...!
Hello ALL.,
How do I termintate an application(say.."Media Player") which is opened using CreateProcess(...) ?
Thanks in advance...!
-
October 23rd, 2003, 12:06 AM
#2
Have you tried sending WM_QUIT to it using PostMessage()?
-
October 23rd, 2003, 12:16 AM
#3
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.
-
October 23rd, 2003, 12:30 AM
#4
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
-
October 23rd, 2003, 12:32 AM
#5
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
-
October 23rd, 2003, 12:35 AM
#6
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.?
-
October 23rd, 2003, 01:04 AM
#7
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.
-
October 23rd, 2003, 01:06 AM
#8
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..
-
October 23rd, 2003, 01:13 AM
#9
I also tried with the code given HERE referred by mdmd ..(Thanks to him)..
But, failed...
Plz , help me out..
-
October 23rd, 2003, 01:24 AM
#10
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 );
-
October 23rd, 2003, 01:25 AM
#11
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.
-
October 23rd, 2003, 01:30 AM
#12
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.
-
October 23rd, 2003, 01:34 AM
#13
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) ;
-
October 23rd, 2003, 01:54 AM
#14
Thanks mdmd.. & everone for your patience..
Unmodified code works fine...
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
|