Making a COM Component Visible to the CLR

Most organizations will not have the time or budget to rewrite their existing applications in .NET, no matter how desirable the features of the new platform are. Fortunately, it is easy to use existing COM DLLs with .NET applications. Working with COM DLLs requires that the information about the classes contained in the DLL be described in a way that that is consistent with the .NET Framework.

In the COM world, the file that contains information describing the classes in a component is called a type library. This type library information is embedded inside the DLL file or can exist in a separate file with a .tlb extension. To use the COM component from your .NET application, you must take this COM type library information and create a .NET interop assembly. There are several ways to do this—by using Visual Studio .NET, by using a command-line utility (the Type Library Importer utility, tlbimp.exe, that is supplied with the .NET Framework), by using .NET Framework classes from the System.Runtime.InteropServices namespace, or by creating custom wrapper classes. These last two options are outside the scope of this book and the exam objectives.

Remember that all COM DLLs that you want to reference from a .NET application must be registered on the computer that the application will run on (unlike .NET DLLs, which do not require registration). Use the command-line utility regsvr32.exe, or a Windows setup program to register the DLL. Figure 2.2 shows what this looks like.

click to expand
Figure 2.2: The Regsvr32 utility

If you are using Visual Studio .NET, you simply set a reference to the type library file or the DLL; Visual Studio .NET does all the work. Figure 2.3 shows the Add Reference dialog box. After compiling your application, you will see a file in the \bin subdirectory for your project called Interop.COMDLLname.dll. This file contains all the information that the CLR needs to work with the COM component.

click to expand
Figure 2.3: The Add Reference dialog box

Note 

Type library files (*.tlb files) can be generated from Visual Basic 6 by selecting the Remote Server Files check box on the Component tab of the Project Properties dialog box.

After you have referenced the DLL, you can instantiate objects from the class and use their methods, just as you would with any other component, as shown in Listing 2.3. This listing shows how to use a class called CMath, which has a method called Add.

Listing 2.3: Instantiating an Object from a COM Class

start example
Dim Result As Short Dim objAdd As CMath = New CMath() Result = objAdd.Add(CType(txtNum1.Text, Short), CType(txtNum2.Text, Short))
end example

In Exercise 2.4, you are going to add a COM DLL to a Visual Studio .NET project. You can use the COM DLL, named COMCalc.dll, that is provided on the CD included with this book.

Exercise 2.4: Referencing a COM Component in Visual Studio .NET

start example
  1. Create a new Visual Studio .NET project. Use the Windows Application template and name your project COMTester.

  2. Copy the file \path\COMCalc.dll from the CD into your project directory.

  3. To open a command window, choose Start Ø Programs Ø Visual Studio .NET Ø Visual Studio .NET Tools Ø Visual Studio .NET Command Prompt.

  4. Navigate to your project directory.

  5. Type regsvr32 COMCalc.dll at the command prompt. You should see a message box indicating that the component was registered successfully.

  6. Close the command window and return to your Visual Studio .NET project.

  7. In the Solution Explorer, right-click the project name and choose Add Reference from the pop-up menu.

  8. Click the COM tab and then scroll down the list until you see the entry COMCalc for Interop Demo. Verify that this DLL is located in your project directory.

  9. Click this entry. Then click the Select button and the OK button.

  10. Open the Object Browser (from the Visual Studio .NET menu, View Ø Other Windows Ø Object Browser. This is shown in the following graphic.) You can expand the node titled Interop.ComCalc. You will see that the component contains one class, called CMath. This class offers four methods. Notice the parameters that each method accepts.

    click to expand

  11. Create a simple user interface to test these methods. Your form will need three text boxes and two buttons, named as follows:

    • txtNum1

    • txtNum2

    • txtResult

    • btnAdd

    • btnSubtract

  12. In the Click event of btnAdd, add the following code:

    Dim Result As Short Dim objAdd As CMath = New CMath()    Result = objAdd.Add(CType(txtNum1.Text, Short), _      CType(txtNum2.Text, Short))    txtResult.Text = CType(result, String) 

    click to expand

  13. In the Click event of btnSubtract, add the following code:

    Dim Result As Short Dim objSub As CMath = New CMath()    Result = objSub.Subtract(CType(txtNum1.Text, Short), _       CType(txtNum2.Text, Short))    txtResult.Text = CType(Result, String)
  14. Run the project, enter some values into the two text boxes, and test each method. You should see results similar to those shown here. If you like, you can implement buttons for the Multiply and Divide methods as well.

end example

Command-Line Tools

If you are not working in Visual Studio .NET, there is also a command-line utility, tlbimp.exe, called the Type Library Importer, that can create an interop assembly from a COM type library or DLL. From the Windows command prompt, navigate to the directory that contains your Visual Basic .NET source code files. The next code snippet shows an example of using this utility to create a .NET interop assembly, called myInterop.dll, from a COM type library called myComponent.tlb. Use the /out: parameter to specify the name of the output file.

C:\path>tlbimp  myComponent.tlb  /out:myInterop.dll

Or, if you have only the COM DLL, the Type Library Importer can use that file instead.

C:\path>tlbimp myComponent.dll /out:myInterop.dll

You can use the .NET Framework’s Intermediate Language (IL) Disassembler tool, ildasm.exe, to view details about the interop assembly that you just created. You can see the GUID identifiers for the original COM component. You can also see the classes that are contained in the component. You will also see the methods that those classes expose and the data types of all arguments and return values. Notice that the COM classes will also have a default (non-parameterized) constructor method.

You can start ILDASM from the command prompt, as shown in the following code. Figure 2.4 shows what the interop assembly that you worked with in Exercise 2.2 looks like in ILDASM.

C:\path>ildasm myInterop.dll

click to expand
Figure 2.4: ILDASM

You can use the .NET Framework command-line compiler to compile your Visual Basic .NET application. Use the /r: parameter to specify that your application references the interop assembly. Use the /o: parameter to specify the name of the output file.

C:\path>vbc mySource.vb /r:myInterop.dll /o:myApp.exe



MCAD/MCSD(c) Visual Basic. NET XML Web Services and Server Components Study Guide
MCAD/MCSD: Visual Basic .NET XML Web Services and Server Components Study Guide
ISBN: 0782141935
EAN: 2147483647
Year: 2005
Pages: 153

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net