Using the .NET Framework Class Library from Visual Basic 6
Around the World with Visual Basic
Asynchronous Method Execution Using Delegates
Building a Progress Bar that Doesn't Progress
Calling All Operators
Create a Graphical Editor Using RichTextBox and GDI+
Creating A Breadcrumb Control
Creating a Five-Star Rating Control
Creating and Managing Secondary Threads
Data Binding Radio Buttons to a List
Deploying Assemblies
Designing With Custom Attributes
Digital Grandma
Doing Async the Easy Way
Extracting Data from .NET Assemblies
Implementing Callbacks with a Multicast Delegate
Naming and Building Assemblies in Visual Basic .NET
Programming Events of the Framework Class Libraries
Programming I/O with Streams in Visual Basic .NET
Reflection in Visual Basic .NET
Remembering User Information in Visual Basic .NET
Advanced Basics: Revisiting Operator Overloading
Scaling Up: The Very Busy Background Compiler
Synchronizing Multiple Windows Forms
Thread Synchronization
Updating the UI from a Secondary Thread
Using Inheritance in the .NET World
Using the ReaderWriterLock Class
Visual Basic: Simplify Common Tasks by Customizing the My Namespace
What's My IP Address?
Windows Forms Controls: Z-order and Copying Collections
Collapse the table of content
Expand the table of content

Using the .NET Framework Class Library from Visual Basic 6

Visual Studio .NET 2003

Scott Swigart
Swigart Consulting LLC.

March 2006

Applies to:
   Microsoft Visual Basic 6
   Microsoft Visual Basic .NET 2003
   Microsoft Visual Basic 2005
   Microsoft Visual Studio .NET 2003
   Microsoft Visual Studio 2005

Summary: In early January of 2002 Microsoft released Microsoft .NET 1.0, which included a massive class library with functionality that wasn't available in Visual Basic 6. This article will show you how you can use this free resource when enhancing your existing Visual Basic 6 or ASP applications. (10 printed pages)

Click here to download the code sample for this article.


What's Useful in the FCL?
Calling into the FCL


Visual Basic has always put a large number of easy-to-use classes at the fingertips of developers. A goal of Visual Basic was to surface the most commonly used functionality of the Win32 API in a way that was very productive and intuitive. Of course, the functionality in the Win32 API varied somewhat from one version of Windows to the next, and not everything was surfaced through Visual Basic 6 consumable COM objects. This left the developer in a position of sometimes having to code to the Win32 API directly, or utilize third party controls to obtain needed functionality for certain scenarios.

In early January of 2002, Microsoft release the Microsoft .NET 1.0 framework, which included a massive class library with functionality that wasn't available in Visual Basic 6. In November of 2005, Microsoft released version 2.0 of the .NET Framework. All versions of the .NET Framework Class Library (FCL) are completely free to develop with and redistribute. This article will show you how you can use this free resource when enhancing your existing Visual Basic 6 or ASP applications.

What's Useful in the FCL?

Version 1.0 of the .NET Framework Class Library provides extensive functionality for the following areas:

  • New data types, such as GUID for working with Global Unique Identifiers, and TimeSpan, for working with a relative times (5 minutes instead of 12:05 AM 1/1/2005).
  • Environment information—OS version, environment variables, command line arguments, machine name, user name, user domain, special folder paths, logical drives.
  • Functionality for creating console (command line) applications.
  • Functionality for writing Windows services.
  • Registry access.
  • Power events.
  • Database access.
  • Numerous types of collections, including sorted lists, dictionaries, and queues.
  • Configuration file management.
  • Operating system interaction—Processes, event logs, performance counters, services, tracing, and debugging.
  • Active Directory access.
  • Advanced graphics capabilities—Drawing, gradients, images, printing.
  • Access to COM+ services such as distributed transactions.
  • File system interaction and IO.
  • Windows Management Interface access.
  • Access to message queuing.
  • Low level network access—DNS lookups, IP Addresses, sockets, HTTP communication.
  • Interoperability with non .NET code (known as unmanaged code).
  • Reflection—Allows late binding, and inspection of types at runtime.
  • Remote object access—Creating instances of remote classes and calling their methods.
  • Serialization—The ability to easily import/export the data of a class as XML.
  • Cryptography—Hashing, symmetric and asymmetric encryption, X509 certificates.
  • Text formats—Conversions for ASCII, UTF-7, UTF-8, Unicode.
  • Regular Expressions—The ability to insure that a string conforms to a certain pattern (the string looks like a phone number, IP address, e-mail address, and so on) The ability to extract sub-strings from a string (for example, extract all the HTML <A> tags).
  • Threading—The ability to have an application do work in the background.
  • Web Services.
  • XML support—Superior functionality compared to MSXML.

Version 2.0 adds the following new functionality:

  • Programmatic access to access control lists (ACLs).
  • Data Protection API (DPAPI).
  • New networking classes (Ping, network connectivity changed notification).
  • Better programmatic support for certificates.
  • FTP classes.
  • Compression and decompression classes.
  • Access to more local computer information.
  • Serial port classes.
  • SMTP classes.
  • New windows forms controls—SoundPlayer, BackgroundWorker.
  • Enhancements to many existing windows forms controls.

This list isn't exhaustive, but it should give you an idea that the FCL is extensive, and contains functionality that wasn't available in Visual Basic 6. Some of this functionality is available directly, and some is accessible through the creation of simple wrappers (literally, only a couple lines of code). In this article, you'll see the functionality that can be used directly. In following articles, you'll learn how to create wrappers to access additional FCL functionality.

Calling into the FCL

Suffice to say, .NET classes and COM objects aren't the same thing. However, the .NET framework was designed so that .NET and COM code can interoperate. You can create a Visual Basic 6 project, and start to use some of the functionality provided by the FCL as follows:

  1. Download and install either version 1.1 or version 2.0 of the .NET Framework. If you have installed Visual Studio .NET 2003 or Visual Studio 2005, or any of the Express products, then the .NET framework is already installed.
  2. Execute Register.bat, which is included in the code download for this article. This registers the .NET framework System.dll so that it can be called as a COM object.
  3. Start Visual Basic 6.
  4. In the New Project dialog, select Standard EXE, and click OK.
  5. Add a CommandButton and Image control to the form.
  6. Set the Stretch property of the Image to true.
  7. Select the Project | References menu command.
  8. Click Browse.
  9. For v1.1 of the .NET Framework, select C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\system.tlb. For v2.0 of the .NET framework, select C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\system.tlb.
  10. Click OK.

This is a type library for one of the .NET assemblies. You can see that once you reference it, "System.dll" appears in your list of references, giving you access to .NET functionality.

Figure 1. Reference added to a .NET DLL

Downloading a File with WebClient.

At this point, for a limited number of .NET classes, you can simply start using them like COM objects.

  1. Create a Click event handler for your CommandButton control.
  2. Enter the following Visual Basic 6 code:
Dim webDownload As System.WebClient
Set webDownload = New System.WebClient
webDownload.downloadFile _
"", _
App.Path & "\latest_westir.jpg"
Set Image1.Picture = LoadPicture(App.Path & "\latest_westir.jpg")

That's it. Just hit F5, and click the button. If you receive a "Can't create object" error, then your System.dll is not registered as a COM object. Double-click on the Register.bat file from the project file download, and the problem will be resolved. Otherwise, you should get something like the following:

Figure 2. Visual Basic 6 application using .NET classes to download an image

In this example, the DownloadFile function of the .NET WebClient class was used to download a recent weather photo. The image is then just loaded into a Visual Basic 6 Image control.

Using the SortedList Class

The FCL also ships with a number of powerful collection classes, which include:

  • ArrayList—An array class that doesn't have a fixed size. You can just keep adding items to it.
  • Hashtable—This class is similar to the Scripting.Dictionary class. You can add items and look them up by key.
  • Queue—This is a first in, first out (FIFO) collection. You push items in, and then read them out at a later time in the same order.
  • Stack—A first-in, last out (FILO) collection. You push items onto the stack, and then pop them off in reverse order.
  • SortedList—Similar to the Hashtable, except when you iterate through the items, they're always sorted by the key.

One that's interesting for Visual Basic 6 use is the SortedList, because Visual Basic 6 doesn't have any built-in sorting capabilities. This makes it easy to load in a list of items, and then output them in a sorted order. For example, the following application loads in a list of files, and then displays them, sorted by name, in a listbox.

Figure 3. Displaying files sorted by name

Building this application is quite simple. First, a reference is added to the Microsoft Scripting Runtime. This provides access to the FileSystemObject, which makes it easy to get a list of the files in a given folder. Then, a reference is added to either C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorlib.tlb or C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorlib.tlb, depending on the version of the framework that you want to use. Mscorlib is part of the Microsoft .NET Framework, and contains the collection classes.

Once these references are added, the following code will get the list of files, sort it, and display it:

Listing 1:

Visual Basic 6 Code to Get and Sort a List of File Names.

' Use the FileSystemObject to get access to the 
' desired folder 
Dim fso As Scripting.FileSystemObject 
Set fso = New Scripting.FileSystemObject 
Dim f As Scripting.Folder 
Set f = fso.GetFolder("c:\windows\system32") 

' Create an instance of a SortedList to store 
' the file names in sorted order 
Dim sa As mscorlib.SortedList 
Set sa = New SortedList 

' Loop through all the files and add them 
' to the SortedList 
Dim fi As Scripting.File 
For Each fi In f.Files 
    sa.Add fi.Name, fi.Size 

' Loop through the list and add the items to 
' the ListBox, sorted by the key (the file 
' name, in this case 
Dim i As Integer 
For i = 0 To sa.Count-1 
    List1.AddItem (sa.getkey(i)) 

This uses the sorting capability of the SortedList to very easily sort a set of items (files in this case), and display the results. Again, you can see that the code can just create an instance of the SortedList, and use it just like any other COM object, even though it's a .NET class.

Just for comparison, here what this same code would look like implemented completely in Visual Basic .NET:

Dim sa As New SortedList 
Dim di As New DirectoryInfo("c:\windows\system32") 
For Each fi As FileInfo In di.GetFiles() 
    sa.Add(fi.Name, fi.Length) 

For Each fileName As String In sa.Keys 

This uses the same SortedList class; however, the .NET Framework includes classes that make it easy to work with the file system. The DirectoryInfo class is designed to let you retrieve information about a directory. Note that at the point at which you create an instance of a DirectoryInfo, you can pass it a string telling it what directory you're interested in. From there, you can call its GetFiles method, which returns information about all the files in that directory. Notice the syntax of the For Each statement. With Visual Basic 6, your For Each statements have to use a variant to loop through items. Here, you can use a specific type (a FileInfo type in this case) to loop through the file information. The next For Each does something similar, iterating through all the keys in the SortedList as strings.

Again, the .NET code is included simply for comparison, but you can see that there are some nice new conveniences built into the Visual Basic .NET language.


In this article, you've taken the first step across the bridge between Visual Basic 6 and .NET. You've seen how you can create the registry entries for .NET classes that let Visual Basic 6 use those classes as though they were COM objects. This opens the door to a small percentage of the FCL, allowing you to use it directly from your Visual Basic 6 code. In the next article in this series, you will learn a technique that will open the door to using the entire FCL by creating simple wrapper classes.

Stay tuned...



About the author

Scott Swigart spends his time consulting, authoring, and speaking about emerging and converging technologies. Scott has worked with a wide range of technologies over his career, beginning with Commodore 64 programming at the age of 12, writing hardware diagnostics for UNIX systems in C++, and building windows desktop, and Web applications. Over the years, Scott has worked with component development, XML technologies, .NET, Web service, and other languages, platforms, and paradigms. With this experience, Scott has seen how technology evolves over time, and is focused on helping organizations get the most out of the technology of today, while preparing for the technology of tomorrow. Scott is also a Microsoft MVP, and co-author of numerous books and articles. Scott can be reached at

© 2015 Microsoft