By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

Terminating Process

P: n/a
GTS
Hi All,

I am spawning a process from a service. The spawned process hungs for
various reasons, (corrupted data, deadlock). I am expecting the process has
to complete the task with in the expected time limit, if it exceeds the
limit I want to terminate the process (no mercy or graceful, just terminate
the process).

The spawnng of the process, counting the time and termination of process all
done in C++. Some time I am not able to terminate the process, the error
message was "Access denied". I dont want this also happens. The program has
to terminate the process even, the process in any state. How to do that?
What security permissions I have to give?

Here is the code snippet of the teminate application,
================================================== =======
static DWORD WINAPI 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 ;
}

// TerminateAppEnum() posts WM_CLOSE to all windows whose PID
// matches your process's.
EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ;

// 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:T A_FAILED);
else
dwRet = TA_SUCCESS_CLEAN ;

CloseHandle(hProc) ;

return dwRet ;
}
static BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam )
{
DWORD dwID ;

GetWindowThreadProcessId(hwnd, &dwID) ;

if(dwID == (DWORD)lParam)
{
PostMessage(hwnd, WM_CLOSE, 0, 0) ;
}

return TRUE ;
}

================================================== =======

All suggestion appreciated.

Thanks

GTS
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"GTS" <gt******@hotmail.com> wrote in message news:<O1**************@TK2MSFTNGP12.phx.gbl>...
Hi All,

I am spawning a process from a service. The spawned process hungs for
various reasons, (corrupted data, deadlock). I am expecting the process has
to complete the task with in the expected time limit, if it exceeds the
limit I want to terminate the process (no mercy or graceful, just terminate
the process).
...


Try this.

HANDLE h = OpenProcess(PROCESS_TERMINATE,FALSE,dwId);
TerminateProcess(h,0);

sending a wm_close to a "hung" process does not really help much....

hth.
-Vinayak
Nov 17 '05 #2

P: n/a
JJ
Or a better idea would be to send WM_CLOSE, wait some time, then check to
see if the process still exists. If it does, then do TerminateProcess() as
a last resort.
"Vinayak Raghuvamshi" <vs************@hotmail.com> wrote in message
news:9a**************************@posting.google.c om...
"GTS" <gt******@hotmail.com> wrote in message

news:<O1**************@TK2MSFTNGP12.phx.gbl>...
Hi All,

I am spawning a process from a service. The spawned process hungs for
various reasons, (corrupted data, deadlock). I am expecting the process has to complete the task with in the expected time limit, if it exceeds the
limit I want to terminate the process (no mercy or graceful, just terminate the process).
...


Try this.

HANDLE h = OpenProcess(PROCESS_TERMINATE,FALSE,dwId);
TerminateProcess(h,0);

sending a wm_close to a "hung" process does not really help much....

hth.
-Vinayak

Nov 17 '05 #3

P: n/a
"JJ" <jj**@nospam.com> wrote in message news:Oh*************@TK2MSFTNGP10.phx.gbl...
Or a better idea would be to send WM_CLOSE, wait some time, then check to
see if the process still exists. If it does, then do TerminateProcess() as
a last resort.


Isn't that what the original poster did?
He sends the windows of the process the WM_CLOSE, then does WaitForSingleObject on the process. If it times-out rather than
signals, he calls TerminateProcess.

--
Adam Clauss
ca*****@tamu.edu

Nov 17 '05 #4

P: n/a
Could you check the process identity the service runs with?
If it's:
"local system" AND
"Allow service to interact with desktop" is enabled,
AND the spawned process has a windows message loop (message pump).
then posting ( WM_CLOSE ) should work.

However, this won't work If "Allow service to interact with desktop" is not
set, as you can't post/send messages from the interactive desktop your
"terminate" program runs in to the sandboxed services desktop/winstation
your spawned process runs in.
"TerminateProcess" will not work either, as the spawned process runs as
"local system" it can only be terminated by a process running as "local
system", even an administrator can't terminate such process.

Willy.
"GTS" <gt******@hotmail.com> wrote in message
news:O1**************@TK2MSFTNGP12.phx.gbl...
Hi All,

I am spawning a process from a service. The spawned process hungs for
various reasons, (corrupted data, deadlock). I am expecting the process
has
to complete the task with in the expected time limit, if it exceeds the
limit I want to terminate the process (no mercy or graceful, just
terminate
the process).

The spawnng of the process, counting the time and termination of process
all
done in C++. Some time I am not able to terminate the process, the error
message was "Access denied". I dont want this also happens. The program
has
to terminate the process even, the process in any state. How to do that?
What security permissions I have to give?

Here is the code snippet of the teminate application,
================================================== =======
static DWORD WINAPI 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 ;
}

// TerminateAppEnum() posts WM_CLOSE to all windows whose PID
// matches your process's.
EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ;

// 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:T A_FAILED);
else
dwRet = TA_SUCCESS_CLEAN ;

CloseHandle(hProc) ;

return dwRet ;
}
static BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam )
{
DWORD dwID ;

GetWindowThreadProcessId(hwnd, &dwID) ;

if(dwID == (DWORD)lParam)
{
PostMessage(hwnd, WM_CLOSE, 0, 0) ;
}

return TRUE ;
}

================================================== =======

All suggestion appreciated.

Thanks

GTS

Nov 17 '05 #5

P: n/a
JJ
Sorry - I missed that.

"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:O1*************@TK2MSFTNGP12.phx.gbl...
"JJ" <jj**@nospam.com> wrote in message news:Oh*************@TK2MSFTNGP10.phx.gbl...
Or a better idea would be to send WM_CLOSE, wait some time, then check to see if the process still exists. If it does, then do TerminateProcess() as a last resort.


Isn't that what the original poster did?
He sends the windows of the process the WM_CLOSE, then does

WaitForSingleObject on the process. If it times-out rather than signals, he calls TerminateProcess.

--
Adam Clauss
ca*****@tamu.edu

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.