COM can be accessed in NET through the NET Interop, just like normal managed class. But there are also some differences between the managed objects and COM class. In this article we'll discuss about the life time.

 

Say we have a COM Class Car. So we can create the COM in C# like below:

CarClass car = new CarClass();

 

When we finish using the "car", Marshal has provided below API to release the COM class.

Marshal.Release(car);

So the "car" will be deleted after this call. There will no such gabage collection work here. Marshal will release the COM class internally.

 

There is also another COM Class defined below:

Class Driver

{

     void GetCar(ICar * pCar);

};

 

So in the GetCar() method, it will receive the "pCar" as input parameter. It may implement like below:

void Driver::GetCar(ICar * pCar)

{

     ICar * pCar2 = pCar;

}

 

Here you need to take care of the reference count, which is import in COM. If you don't release the COM interfaces in the CPP code, then even you call Marshal.ReleaseCOMOjbect(), the COM object still could not be released!!!

posted on 2009-11-25 22:32  xiaxi  阅读(216)  评论(0编辑  收藏  举报