24.2 Using COM Objects in .NET

 <  Day Day Up  >  

You want to use a COM object within a .NET application.


Technique

Using a COM object within a .NET application is similar to using any other .NET class, with the difference in the initial steps that you need to complete beforehand and the internal mechanics that occur within the Common Language Runtime (CLR). The first step is to create a reference to the COM object within your application. Click on Project, Add Reference from the main menu and select the COM tab within the Add Reference dialog. Assuming you didn't change the COM object name , the object will be named ProjectName 1.0 Type Library. For the example created in the previous recipe, the full name is CmdShell 1.0 Type Library.

Once you create a reference to the COM library containing the COM object you created, you can instantiate the object contained within the library, similar to the method used to create a .NET object. The namespace for the COM object is the name of the ATL project and contains interface declarations and class definitions. The following example places two TextBox controls on a Windows Form. When the user enters a command in the tbCommand text box, it is sent to the Run method defined in the COM object created in the last recipe. The result is placed within the tbTranscript text box. The following code demonstrates how to do this task. One particular thing to note is the way in which properties on the COM object are accessed. The CurrentDirectory property defined in the COM object utilizes the familiar syntax used by C#, even though COM object properties are internally implemented as get and set methods :

 
 private void tbCommand_KeyDown(object sender,     System.Windows.Forms.KeyEventArgs e) {     if( e.KeyCode == Keys.Return )     {         tbTranscript.Focus();         tbTranscript.Text += "\r\n" + commandObj.Run( tbCommand.Text );         tbTranscript.Text += "\r\n" + commandObj.CurrentDirectory + ">";         tbTranscript.SelectionStart = tbTranscript.Text.Length;         tbCommand.Text = "";     } } 

Events that are fired by a COM object use delegates for event handlers within .NET. However, although most delegates use two parameters, indicating the source of the event and any arguments, the delegate for a COM event contains only the event arguments it defines. The SomeEvent event created in the previous recipe uses no parameters, which means the delegate will have an empty parameter list. To attach an event handler to the SomeEvent event fired from the COM object, create a new delegate instance and assign it to the SomeEvent event defined in the COM object:

 
 public Form1() {     InitializeComponent();     commandObj = new CmdShell.CCommandComClass();     commandObj.SomeEvent +=         new CmdShell._ICommandComEvents_SomeEventEventHandler( OnSomeEvent ); } private void OnSomeEvent() {     MessageBox.Show( "Event handled within .NET object" ); } 

Comments

As you can see from the discussion, using COM objects within .NET is relatively straightforward, given the interoperability support within the .NET Framework. Adding a reference to a COM library to your .NET project creates a new assembly called the interop assembly . This assembly serves as a proxy between the .NET object and the COM object using a specialized .NET object called a Runtime Callable Wrapper ( RCW ) . The wrapper is generated by Visual Studio .NET by reading information from the COM object's type library. Whenever an ATL project is created using the methods described in Recipe 24.1, "Creating an ATL-Based COM Component," a type library is generated each time the project is built. The type library contains all the type information for the library, which includes information about the library itself as well as interfaces and their associated methods, properties, and events, and it is embedded within the library's resource recipe. You can almost consider the type library as the predecessor to the assembly manifest used within .NET assemblies. Whenever the type library for a COM object changes, which occurs as a result of adding or changing interfaces or interface methods, you have to repeat the steps to add the reference to your .NET project. Recipe 24.3, "Automatically Generating an Interop Assembly," describes how to automatically update your .NET project when these changes occur.

In Figure 24.1, you can see how the RCW relates to the COM object and the .NET client that uses it. You can also see the COM Callable Wrapper (CCW), which is discussed in Recipe 24.7, "Using .NET Objects in COM." The RCW lies in between the COM object and .NET client, facilitating the communication between the two. Furthermore, the RCW is also responsible for type marshalling. Marshalling is the process in which data types within one technology are converted to similar objects of the other technology. For instance, COM objects use a SAFEARRAY as a storage mechanism for arrays to support OLE automation clients, which can include scripting clients or Visual Basic 6 applications. The RCW generates a System.Array parameter for the COM method and marshals the data to convert it from a System.Array object to a SAFEARRAY for use within the COM object.

Figure 24.1. Relationship of RCW between a COM object and a .NET client.

graphics/24fig01.gif

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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