COM interoperability issue

I am using COM interoperability in my C # application.

I have this line of code:

IMyInterface objData = MyCOMClass.GetData();

My question is:

Do I need to allocate resources to objData using? System.Runtime.InteropServices.Marshal.ReleaseComObject (objData);

Thanks for reading.

+3
source share
1 answer

Yes, unless you wait until the GC clears it.

It is worth noting that the COM interaction creates one COM link to the interface.

IMyInterface x = MyCOMClass.GetData();
IMyOtherInterface y = (IMyOtherInterface)x;
IMyInterface z = x;

Marshal.ReleaseComObject(x);
Marshal.ReleaseComObject(y);

or Marshal.FinalReleaseComObject (x); // If you know that no one is using it

+2
source

Source: https://habr.com/ru/post/1765074/


All Articles