Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check COM Interface still alive?

Tags:

c++

com

In COM how does one verify that a pointer to a COM object still has a valid object on the other end?

I have an issue where this following bit of code attempts to check if the m_pServer pointer is still alive, however when that application exposing that interface is killed this bit of code crashes the application. Can anybody advice on how to check the pointer before using it?

if (FAILED(m_pServer->StillAlive())) { // do something }

This code fails if m_pServer is no longer in memory.

EDIT:

EXCEPTION: First-chance exception at 0x7728fbae (kernel32.dll) in Client40.exe: 0x800706BA: The RPC server is unavailable.

CALL STACK:

    kernel32.dll!RaiseException()  + 0x58   
    rpcrt4.dll!RpcRaiseException()  + 0x3e  
    rpcrt4.dll!NdrProxyErrorHandler()  + 0x28   
    rpcrt4.dll!NdrProxySendReceive()  + 0xa4    
    rpcrt4.dll!NdrProxySendReceive()  + 0x119   
    rpcrt4.dll!NdrComplexArrayMarshall()  + 0x26d   
--> Client40.exe!SlaveDriver::run()  Line 97 + 0x14 C++  //Runs while loop, to handle requests
    Client40.exe!DThread::tfunc(void * thisptr=0x0047e694)  Line 56 + 0xd   C++
    Client40.exe!_threadstartex(void * ptd=0x01b20e00)  Line 241 + 0xd  C
    kernel32.dll!BaseThreadInitThunk()  + 0x12  
    ntdll.dll!RtlInitializeExceptionChain()  + 0x63 
    ntdll.dll!RtlInitializeExceptionChain()  + 0x36 
like image 750
Tony The Lion Avatar asked Aug 17 '10 08:08

Tony The Lion


1 Answers

What you're trying to do here is simply not possible. Because m_pServer lives in another process you're really asking the following question

Is Process XXX still running?

This is simply not an answerable question in the world of windows (or linux / unix). You can never reliably answer the question because the moment the question is answered it's result can be invalidated. Process's can terminate at any point in time including between your check and the access of the COM object.

However a slightly different version of this question is answerable

Was Process XXX still running?

The only way to approach a problem like this is to perform the operation in question and simply expect it to fail. If it succeeds then great you've answered the modified version of the question. If it fails then you need to interpret the failure to determine if the operation failed or the process is gone.

This is the only way to properly handle this situation.

like image 138
JaredPar Avatar answered Sep 24 '22 14:09

JaredPar