windows - 从 EXE COM 服务器检测客户端进程终止

标签 windows com

我正在编写一个 EXE COM 服务器,它公开一个锁定系统资源的类。 在正常执行中,客户端释放资源(COM 可执行文件在几秒钟后关闭。 在异常执行中,客户端应用程序崩溃,给 com 服务器留下一个引用计数为正的实例。 COM 可执行文件运行约 12 分钟直至终止。这意味着系统资源在此期间被锁定。

有没有办法像在套接字 IPC 或驱动程序协议(protocol)中一样立即检测客户端终止?否则,COM 似乎不如其他 IPC 机制。

最佳答案

几年前我也有过同样的问题。我在这里找到了答案:How To Turn Off the COM Garbage Collection Mechanism .简而言之:不,没有办法立即检测到客户端终止。摘录:

When a COM client terminates normally, it releases all references to its server object. When a client terminates abnormally however, there might be outstanding references to the server object. Without a garbage collection mechanism, the server code has no way of knowing when to reclaim the resources allocated for the COM object, which can then cause a resource leak. To address this problem, COM implements an automatic garbage collection mechanism in which the COM resolver process (RPCSS) on the client machine pings the server machine on behalf of the client process.

Alternatives to using COM's GC protocol (for example, using periodic application-level "pings"--method calls that inform the object that clients are still alive, or using an underlying transport mechanism such as TCP keepalives) are demonstrably much less efficient. Therefore, DCOM's default GC mechanism should be used for any objects that must be shut down when their clients disappear or otherwise misbehave if those objects would effectively become memory leaks on the server.

The resolver on the server machine keeps track of the pings for each server object. The ping period is 2 minutes and, currently, it is non- configurable. When the resolver on the server machine detects that an object has not been pinged for 6 minutes, it assumes that all clients of the object have terminated or otherwise are no longer using the object. The resolver will then release all external references to the object. It does this by simply having the object's stub manager (the COM runtime code that delivers calls to each object) call ::Release() on the object's IUnknown interface. At this point, the object's reference count will be zero so far as the COM runtime is concerned. (There may still be references held by local (same-apartment) clients, so the object's internal reference count may not necessarily go to zero at this point.) The object may then shut itself down.

NOTE: Garbage collection applies to all servers regardless of whether their clients are local or remote, or a combination of local and remote. The underlying pinging mechanism is different in the local case as no network packets are generated, but for all practical purposes, the behavior is the same.

关于windows - 从 EXE COM 服务器检测客户端进程终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1049649/

相关文章:

C++:如何以编程方式在 Windows 凭据管理器中创建本地用户登录凭据,以便 "runas/savecred"可以使用它?

windows - 如何在 Windows 上创建虚拟网络接口(interface)?

python - 如何使用 setuptools 安装可写共享和用户特定数据文件?

c# - 使用 TPL 和进程外 COM 服务器的 C# WPF 应用程序出现奇怪问题

php - Apache, PHP, 找不到指定的模块

c++ - 适用于 Windows 的开源 C++ 调试器

python - Win32com 代码无法在 IIS 上运行

c++ - IDropTarget::Drop 方法 Hook

c++ - COM 错误 0x80004003(无效指针)访问 MS Outlook 联系人

c# - 公开 .Net 服务