LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

COM problem with mixed mode .NET dll

Hi

I am creating a mixed mode .NET environment based DLL for usage within LabView. The DLL also includes
unmanaged code and usage of an external hardware device via COM interface (which is instanciated within
unamanged code).
My problem is that if I use the DLL within LabView the resources allocated via the COM interface will not
be released (interaction with the COM interface works). Using the DLL in a standalone .NET test
application causes no problems.

Code in DLL:

-----------------------------------------------
called after creating dll instance (after constructor node), managed code

CoInitialize(0);
__crt_dll_initialize();
-----------------------------------------------
creating com interface, unmanaged code

CoCreateInstance(..., NULL, CLSCTX_INPROC_SERVER, ..., ...) ;

-----------------------------------------------
called before releasing dll instance  (close reference), managed code

__crt_dll_terminate();
CoUninitialize();
-----------------------------------------------

How can I solve this problem??


best regards
Christian

0 Kudos
Message 1 of 7
(3,425 Views)
What resources are you talking about - ones in the COM instance created in the middle step? If you are not using the .NET COM Interop, then those resources should be released when you release the final refcount on the COM object. If they are relying on the DLL shutdown, then they are in dangerous territory - it isn't guaranteed to be executed.
 
But in this specific case, it might be the fact that your DLL isn't unloaded when the VI stops running. The LV process is still running. Unlike Visual Studio, we run the VI inside the LV process. VS spawns off another process to run the debugged application.
 
You might try building your application into a EXE from the App Builder and see if that works...
0 Kudos
Message 2 of 7
(3,422 Views)
Also, does the COM interface have a type library? If so, you might find it a lot easier to let .NET handle the COM interop - it is very good at it and allows you .NET code to be much simplier.
0 Kudos
Message 3 of 7
(3,418 Views)

Hi, i have got the same problem. I have als o an mixed mode dll.  With imported namespaces from an third party provider and dlls with hardware i/o communication. When i test it in the VS 2003 environment it works fine. I can call all functions as often i wants

But if I integrate my Com DLL in LV after a view times of execution i get an error of "Object already exists". I tried it to test it in an LV builded .exe file. But same result. In the my dll. I destroyed all dependencies. ( I hope so). Is it possible that the close reference VI doesn´t destroy the object reference by  finalizing the . net object?

I need urgently help in this purposes because i have to obey an dead-line.

0 Kudos
Message 4 of 7
(3,401 Views)
The COM instance (PCI driver of an external hardware) is in an unmanaged lib, which can not be changed to a managed lib, so I can not use the .NET COM Interop.

I built an EXE with the App Builder but I got the same problem.
I also built an unmanaged dll using this lib (including the COM interface). If I use the unmanaged dll in labView the COM interface is unloaded after termination.

If I use the managed dll in my Vi, I first call a function which destroys the instance of the unmanaged lib. Afterwards I destroy the .NetClass with the "Close Reference"  function.

I hope you can help me to fix this problem.
Thanks
Christian
0 Kudos
Message 5 of 7
(3,396 Views)
darastad,
 
I don't understand how you are having the same problem. A mixed mode DLL is simply one that has both native and .NET code inside it - not necessary COM related. You call it a COM DLL and then talk about .NET objects so I am afraid I am a little confused.
 
However, when you close a .NET refnum via the Close Reference, we release our reference to the object. The .NET garbage collector won't necessarily collect the object at that time - only when all other references are released. Also, if you object must be disposed (calling the Dispose() method), then either you must do that before closing the reference or define a Finalize method on the object and wait for the GC's Finalizer thread to do it - that is standard .NET, not something special in LabVIEW.
 
If it works sometimes and then you get an "Object already exists", it sounds like the Dispose is not being called manually and the object is waiting for the Finalizer to do it. In that case I would recommend trying to call it right before you call the Close Reference.
0 Kudos
Message 6 of 7
(3,386 Views)
jankee,
 
Sorry to be diffcult but the terms you are using don't add up for me so I still don't understand what is happening.
 
1. An unmanaged library doesn't need to be changed to a managed library to use COM interop. If you convert it to a managed library there is no need for COM interop. All you need is the Type Library, which is a COM concept. If you have that, then COM interop handles it all. If you don't have a Type Library, then you would have to define the COM Interop layer yourself - not impossible but the C++ route may be easier.
 
2. You are saying that if you wrap the COM interface with a standard C DLL and call that from within LV then the COM object is cleaned up correctly?
 
3. As with darastad, it may be that the resources are not being disposed. Is there a Dispose() method defined on the .NET object? If so, make sure you call that before you uninitialize the library.
 
I am not sure what is happening, especially if you create a EXE and that doesn't release the resource on shutdown. Is there anyway you can strip the code down to a simple init/shutdown and recreate the problem? Then post that code up so I can take a look at it?
0 Kudos
Message 7 of 7
(3,383 Views)