Monday, March 05, 2007

Using .NET components in an VB6 host

In order to allow correct objects cleanup on process shutdown of a VB6 executable that uses .NET components via CCWs and RCWs you must explicitly shut down .NET runtime in the unload of the project. Otherwise you can observe nasty crahses.

The easiest way to achive this is using mscoree.CorRuntimeHost.
Simply create an instance of this server an call Stop when your project unloads. With Start you can also control, when the runtime is loaded.

You need to reference mscoree.tlb and mscoree.dll needs to be registered via regasm.


Dustin said...

I'm needing to do this very thing... I'm added a .NET usercontrol to a VB 6 ActiveX control and then using it in another application. When I close the application, the VB6 shuts down fine, but the .NET .dlls are still loaded in memory.

Could you give a short example of how to unload the .NET pieces?

Volker von Einem said...

Assemblies that have been loaded into the default application domain or assemblies that have been loaded as application domain-neutral cannot be unloaded without shutting down the entire process.

Please read here:
Unloading Domains and Shutting Down a Process

Cheers Volker