CLR_cn

//Code

IntPtr comObjPtr;
SomeWinapiFunction(out comObjPtr); // Get a com object ptr.
IUnknown obj = (IUnknown)Marshal.GetTypedObjectForIUnknown;(comObjPtr, typeof(IUnknown));

//use obj.

Marshal.ReleaseComObject(obj);


My question is , do I need to call "Marshal.Release(comObjPtr)" I think "Marshal.ReleaseComObject(obj)" can release the COM object, so I do not have to call Marshal.Release() to release the pointer. Right




Re: Visual C# General A question about C# use COM object.

H. Tony

I think you need to release this pointer as well, as far as I remember.





Re: Visual C# General A question about C# use COM object.

CLR_cn

Some one else






Re: Visual C# General A question about C# use COM object.

Mattias Sjogren

You should call Release on the interface pointer (assuming SomeWinapiFunction has called AddRef on it). When the RCW is created by GetTypedObjectForIUnknown is called it will AddRef again and that will be balanced by ReleaseComObject.