How to Access the .NET Component From VB6

In this article you will be learn how to how to access a .NET component from Visual Basic 6.

Visual Basic 6

Visual Basic is a third-generation event-driven programming language and Integrated Development Environment (IDE) from Microsoft for its COM programming model. Programming in VB is a combination of visually arranging components or controls on a form, specifying attributes and actions for those components and writing additional lines of code for more functionality.

It's mainly for thick client applications.

The application is targeted towards Windows registries, not assemblies.

.Net

A Microsoft operating system platform that incorporates applications, a suite of tools and services and a change in the infrastructure of the company's Web strategy. The .NET Framework supports building and running of the next gen of applications and XML Web services.

It will support both thick and thin client applications.

The applications are targeted towards assembly.

Use .Net components to VB6 applications

.Net COM enables component

Build the component as a COM visible one.





Code part

  1. [ComVisible(true),  
  2. GuidAttribute("137AD71F-4657-4362-B9E4-C6D734F1F530")]  
  3. [InterfaceType(ComInterfaceType.InterfaceIsDual)]  
  4. public interface IGetMyString  
  5. {  
  6. string GetMyString();  
  7. }  
  8. [ComVisible(true),  
  9. GuidAttribute("89BB4535-5A89-43a0-89C5-19A4697E5C5C")] [ClassInterface(ClassInterfaceType.None)]  
  10. public class Class1 : IGetMyString  
  11. {  
  12. string GetMyString()  
  13. {  
  14. }  
  15. }  
Ensure that signing is done.



Build

Now build the component.

In the Bin folder you will get a .dll, a .tlb and a .pdb file.

If suppose the tlb is not available then use the following command line in your Visual Studio command prompt.
  1. tlbexp myTest.dll /out: myTest.tlb  
Incorporate tlb file into VB6 environment

Run with the default command line. Regasm does the following two main things to register an assembly: 
  • It creates COM registration entries that register the Class Guids. These are the entries in HKCR\CLSID\{Your Guid} that allow COM client programs to find your classes and your assembly, either by ProgId or by your Class Guid.
  • It creates type library information from the assembly's metadata and registers that type library information. These registry entries are in HKCR\Interface\{Your interface Guid}.

Refer the .tlb file in the VB6 developer tool as the reference.

Create the instance in the VB6 file. Now you can access the features.