How to identify a stale handle?

Lets suppose you are keeping handles of all processes running in a system using OpenProcess, now it’s just normal that some processes may exit, but since you have a valid handle there is no way to tell whether the process is still running or whether it has exited. You cannot check for NULL since the process was running when OpenProcess was called.

Ha! So is there a way? Yes man! Grrrrrr!

We can use WaitForSingleObject with a zero timeout. If dwMilliseconds is zero, the function tests the object’s state and returns immediately. Yes this is the key, this is what we want, the state of the object i.e. whether it’s signaled or non-signaled. An opened process handle is in a signaled state when corresponding process exits or is killed and that’s the only time IMK a process handle can become signaled. So we call WaitForSingleObject with zero timeout and if the return value is WAIT_TIMEOUT then our baby is running else if the return value is WAIT_OBJECT_0 then our baby is no more running. 😉

So here is a function which identifies a stale process handle or the handle of process which is no more running. Function name is IsProcHandleValid.

[sourcecode language=’cpp’]bool IsProcHandleValid( HANDLE hProc )
{
if( !hProc )
{
ASSERT( FALSE );
return false;
}

// Just check signaled state of the process handle, it will
// become signaled whenever the process exits
const DWORD RetVal = WaitForSingleObject( hProc, 0 );
if( RetVal == WAIT_FAILED )
{
::MessageBox( GetForegroundWindow(), _T( “WaitForSingleObject call failed, most probably the handle does not have wait permission!” ), _T( “Error!” ), MB_OK | MB_ICONERROR );
return false;
}

return ( RetVal == WAIT_TIMEOUT );
}

void TestProcessHandle()
{
STARTUPINFO StartInfo = { 0 };
StartInfo.cb = sizeof( StartInfo );
PROCESS_INFORMATION ProcInfo = { 0 };

BOOL Result = CreateProcess( _T( “C:\windows\notepad.exe” ),
NULL,
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&StartInfo,
&ProcInfo );

ASSERT( Result );

// Open a handle to newly created “notepad” process
HANDLE hProc = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcInfo.dwProcessId );
ASSERT( hProc );

// Kill created process
if( MessageBox( NULL, _T( “Do you wanna kill this notepad instance?” ), “Kill notepad?”, MB_YESNO | MB_ICONQUESTION ) == IDYES )
{
PostThreadMessage( ProcInfo.dwThreadId, WM_QUIT, 0, 0 );

// Wait for notepad to exit
WaitForSingleObject( ProcInfo.hThread, INFINITE );
}

// Check validity of opened notepad handle
if( IsProcHandleValid( hProc ))
{
::MessageBox( NULL, _T( “We’ve got a live handle, notepad is still running.nnSo our test worked, YaY!!” ), “Testing process handle”, MB_OK );

// Now kill since we are leaving
PostThreadMessage( ProcInfo.dwThreadId, WM_QUIT, 0, 0 );
WaitForSingleObject( ProcInfo.hThread, INFINITE );
}
else
{
::MessageBox( NULL, _T( “Notepad is not running, we have a stale handle.nnSo our test worked, YaY!!” ), “Testing process handle”, MB_OK );
}// End if

// Release handles in ProcInfo returned by CreateProcess
CloseHandle( ProcInfo.hProcess );
CloseHandle( ProcInfo.hThread );

// Release opened handle
CloseHandle( hProc );
}

int main()
{
TestProcessHandle();
return 0;
}[/sourcecode]

Yup! that’s it, have fun coding!

One thought on “How to identify a stale handle?

Appreciate your comments...