Using COM Components

Using COM Components

Beyond simply including ActiveX components within Windows forms, you will also encounter situations that involve the instantiation and invocation of other types of legacy COM components, including controls and class libraries. During the process of migration to the .NET Framework, it is very possible that other developers within your organization may continue producing new COM-based functionality, while vendors may continue to make use of this technology for some time beyond your organization's migration. Therefore, it is critical to know how to use COM components within your .NET applications.

Runtime Callable Wrappers

The .NET Framework makes use of a Common Language Runtime (CLR), which provides support for managed code, including garbage collection, member management, security, and versioning. Legacy code, which is not designed to operate within the CLR, does not gain this support and is therefore termed unmanaged code.

A proxy class must wrap unmanaged code, such as COM components, in order to allow the CLR to call the encapsulated component. This proxy is termed a runtime-callable wrapper . If you have Visual Basic 6.0, you may use the following steps to create a suitable COM component to test the inclusion of an unmanaged COM component within a .NET application:

  1. Open Visual Basic 6.0 and create a new ActiveX DLL project.

  2. Rename the Project1 node to MyCustomer and the Class1 node to Balances in the Project Explorer.

  3. Add the following code to the Balances class:

     Option Explicit Private mintCustomerCount As Integer Private macurBalances(1 To 10) As Currency ' Create a read-only CustomerCount property Public Property Get CustomerCount() As Integer     CustomerCount = mintCustomerCount End Property ' Create a GetBalance method Public Function GetBalance(CustomerNumber As Integer) As Currency     GetBalance = macurBalances(CustomerNumber) End Function ' Initialize the data Private Sub Class_Initialize()     Dim intI As Integer     mintCustomerCount = 10     For intI = 1 To 10         macurBalances(intI) = Int(Rnd(1) * 100000) / 100     Next intI End Sub 
  4. Save the project and then build the MyCustomer.dll file to create the COM component.

The Type Library Importer ( tlbimp.exe )

It is possible to import the metadata of a COM component (contained in the component's type library) by using the Type Library Importer ( tlbimp.exe ). You can create a runtime-callable wrapper for the COM component created in the last example using the following steps:

  1. Open the .NET command prompt, accessed through Start, Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools, Visual Studio .NET Command Prompt.

  2. Navigate to the folder containing MyCustomers.dll and enter the following code:

     tlbimp MyCustomer.dll /out:NETMyCustomer.dll 
  3. After creating a new project with a form, you may right-click the References node in the Solution Explorer and select Add Reference.

  4. Navigate to the NETMyCustomer.dll file and then click OK to add the new reference to the project.

  5. Add the following code to the top of the form's code module:

     Imports NETMyCustomer 
  6. Use the component within your code as you would any .NET managed code component. The wrapper will serve as a proxy between the .NET and COM technologies.

Table 9.1 displays a number of important options for the Type Library Importer ( tblimp.exe ).

Table 9.1. Options for the Type Library Importer

Option

Meaning

/asmversion: versionNumber

The assembly's version number

/delaysign

Prepares the assembly for delay signing

/help

Displays help for the listed options

/keycontainer: containerName

Signs the assembly with the strong name from the specified key container

/ keyfile : filename

Signs the assembly with the strong name from the specified key file

/namespace: namespace

The namespace for the assembly

/out:filename

The name of the created assembly

/primary

Produces a primary interop assembly

/ publickey : filename

The file containing a public key used to sign the resulting file

/reference: filename

A file to be used to resolve references from the imported file

/silent

Suppresses information that would be displayed during conversion

/strictref

Refuses to create the assembly if any references cannot be resolved

/sysarray

Imports COM SafeArrays as instances of System.Array class

/unsafe

Creates interfaces without .NET security checks

/verbose

Displays additional information during conversion

/?

Displays help on the listed options

Using COM Components Directly

The Visual Studio .NET interface supports directly adding a reference to COM components. Here are the steps to follow:

  1. After creating a new project with a form, right-click the References node in the Solution Explorer and select Add Reference.

  2. Select the COM tab in the Add Reference dialog box and navigate the list of registered COM components until you locate the MyCustomer library.

  3. Click OK to add the reference to the COM component.

  4. Add the following code to the top of your form's code module:

     Imports MyCustomer 
  5. Use the component within your code as you would any .NET managed code component. The wrapper will serve as a proxy between the .NET and COM technologies.

graphics/alert_icon.gif

When deciding between the Type Library Importer and directly referencing a COM component, keep in mind that you should use the direct reference only if you do not need to be able to sign the resultant assembly and place it in the Global Assembly Cache (GAC) for distribution. If you need to distribute the application or need the ability to manage its name, namespace, or version, then you must use the Type Library Importer.


Using COM+ Components

COM+ extends the COM standard to allow several new features when running under Windows 2000 and later, including the following:

  • Role-based security

  • Object pooling and reusability

  • Queued components for asynchronous calls

  • Transactional processing

  • Publisher/subscriber events model

Creating references to COM+ components may be performed using the same techniques as used for COM components. Wrapped COM and COM+ components within .NET applications share the same basic problems, as detailed earlier in this section.



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

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