Recently, a customer asked if it would be possible to add some specific functionality to a program written by us. The program in question displayed the forces and streamed in real-time, acting on the various axles of a racing car. The version at the time simply had a
PictureBox control in VB6 with text boxes, etc. positioned around the picture to display the values. The customer was requesting that this whole "picture" be scaled depending on the size of the window, so that on large screens it would be easier to read.
Put quite simply, the language in which the application was written, VB6, does not provide very good support for graphics. This is especially the case for resizing pictures proportionally, which was the one major client requirement.
The time frame for implementing the solution was limited, so although not completely out of the question, a complete re-write of the software in VB.NET was not really feasible. One possibility which presented itself was to see if a control written using .NET could be used in a VB6 application using COM Interop. As it turned out, this is quite easy, but the leg-work involved behind it revealed quite a few dead-ends. So, it is the aim of this article to eliminate those dead-ends for other people wishing to accomplish the same thing.
Stage 1: Creating the Control
This took the longest of the three stages in our case, simply because of the nature of the control and calculating the positions of where text, etc. was meant to be positioned. I will not delve into the details of our control, but only the steps that are required for VB6 Interop.
Create a new Windows Control Library project from within Visual Studio.
In both the Debug and Release modes of the Property Pages, set the "Register for COM Interop" checkbox.
- Inside the AssemblyInfo.cs file, change the assembly wide attribute
true. If it's not already in the configuration file, add it.
That is all that is required to make the project visible to VB6 projects.
A quick word about these: properties are exposed to VB6 so, like .NET controls, if you want to expose a value, you must wrap it in a
Property expression. You cannot just have it visible as a field.
Stage 2: Registering the Assembly
The library must be registered on the client machine before use by VB6. If it is not registered on the development machine, then it will not be visible in the References dialog of VB6. If it's not registered on the installation machine, then it is a similar problem to if you have not registered a classic DLL or ActiveX control. The "Register for COM Interop" checkbox in VS2005 performs this registration automatically while the environment is running, but un-registers it when VS is closed.
To register the assembly, you must use the .NET equivalent of regsvr32, regasm. This is located in the framework directory, usually "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727". To register it, open a command prompt and run the following command, assuming that the framework directory and the assemblies directory are in the environment's current path.
Stage 3: Adding to VB6 Projects
The secret here is the
VBControlExtender object, which allows a .NET control to be hosted on a VB6 form. However, the first stage is to add a reference to the assembly you just registered. This is accomplished by checking the box in the Project | References menu.
Once that has been accomplished, then the following code can be added to the form's code in the project:
Dim car As VBControlExtender
Private Sub Form_Load()
Set car = Controls.Add("CarControl.Car", "car", Me)
The other code included in the demo's source file simply resizes the control based on the form size and sets random values to the properties of the control.
Private Sub Form_Resize()
car.Left = 100
car.Width = Me.Width - 300
car.Top = 100
car.Height = Me.Height - 700
car.Visible = True
Private Sub timer_Timer()
car.object.FrontL = Rnd()
car.object.FrontR = Rnd()
car.object.RearL = Rnd()
car.object.RearR = Rnd()
You'll notice that I have had to refer to the properties of the .NET control through
car.object. This provides late-binding functionality for VB6. All COM-Visible methods in the .NET control are accessible through this object. You just have to know what you're typing because it is late-bound.
Hopefully, my ability (or lack thereof) as an article writer hasn't masked the important bits of the article so much that it is unusable. I personally find that the code speaks for itself and have included all .NET & VB6 code in the attached ZIP files, so do take a moment to browse through them.
- 31st May, 2007 -- Original version posted
Ed is a student who due to a form of cancer (now clear) took a year out before going to Imperial College, London to study Electronic Engineering.
His interests include shooting (clay-pigeon (shotgun), air-rifle and rifle), playing with his three labradors (Sandy, Rosie and Tundra), programming (most experienced in C# and C, although those are not the only ones), walking (has completed Gold Duke of Edinburgh's Award), playing games and reading.
He lives in two places on a 57 acre farm in West Waleswith the rest of the family during the holidays; and Greater London during term time.
1st Year MEng Electronics Engineering (Imperial College, London)