检查COM接口是否仍然有效? [英] Check COM Interface still alive?

查看:75
本文介绍了检查COM接口是否仍然有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在COM中,如何验证指向COM对象的指针在另一端仍具有有效对象?

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

我有一个问题,下面的这段代码尝试检查 m_pServer 指针是否仍然有效,但是当暴露该接口的应用程序被杀死时,这段代码使该应用程序崩溃.有人可以建议使用前如何检查指针吗?

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 }

如果m_pServer不再在内存中,则此代码将失败.

This code fails if m_pServer is no longer in memory.

例外: Client40.exe中0x7728fbae(kernel32.dll)的首次机会异常:0x800706BA:RPC服务器不可用.

呼叫堆栈:

    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 

推荐答案

在这里尝试执行的操作根本不可能.因为 m_pServer 存在于另一个进程中,所以您实际上是在问以下问题

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

"XXX"进程仍在运行吗?

Is Process XXX still running?

在Windows(或linux/unix)世界中,这根本不是一个可以回答的问题.您永远无法可靠地回答问题,因为一旦回答问题,其结果就会失效.进程可以在任何时间点终止,包括在检查和访问COM对象之间.

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

"XXX"进程仍在运行吗?

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.

这篇关于检查COM接口是否仍然有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆