Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TerminateProcess fails with ERROR_ACCESS_DENIED because of COM object ??

2,082 views
Skip to first unread message

Chris Shearer Cooper

unread,
Aug 7, 2008, 8:10:49 AM8/7/08
to
My program starts up a second process, this other process then does
various things including creating a COM object. If the main program
detects that the second process is hung, it tries to call
TerminateProcess() to make the second process go away. The problem
is, TerminateProcess() sometimes fails with ERROR_ACCESS_DENIED.

My code first calls TerminateProcess() using the process handle
returned in the PROCESS_INFORMATION structure during CreateProcess(),
and if that fails I then call OpenProcess(PROCESS_TERMINATE, ...) to
make sure I have terminate rights, and then call TerminateProcess()
using that handle, and it still fails with ERROR_ACCESS_DENIED.

So what could be preventing TerminateProcess() from doing its dirty
work? Is it because the second process has a COM object still active
(it's probably the COM object that's causing the process to hang)?
What can I do, to make sure that I can terminate this second process?

Thanks,
Chris

Alexander Grigoriev

unread,
Aug 7, 2008, 10:24:32 AM8/7/08
to
Make sure you don't habitually close the process handle immediately after
CreateProcess call. This handle has full rights to the process.

Make sure to check return value from CreateProcess for zero, before calling
GetLastError().

"Chris Shearer Cooper" <chris.shea...@gmail.com> wrote in message
news:a3183305-0a61-403e...@f36g2000hsa.googlegroups.com...

Chris Shearer Cooper

unread,
Aug 7, 2008, 11:11:37 AM8/7/08
to
Nope, the process handle is valid, I'm not closing it prematurely, and
especially since I'm successfully calling OpenProcess(), I'm
definitely working with a valid process handle.

I am checking all functions, including CreateProcess(), for failure.
They are all fine, except for TerminateProcess().

Chris

On Aug 7, 8:24 am, "Alexander Grigoriev" <al...@earthlink.net> wrote:
> Make sure you don't habitually close the process handle immediately after
> CreateProcess call. This handle has full rights to the process.
>
> Make sure to check return value from CreateProcess for zero, before calling
> GetLastError().
>

> "Chris Shearer Cooper" <chris.shearer.coo...@gmail.com> wrote in messagenews:a3183305-0a61-403e...@f36g2000hsa.googlegroups.com...

Alexander Grigoriev

unread,
Aug 7, 2008, 10:41:38 PM8/7/08
to
Show your TerminateProcess call and how you check its success.

"Chris Shearer Cooper" <chris.shea...@gmail.com> wrote in message

news:37c27195-35db-478b...@2g2000hsn.googlegroups.com...

Chris Shearer Cooper

unread,
Aug 7, 2008, 11:45:24 PM8/7/08
to
BOOL bCreate = CreateProcess(szExe, (char *)(LPCTSTR)szCmd, NULL,
NULL, FALSE,
CREATE_NO_WINDOW, NULL, NULL, &startup, &process);
if (!bCreate)
{
TRACE("*** Unable to launch %s\n", szExe);
return false;
}

m_hProcess = process.hProcess;
m_lProcessId = process.dwProcessId;
CloseHandle(process.hThread); // We don't keep the thread handle

// and then much later ...

BOOL bTerminate = ::TerminateProcess(m_hProcess, 12);
if (bTerminate)
return;

DWORD lError = GetLastError();
if (lError != ERROR_ACCESS_DENIED)
{
TRACE("Error %d terminating (process %d)\n", lError,
m_lProcessId);
return;
}

// Access is denied.
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE,
m_lProcessId);
if (hProcess == NULL)
{
DWORD lError = GetLastError();
TRACE("OpenProcess(%d) failed with error %d\n", m_lProcessId,
lError);
return;
}

bTerminate = ::TerminateProcess(hProcess, 12);
if (bTerminate)
{
DWORD lError = GetLastError();
TRACE("Only able to terminate after OpenProcess(PROCESS_TERMINATE)
\n");
}
else
{
DWORD lError = GetLastError();
TRACE("Error %d terminating (process %d)\n", lError,
m_lProcessId);
}

CloseHandle(hProcess);

On Aug 7, 8:41 pm, "Alexander Grigoriev" <al...@earthlink.net> wrote:
> Show your TerminateProcess call and how you check its success.
>

> "Chris Shearer Cooper" <chris.shearer.coo...@gmail.com> wrote in messagenews:37c27195-35db-478b...@2g2000hsn.googlegroups.com...

Alexander Grigoriev

unread,
Aug 8, 2008, 12:47:11 AM8/8/08
to
Are both processes 32 bit or 64 bit? Does any of them run with elevated
privileges?

"Chris Shearer Cooper" <chris.shea...@gmail.com> wrote in message

news:e182d577-5d52-4a8d...@x35g2000hsb.googlegroups.com...

roge...@gmail.com

unread,
Aug 8, 2008, 3:46:29 AM8/8/08
to
Are you running any anti-virus/anti-spyware software that might be
hooking the calls?

Regards,
Roger.

Chris Shearer Cooper

unread,
Aug 8, 2008, 8:52:46 AM8/8/08
to
Both processes are 32-bit, I'm not doing anything to elevate
privileges (and I'm doing my testing on Windows XP), I do have WebRoot
Spy Sweeper running, I will try turning that off and re-testing.

Thanks,
Chris

10_27@discussions.microsoft.com luke 10_27

unread,
Sep 18, 2008, 11:09:01 AM9/18/08
to
Had a similar problem OpenProcess OK but Terminate fails with Access Denied
when timing out lock of a shared resource owned by another process. Found out
this was due to a second execution of the time out function. In my case the
process was terminated successfully on the first time through (disappeared
from task manager etc) - but due to an error the resource was not cleaned up
and also all handles to the process not closed - thus a 2nd execution of the
timeout function occurred - the OpenProcess was again fine but the
TerminateProcess failed with Access Denied.
0 new messages