CComModule :: Unlock(); [英] CComModule::Unlock();

查看:280
本文介绍了CComModule :: Unlock();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图确定这个函数是什么,但是我似乎不能在CComModule类的MSDN文档下找到它。



此函数用于 DllCanUnloadNow() 以正常工作。



你知道当你为一个in-proc服务器调用 CoCreateInstance()时,COM会自动调用 LoadLibraryEx ()以根据需要加载COM服务器DLL。但DLL保持加载多长时间?事实上,COM会定期调用每个加载的COM服务器DLL的 DllCanUnloadNow()。如果它返回 S_OK COM允许调用 FreeLibrary()



何时可以安全卸载DLL?显然,你不能卸载它,直到DLL实现的所有对象被销毁。所以这里有锁计数 - 一个全局整数变量计数由DLL实现的活对象的数量。



当创建一个新的COM对象 - CComModule :: Lock()从其构造函数(通常 CComObject 构造函数),并在对象被销毁时递增变量 - CComModule :: Unlock ()从它的析构函数中调用并递减变量。当 CComModule :: GetLockCount()返回零时,意味着没有活对象,可以安全卸载DLL。



因此,锁计数非常类似于由 IUnknown 实现的引用计数。引用计数为每个对象,锁定计数为每个COM in-proc服务器。


I've been trying to determine what this function does, however I cannot seem to find it anywhere under the MSDN documentation of the CComModule class.

Could anyone tell me what it is used for?

解决方案

This function is for DllCanUnloadNow() to work properly.

You know that when you call CoCreateInstance() for an in-proc server COM automagically calls LoadLibraryEx() to load the COM server DLL if necessary. But how long is the DLL kept loaded? In fact COM calls DllCanUnloadNow() for every loaded COM server DLL periodically. If it returns S_OK COM is allowed to call FreeLibrary().

When is it safe to unload the DLL? Obviously you can't unload it until all the objects implemented by the DLL are destroyed. So here comes "lock count" - an global integer variable counting the number of live objects implemented by the DLL.

When a new COM object is created - CComModule::Lock() is called from its constructor (usually CComObject constructor) and increments the variable, when an object is destroyed - CComModule::Unlock() is called from its destructor and decrements the variable. When CComModule::GetLockCount() returns zero it means that there no live objects and it's safe to unload the DLL.

So the lock count is very similar to the reference count implemented by IUnknown. The reference count is per object, the lock count is per COM in-proc server.

这篇关于CComModule :: Unlock();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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