Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Calling .Net Classes from Visual Basic 6
By Peter Aitken

Rate This Article: Add This Article To:

Calling .Net Classes from Visual Basic 6 - ' Using Your Wrapped Class '
( Page 4 of 4 )

This key pair file was associated with the Visual Basic source this way:

<Assembly:AssemblyKeyFileAttribute("sgkey.snk")>

With the strong name in hand, you are ready to compile your source code. Here's the required command, assuming you saved the source code as WebClientVB6.vb.

vbc WebClientVB6.vb /r:System.dll /target:library /out:MyWrappers.dll 

The result of the compilation is a file named MyWrappers.dll. I used this generic name because you could compile multiple wrappers into a single assembly, although in this case only one wrapper is compiled.

The next two steps are to add the newly created assembly to the assembly cache and to register it. The first step is accomplished using the GACUTIL utility, another part of the SDK:

gacutil /i mywrappers.dll

The registration step is similar to what you did before when a wrapper was not needed:

regasm mywrappers.dll /tlb:mywrappers.tlb

The additional argument instructs the regasm utility to create the type library for the assembly as well as registering it.

Now you are ready to use the wrapped .Net class in your VB6 program.

Using Your Wrapped Class

Now we return to VB6 and see how the wrapped class method can be used. First, you add a reference to the assembly you just created. Open the References dialog box and you'll find the assembly name listed — MyWrappers, in this case. Check it and close the dialog box.

Now it's a simple matter of creating an instance of the wrapper using the assembly name and the wrapper name. Here's the code:

Dim wcr As MyWrappers.WebClientVB6
Set wcr = New MyWrappers.WebClientVB6

Then just call the wrapped method as shown here. This example downloads the home page of my web site and displays the HTML in a text box:

Text1.Text =
wcr.GetFileAsString("http://www.pgacon.com")

Deploying Your Application

Getting your hybrid VB6/Net application to run on your development box is one thing. What about deploying it to your end-users? There is, as you might expect, a bit of added complexity in this process as well. Fortunately it is well explained in an article on the MSDN Web site.

The ability to call .Net classes from a VB6 program is a very useful skill for a programmer. There is some extra work involved, but overall you are likely to save a lot of time and effort by combining the strengths of the two development tools in one program.

To learn more about the differences in versions between BASIC versions, click here.

Peter Aitken posts a Visual Basic tip every week in the DevSource discussion forum. Ask him to help you figure out your problems!



 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By Peter Aitken