C++/VB - TerminateProcess fails with ERROR_ACCESS_DENIED because of COM object ??
Asked By Chris Shearer Cooper
09-Aug-08 12:54 AM
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
Windows XP
(1)
GetLastError
(1)
Process.dwProcessId
(1)
TerminateProcess
(1)
CreateProcess
(1)
CloseHandle
(1)
OpenProcess
(1)
LProcessId
(1)
Alexander Grigoriev replied...
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 replied...
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
Alexander Grigoriev replied...
Show your TerminateProcess call and how you check its success.
Chris Shearer Cooper replied...

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);
Alexander Grigoriev replied...
Are both processes 32 bit or 64 bit? Does any of them run with elevated
privileges?
roger.or replied...
Are you running any anti-virus/anti-spyware software that might be
hooking the calls?
Regards,
Roger.
Chris Shearer Cooper replied...
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
luke 10_2 replied...
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.

M, T1836%T<FEX5')A;G-L871I;VXH)FUA = % = O<FQD+#$P+C!F+# N, &8L, "XP M9BD[#0H)<&0S9$1E = FEC92T^4V5T5')A;G-F;W)M*"!$, T144U]73U), 1"PF C P, # P, #LL"B @.38N, S Y.38W.RTW, 2XR, 3<W, 3([ M, "XP, # P, # [+ H@("TY-BXS, #DY-C<[-S$N, C$W-S$R.S N, # P Y, 3$W.RP*(" M M.38N, S Y.38W.RTW, 2XR, 3<W, 3([, "XP, # P, # [+ H@(#DV+C, P.3DV-SLM M-S$N, C$W-S$R.S CLP+C P, # P, #LL"B @+3DV+C, P.3DV-SLW, 2XR, 3<W, 3([, "XP, # P M, # [+ H@("TY-BXS, #DY-C<[-S$N, C$W-S$R.S$Q C0S.3$Q-SLL"B @+3DV+C, P.3DV-SLW M, 2XR, 3<W, 3([, "XP, # P, # [+ H@("TY-BXS, #DY-C<[+3<Q+C(Q-S<Q, CLQ M, 3 3@L, "PQ.3LL"B @, SLQ.2PV+#$X.SL*"B @ M365S:$YO<FUA;', @('L*(" @-CL*(" @, "XP, # P, # [, "XP, # P, # [+3$N M, # P, # P.RP*(" @, "XP, # P, # [, "XP, # P, # [, 2XP, # P, # [+ H@(" P M+C P, # P, #LM, 2XP, # P, # [, "XP
C!F M+" P+C!F+"TS, "XP9B I.PT*(" @($0S1%A614-43U(S('95<%9E8R@@, "XP M9BP@, 2XP9BP@, "XP9B I.PT*(" @($0S1%A-051225A!, 38@;6%T5FEE = SL- M"B @("!$, T1836%T<FEX3&]O P+C P, # P, #LP+C P, # P M, #LP+C P, # P, #LL"B @, "XP, # P, # [, "XP, # P, # [, "XP, # P, # [+ H@ M(# N, # P, # P.S N, # P, # P.S N, # P, # P.RP P+C P, # P, #LP+C P M, # P, #LP+C P, # P, #LL"B @, "XP, # P, # [, "XP, # P, # [, "XP, # P, # [ M+ H@(# N, # P, # P.S N, # P, # P.S N, # P, # P.RP P+C P, # P, #LP M+C P, # P, #LP+C P, # P, #LL"B @, "XP, # P, # [, "XP, # P, # [, "XP, # P M, # [.PH@(#$R.PH@(#, [, "PS+#([+ H@(#, [, RPP+#$[+ H@(#, [-"PW+#4
C++ / VB Problem with XP scheduler? I am having firefox 3.5.5 and 3.5.6 freeze my box when I update several tabs in a rapid succession on Win XP SP3. What happens is box totally freezes. Task manager -> Performance tab shows one of cores ctl-alt-del) and disk stops flashing every few seconds as it normally does under XP. Interestingly enough, it does not happen under Windows 7 on the same box no matter how hard I try. Some say it is a problem with crappy XP scheduler. Is there anything I can do under XP to fix this issue? Hardware: Asus M4A78 motherboard, Athlon 2, X4 620 CPU, 4 Gigs of DDR2 ram by Team Extreme. Running on Windows XP SP3. - - Programmer's Goldmine collections: http: / / preciseinfo.org I'd say it is a problem
C++ / VB Windows Vista and NetSharingManager.EnumEveryConnection Hello, The following code snippet works on Windows XP SP2 (default user privilege), but not on Windows Vista (also only with default user privilege)! :-( Dim icsMgr As New NetSharingManager For Each icsConn In icsMgr.EnumEveryConnection But, when I debug the code above as a local adminstrator on Windows Vista, then it works (no exceptions is thrown). What is the correct way to enumerate all network connections on Windows Vista, without local administrator privilege? Thanks and Regards, andersch Hi, To let me better understand file as follows: IDR_MANIFEST RT_MANIFEST MOVEABLE PURE { manifestVersion = ""1.0""> level = ""requireAdministrator"" uiAccess = ""false"" / > } If windows platform DK has been installed on your computer, please run rc.exe to compile the I'm using this API for a VPN quarantine agent. This agent checks if the windows firewall is enabled for each network connection (not only the global operations mode). The VPN