Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Pass an Array From .NET Code to VB6 Code?

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{
		string[] GetArray();

	public class Test : ITest{
		string[] ITest.GetArray(){
			string[] stringArray = {"red", "yellow", "blue"};
			return stringArray;

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 or check the option in the Build Project settings to "Register for COM Interop". 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.

VB Test Client
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Interop\TestServer_2\]

Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.