VB and VBA Users Source Code: Using your .NET components from within VB 6 (using TlbExp and Regasm)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Using your .NET components from within VB 6 (using TlbExp and Regasm)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, April 18, 2005
Hits:
1852
Category:
Visual Basic General
Article:
TlbExp.exe and Regasm.exe tools aid us in exporting assembly information to a type library so that the components can be called from unmanaged code. Below is a step by step example showing how to use a demo VB.NET class from within VB 6: 1) Open a new dotnet project (Class Library). 2) Add a class to the project called MyClass as below. public class MyClass { public MyClass() { } public int Add (int i , int j) { return i +j; } public int Mul (int i , int j) { return i *j; } } 3) Strong name your assembly, Compile the project and create the assembly. 4) Go to the visual studio command prompt and type tlbexp /? to explore all the options. You should see the following: /out:FileName File name of type library to be produced /nologo Prevents TlbExp from displaying logo /silent Prevents TlbExp from displaying success message /verbose Displays extra information /names:FileName A file in which each line specifies the captialization of a name in the type library. /? or /help Display this usage message 5) To create a typelibrary from the dll you need to type the following command: "tlbexp"
.dll (e.g. tlbexp myDll.dll) 6) If it the export was successful you should see a message similiar to the one below: Assembly exported to C:\Test\myDll.tlb Note, tou can also use the /verbose option to check out the details of the classes that were exported. 7) Although our typelibrary is created the assembly is not yet registered. You can register the assembly using "regasm" tool. 8) Go to visual studio command prompt and type regasm /? to explore all the options. You should see the following: /unregister Unregister types /tlb[:FileName] Export the assembly to the specified type library and register it /regfile[:FileName] Generate a reg file with the specified name instead of registering the types. This option cannot be used with the /u or /tlb options /codebase Set the code base in the registry /registered Only refer to already registered type libraries /nologo Prevents RegAsm from displaying logo /silent Silent mode. Prevents displaying of success messages /verbose Displays extra information /? or /help Display this usage message 9) To register the assembly use the following command: regasm myDll.dll (regasm
.dll) 10) You can also create a .reg file containing all the registry entries using: regasm myDll.dll /regfile:myDll.dll.reg Note, you can also create a typelibrary from the assembly using regasm tool with the /tlb option. 11) To use this component from VB 6.0 put the assembly in GAC. You can do this by either using gacutil or just drag and drop the assembly in the following folder: C:\winnt\assembly\ 12) You can now use this assembly from VB 6.0 . 13) Open a new VB standard project. Go to references and select the .tlb file that we generated from the list. 14) You should now be able to use your .Net assembly from VB 6.0. Sample code of using your .NET assembly from VB Dim myDll As myDll.MyClass Set myDll = New myDll.MyClass Label1.Caption = myDll.Add(1, 2)
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder