COM - NET integration

M

Michaelov, Itzik

Hello
I created very simple DLL in C#.
I changed the above DLL COMVisible attribute to true ([assembly:
ComVisible(true)])
So a TLB file created.
Now i am using this DLL functionality from VB6.
All works fine.
The problem is when i set object to nothing in VB6, the .NET object
Finalize(~) method doesn't executed.
In this case i can't release public COM members of my .NET Classes.
I can't explicitly call to .Dispose from VB6.
Do you have any idea how resolve this problem ?

Thanks
 
A

Alberto Poblacion

Michaelov said:
I created very simple DLL in C#.
I changed the above DLL COMVisible attribute to true ([assembly:
ComVisible(true)])
So a TLB file created.
Now i am using this DLL functionality from VB6.
All works fine.
The problem is when i set object to nothing in VB6, the .NET object
Finalize(~) method doesn't executed.
In this case i can't release public COM members of my .NET Classes.
I can't explicitly call to .Dispose from VB6.
Do you have any idea how resolve this problem ?


What I think is happening is that setting the reference to Nothing in
VB6 merely releases the reference to the CCW (COM-Callable Wrapper). The CCW
in turn releases the reference to the managed object. However, since this is
happening on the managed side, releasing the latter does not execute the
Finalizer; it only makes the object available to the Garbage Collector,
which will later run the Finalizer after a memory collection adds the
finalizer into the finalization queue.

If you need the finalization code to run immediately, you shoud be using
the "standard" managed-code technique of adding a Dispose method and
explicitly calling it. You say "I can't explicitly call to .Dispose from
VB6." Why is that? If your C# code contains a "public void Dispose()", there
is no reason why such a method would not be callable from your VB6 client
over COM-Interop.
 
M

Michaelov, Itzik

Because client uses interface and i can't add new function
The purpose that the clients code will not be changed

Thanks

Alberto Poblacion said:
Michaelov said:
I created very simple DLL in C#.
I changed the above DLL COMVisible attribute to true ([assembly:
ComVisible(true)])
So a TLB file created.
Now i am using this DLL functionality from VB6.
All works fine.
The problem is when i set object to nothing in VB6, the .NET object
Finalize(~) method doesn't executed.
In this case i can't release public COM members of my .NET Classes.
I can't explicitly call to .Dispose from VB6.
Do you have any idea how resolve this problem ?


What I think is happening is that setting the reference to Nothing in
VB6 merely releases the reference to the CCW (COM-Callable Wrapper). The
CCW in turn releases the reference to the managed object. However, since
this is happening on the managed side, releasing the latter does not
execute the Finalizer; it only makes the object available to the Garbage
Collector, which will later run the Finalizer after a memory collection
adds the finalizer into the finalization queue.

If you need the finalization code to run immediately, you shoud be
using the "standard" managed-code technique of adding a Dispose method and
explicitly calling it. You say "I can't explicitly call to .Dispose from
VB6." Why is that? If your C# code contains a "public void Dispose()",
there is no reason why such a method would not be callable from your VB6
client over COM-Interop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top