Access Requirements

Every .NET component that is going to be accessed from Visual Basic 6.0 must be registered for interoperability in the system. Registration takes place at a global level and will make the component available to all Visual Basic 6.0 applications. An assembly is the primary building block of a .NET application and it can contain different classes that are available to COM clients. Registering an assembly will make the classes in a registered assembly available to all Visual Basic 6.0 applications on the system. For more information on native dynamic link libraries and assemblies see Chapter 4, "Common Application Types."

The process to register a .NET assembly and make it COM-callable involves creating a type library (.tlb) and including the corresponding entries in the system registry. Fortunately, this is easily accomplished from within the Visual Studio .NET IDE by setting the appropriate build configuration option.

► To register a component for COM interop

1. Open the Visual Basic .NET project that contains the classes you want to make COM-callable.

2. From the Project menu, choose Properties.

3. Under the Configuration Properties folder, click Build.

4. Select the Register for COM Interop check box, and then click OK.

5. On the File menu, click Save to save your project with the configuration changes.

6. On the Build menu, choose Build Solution.

Following the steps of this procedure will create the Visual Basic .NET DLL and register it for COM interoperability. The assembly's AssemblyName property value will be the name with which it is registered under COM. This property can be modified by performing the following procedure.

► To change they AssemblyName property of an assembly

1. Right-click the project name in Solution Explorer, and then click Properties.

2. In the Common Properties folder, click General.

3. In the Assembly name text box, specify the name for the assembly.

4. Click OK to save your changes.

An assembly can also be registered at a Windows command line by using the Assembly Registration tool (Regasm.exe) distributed with the .NET Framework. This would typically only be necessary during deployment, because the steps outlined above can be used to register an assembly during the build. The command for registering an assembly with the Regasm.exe utility is as follows:

regasm Assembly.dll /tlb: Assembly.tlb

This command will register an assembly and generate a type library so that it appears as a regular COM object. Items in italics should be replaced with the appropriate names for your project. The /tlb option is used to specify that a type library file should be generated.

Note: Be careful of naming conflicts between assemblies. Be sure to unregister any older versions of the same assembly before registering the new one.

Should it be necessary to unregister the assembly, this can be achieved by using the following regasm command:

regasm /u Assembly.dll

For the Visual Basic 6.0 application to find the .NET assembly at run time, the assembly must be in either the global assembly cache, or in the application's executable folder. If you wish to install the assembly to another location, and still have it globally accessible to Visual Basic 6.0 applications, you can use the /codebase option with the Regasm.exe utility. This option allows the user to specify a file path that will be used by the common language runtime to locate the corresponding assembly. However, the details of the /codebase option are beyond the scope of this guide, and it is recommended that COM-callable assemblies be placed in the global assembly cache when applications are deployed.

0 0

Post a comment