6.2. Using COM Objects in VB 2005

One of the primary reasons VB 6 programmers and their companies are reluctant to migrate to VB 2005 is the huge investments they have made in developing COM components. However, COM components continue to be supported in .NET. In the following sections, you will see how you can use legacy COM components in your VB 2005 applications.

The most direct way to use COM objects in VB 2005 is to use COM Interop. Although applications that run under the .NET Framework can only work with .NET components, .NET allows you to use your existing COM components by means of a Runtime Callable Wrapper (RCW). When you use RCW to interact with a COM object, an assembly is used as a wrapper for the COM object. The RCW acts as a bridge between the unmanaged code (the COM object) and managed code (your .NET application), and all communications with the COM object are routed through this class (see Figure 6-2).

Figure 6-2. Runtime Callable Wrapper

COM objects are unmanaged code, therefore you need to remember to free up their resources when they are no longer in use.

To illustrate the support of COM in .NET, let's consider a simple example.

Suppose you want to display a PDF file in a VB 2005 Windows application. To do so, you can make use of the Adobe Acrobat Browser Document control (which is a COM object) installed on your system (the component is installed on your computer when you install the Adobe Acrobat Reader).

First, create a new Windows application. To use the Adobe COM component, look for it and select it from the list of COM components on your system. To see the list, click the COM tag of the Add Reference dialog in Solution Explorer in Visual Studio 2005, as shown in Figure 6-3. Click OK.

Drag and drop the Adobe Acrobat 7.0 Browser Document, which is now located in the Toolbox under the All Windows Forms tab, onto your Windows Form (Form1, unless you have renamed it). The result is shown in Figure 6-4.

Figure 6-3. Add a COM component to your project

Figure 6-4. Filling the form with the Adobe COM component

To display a PDF document using the control, double-click on the Windows Form and code the Load event as shown in Example 6-1.

Example 6-1. Form1 Load event handler
 Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load, _ MyBase.Load AxAcroPDF1.LoadFile("C:\MiniReader.pdf") End Sub 

There isn't much difference in coding; your COM component is used much like a .NET component.

Besides exposing using COM components in a .NET application, you can also use .NET Components in a COM application via the COM Callable Wrapper (CCW). The CCW is used to marshal calls between managed and unmanaged code (see Figure 6-5), thereby allowing COM applications to make use of .NET components.

Figure 6-5. COM Callable Wrapper

Figure 6-6 shows what happens when the application is run.

Figure 6-6. Using the COM component

Not only does COM interop make it easy to use COM components in your VB 2005 application, it also does away with the error-prone task of installing and registering COM components on target computers through a new feature known as RegFree COM (Registration-Free COM).

RegFree COM runs only on Windows XP and later releases of the Windows operating system.

Using RegFree COM, you can deploy an application that uses a COM component without registering it on the user's machine, thus avoiding the notorius collection of problems commonly referred to as "DLL Hell." RegFree COM even allows you to run multiple versions of a COM component on the same machine.

RegFree COM works by automatically generating a manifest from the COM component's type library and component registration on the developer's machine. Therefore, while it is not required to install the component on the end users' machines, a copy must be registered on the developer's machine.

To enable use of RegFree COM, all COM components referenced in Visual Studio 2005 now have a new Isolated property (see Figure 6-7). If you set Isolated to true, the component can be deployed through ClickOnce, and Visual Studio 2005 will automatically do all the work to deploy the COM component onto the target machine (without needing to register it on the target machine).

Figure 6-7. Setting the Isolated property for RegFree COM

Visual Basic 2005 Jumpstart 2005
Visual Basic 2005 Jumpstart
ISBN: 059610071X
EAN: 2147483647
Year: 2005
Pages: 86
Authors: Wei-Meng Lee

Similar book on Amazon

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