开发者

How do I get FinalRelease to be called when using GlobalInterfaceTable?

开发者 https://www.devze.com 2022-12-15 21:54 出处:网络
I am using Visual Studio 2008 and I have created an ATL Project.I have aded a ATL Simple Object to the project.I want to add my object to the Global Interface 开发者_运维问答Table when the object is c

I am using Visual Studio 2008 and I have created an ATL Project. I have aded a ATL Simple Object to the project. I want to add my object to the Global Interface 开发者_运维问答Table when the object is created, so I have the following code in my FinalConstruct call:

HRESULT res;
res = CoCreateInstance(GLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, IID_IGlobalInterfaceTable, (void **)mGit);
// res here is OK, mGit is not null
if(mGit != NULL)
{
   CComQIPtr<ISimObj> com_ptr = this;
   result = mGit->RegisterInterfaceInGlobal(com_ptr, __uuidof(ISimObj), &mGitCookie);
   // result is OK and mGitCookie is not null
}

To clean up, I added the following to the FinalRelease call:

if((mGit != NULL) && (mGitCookie != NULL))
{
   result = mGit->RevokeInterfaceFromGlobal(mGitCookie);
}

if(mGit != NULL)
{
   mGit->Release();
}

Then I created a C# Project and added the DLL as a reference and created a new Instance of SimObjClass. I debug the project from the VC++ project and see that FinalConstruct is called and everything is happy. Then I want to delete the COM object so I call:

int res = Marshal.ReleaseComObject(mSimObjClass); // res is 0

from my C# application. But while debugging the application, I noticed that FinalRelease is not called. Reading further, it appears that RegisterInterfaceInGlobal will increment the reference count.

My question is, what is the correct way to ensure that FinalRelease is called when using GIT? I dont really want to create a function that will do it manually, since that seems to defeat the purpose of FinalRelease. Any ideas?

EDIT: I should also note, that if I remove the Register call, FinalRelease does get called when I call ReleaseComObject.


Yes, calling RRIG will increment the reference count. You can't use FinalRelease, you'll have to revoke it when the reference count drops down to 1. I don't see an obvious way to do this in ATL, other than deriving from CComObjectRootBase so you can write your own InternalRelease().

You might be better off making this explicit with, say, a Publish and Revoke method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号