Integrating WinForms with VB6
I wanted to add a dialog to an existing VB6 application, but I wanted to use some of the features of .NET (anchoring, docking, etc...). With COM-interop, I thought it would be pretty easy. For a test, I whipped up a super-quick WinForms app, but changed the output type from "Windows Application" to a "Class Library". I ran REGASM on the DLL to expose it to COM then placed it in the GAC. A few quick lines of VB6 code instantiated and ran the form:
Set winForm = CreateObject("Sample.Form1") winForm.Text = "VB6 Caption Setting" winForm.ShowDialog
Success! Worked like a charm. Before declaring victory, I wanted to try a few things. I removed it from the GAC, unregistered it and made my changes in VS.NET. Re-compiled, re-registered and re-placed it in the GAC. Ran the VB6 app and the old version of the form showed up! Hmmmm... I re-checked the DLL to make sure I had re-compiled it -- I had. I tried removing it from the GAC, unregistering and re-registering and re-adding it to the GAC. The VB6 still ran the original version. After playing around with this process for a while, I tried shutting down the VB6 IDE and re-starting it. When I ran the VB6 sample code, the correct version of the form popped up.
I haven't dug into the details of what's going on, but my guess is that the AppDomain that started up the first form was loaded by the VB6 IDE. Even though the VB6 application I was developing shut down, the IDE didn't so the AppDomain remained loaded -- along with my Form. When I tried to create the object the second time, the type was already loaded in the AppDomain from the first time I ran it. This means I saw the old version of the form.
I'm going to see if I can force the AppDomain to unload when the form closes. This, of course, won't be necessary in production, but will help me out when prototyping.