Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, October 09, 2006

Using .NET DLL in VB 6.0

1.    Create an strong name using

                sn -k Testkey.snk
                Keep this strong name in “AssemblyInfo.cs” file
[assembly: AssemblyKeyFile(@"C:\Documents and         Settings\avempati\Testkey.snk")]

2.    You can install an assembly in the global assembly cache using gacutil.exe utility. Assemblies can be uninstalled using the /u option.

gacutil /i TestServer.dll

3.. Most unmanaged application development tools require a type library before you can make references to external types.

A type library can be generated from an assembly using the tlbexp.exe, which produces a .tlb file that can then be referenced from your unmanaged development tool.

tlbexp TestServer.dll 
or else 

If the assembly is to be used privately by a single application, it can be copied directly to the application directory

 (rather than installing the assembly in the GAC). Private assemblies do not require shared names, but they must be registered for use from COM.

You can register an assembly using regasm.exe utility. Assemblies can be uninstalled using the /u option.

               regasm TestServer.dll 


This example demonstrates how to use a .NET object from a Visual Basic 6.0 application. The same technique can be used to create the object from any COM application including those built with Visual C++ 6.0, VBScript or JScript.

In this example, a simple class called Test implements ITest interface. This interface has a single method that returns an array of strings and can be easily used from COM. The array can be enumerated as any other Visual Basic array. It is possible to avoid explicit definition of interface and use class interface, which contains all of the members of the class and all the members of its base classes. This technique is available by applying the DefaultInterface.AutoDual attribute, but is strongly discouraged because it limits your ability to version your classes.

Namespace TestServer
               Public Interface ITest
                               Function GetArray() As String()
               End Interface
               Public Class Test : Implements ITest
                               Function GetArray() As String() Implements ITest.GetArray
                                              Dim stringArray() As String = {"one", "two", "three"}
                                              Return stringArray
                               End Function
               End Class

End Namespace

After compiling the managed code to create an assembly, the assembly must be installed in the global assembly cache (GAC) and registered for use from COM. Use gacutil.exe to install the assembly in the GAC and add the registry entries needed to enable the types within the assembly to be created from COM.

gacutil /i TestServer.dll

In order to use the type from Visual Basic 6.0, it helps to have a type library that describes the types contained in the assembly. Use tlbexp.exe to produce a type library from any managed assembly. Once a type library is available, it can be added the project through the Project/Reference dialog in Visual Basic 6.0.

tlbexp TestServer.dll

Once the reference to the type library is added to the project, the types defined in the assembly can be referenced directly with the Visual Basic 6.0 code.

Dim dotNETServer As TestServer.ITest
Set dotNETServer = New TestServer.Test
Dim str As Variant
Debug.Print ".NET server returned: "
For Each str In dotNETServer.GetArray
      Debug.Print str

In order to run the application, the assembly is typically installed in the global assembly cache as described above.

During development it is easier to simply copy the assembly to the application directory.

 Assemblies can only be located if they reside in the application directory or in the Global Assembly Cache.

Note: If you are trying to run the application from within the Visual Basic 6.0 development environment,

 the assembly must be in the same directory as vb6.exe because vb6.exe is really the process hosting the assembly.


Post a Comment