<a href="http://www.micropoll.com/akira/mpview/585320-168921">Click Here for Poll</a><a href="http://www.questionpro.com" title="online surveys">Online Survey</a><BR> | <a href="http://www.micropoll.com" title="Website Polls">Website Polls</a><BR> | <BR><a href="http://www.micropoll.com/akira/MicroPoll?mode=html&id=168921">View MicroPoll</A></div>

Visual Studio 2010!

Read now >

Windows Mobile Development Thoughts

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

 

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
( Page 1 of 4 )

Want the speed and ease-of-use of VB6, but the power of .Net? You don't have to give up either one. Learn how to get the best of both worlds.

Despite all the power and features of Visual Studio.Net, lots of developers are sticking with good old Visual Basic 6 for at least some of their development projects. There's good reason for this. Tens of thousands of developers know VB6 from the inside out, and can create applications quickly and efficiently. The language's ease of interface design and excellent database tools make it a great choice for many kinds of projects. And the terrific new features of .Net, such as the Common Language Runtime and Web services, simply are not needed for many projects. In fact, there is a movement afoot from VB6 developers to try to convince Microsoft to continue supporting VB6, an idea with which I heartily concur.

To read about the efforts to keep VB6 alive, click here.

Even so, the .Net class library has some mighty tempting goodies. Sometimes, while working on a VB6 project, I have found myself wanting to insert a class — only to realize it is a .Net class that I am thinking of. More than once, I have pondered whether to start a project in VB6 with its speed and ease of development, or in .Net with its undeniably powerful class library. Imagine my delight when I discovered that I did not have to chose one or the other, that I could use the .Net class library from a VB6 program. Not the whole library, and not every class, but enough to make a significant difference. This library is completely free to use and distribute; your end users do not need Visual Studio.Net, nor do you for that matter (although the documentation is useful).

What does the .Net class library offer? If you have worked much with .Net, you already have some idea, but here are a few of the capabilities that may be of interest when you are working in VB6:

  • Low level network access including DNS lookups, IP Addresses, sockets, and HTTP communication.
  • Serialization, the ability to easily import/export the data of a class as XML.
  • Powerful support for regular expressions.
  • Advanced graphics capabilities for drawing, gradients, bitmap images, metafiles, and printing.
  • Active directory access.
  • Powerful collections of various kinds including sorted lists, dictionaries, and queues.
  • Superior XML support as compared with MSXML.

This is just a partial list, but I hope it whets your appetite!

To use .Net classes in VB6, you must add a reference to the type library for the assembly that contains the class. You do this as follows:

  1. Select Project | References.
  2. In the References dialog box click the Browse button.
  3. Navigate to C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322.
  4. Select the appropriate .tlb file and click Open.
  5. Click OK.

How do you know which assembly to select in Step 4? You need to look up the class of interest in the .Net class library documentation. In the Requirements section, it lists the assembly that contains the class. For example, the SortedList class is in Mscorlib, which means you would select Mscorlib.tlb in step 4 above.

You also have to register the assembly as a COM object. Unlike standard DLLs, which are registered with the REGSVR32 command line application, your must use the REGASM application that is installed with the .Net runtime. Here's how:

  1. Select Run from the Windows Start menu
  2. Enter cmd in the box and press Enter. Windows opens a command prompt.
  3. Change to the folder C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322.
  4. Type "regasm system.dll" and press Enter.

After doing this, you will be able to use some .Net classes as if they were COM objects. You must include the namespace qualifiers when declaring them, because VB6 does not give you the capability to import namespaces.

To illustrate, let's use the WebClient class from .Net. This handy class provides the capability to download from and upload to a resource identified by a URL. By using the WebClient class in your program, you avoid the various problems that are known to plague the Internet Transfer control (the usual way to accomplish this in VB6).

The DownloadFile method in WebClient easily downloads data from a URL and saves it in a local file. This class is in the System assembly, so follow the steps above to add System.tlb to your project references. Be sure to use REGASM to register the assembly as well; you need do this only once.

Here's some code that shows how to use this class in VB6 to download an image file and then display it in an Image control:

Dim wc As System.WebClient
Set wc = New System.WebClient
wc.downloadFile "http://www.somewhere.com/somefile.jpg", _
    App.Path & "\filename.jpg"
Set Image1.Picture = LoadPicture(App.Path &
"\filename.jpg")

When you run the code, the specified image displays in the Image control. Yes, it's really that simple!

Some .Net class methods are beyond the reach of VB6 programmers using this straightforward approach. This is because most .Net classes do not appear as COM objects, and it is COM that VB6 understands. Also, many .Net class methods use data types that are not available in VB6, either as arguments or as return values (or both, of course). Then you need to create a wrapper for the class and/or method.



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