Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
iii |
|
|
|
Instructor Notes
Presentation:
90 Minutes
Lab:
60 Minutes
After completing this module, students will be able to:
!State the need for interoperability between Microsoft® .NET assemblies and COM components.
!Use platform invoke to call a function in a dynamic-link library (DLL).
!Expose the methods and properties of a COM object to an assembly.
!Describe the three ways to generate runtime callable wrappers.
!Use Microsoft Visual Studio® .NET to call a COM object.
!Use the Type Library Importer to generate metadata from a type library.
!Expose the methods and properties of a .NET Framework class to a COM client.
!Use the Type Library Exporter to generate a type library for an assembly.
!Use the ClassInterfaceAttribute to control and modify the type of interface that is generated for a .NET Framework class.
Materials and Preparation
This section provides the materials and preparation tasks that you need to teach this module.
Required Materials
To teach this module, you need the Microsoft PowerPoint® file 2349B_15.ppt.
Preparation Tasks
To prepare for this module, you should:
!Read all of the materials for this module.
!Complete the lab.
iv |
Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
Demonstrations
The following demonstrations are used to show how to create a managed assembly from an unmanaged DLL file, and how to create a COM component from a managed assembly.
In this module, use a Visual Studio .NET Command Prompt window to obtain a command prompt window with the appropriate environment values that are required for .NET tools.
Demonstration: Using the Type Library Importer
Before running the demo, the COM object needs to be registered in the system registry.
Important To use Microsoft Visual Studio .NET tools within a command prompt window, the command prompt window must have the proper environment settings. The Visual Studio .NET Command Prompt window provides such an environment. To run a Visual Studio .NET Command Prompt window, click Start, All Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools, and Visual Studio .NET Command Prompt.
!To register the COM object
1.At the command prompt, change the current folder to <install folder>\ Democode\Mod15\Demo15.1.
2.Run regsvr32 TLBIMPDemo.dll to register the COM object.
3.A dialog box appears informing you that the registration succeeded. To dismiss the dialog box, click OK.
In this demonstration, students will see how to use the Type Library Importer to generate metadata for a DLL, and then use the metadata to create a managed wrapper for the component.
!To create a managed wrapper for the TLBIMPDemo component and generate metadata by using the Type Library Importer
1.Run the Type Library Importer on TLBIMPDemo.dll, by typing the following command in a Visual Studio .NET Command Prompt window:
tlbimp TLBIMPDemo.dll /out:Calculator.dll
The resulting DLL is a .NET assembly and is named Calculator.dll. 2. Build the calculator application by typing the following command:
csc /reference:Calculator.dll CalcUI.cs
Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
v |
|
|
|
!To run the calculator application and view the metadata of Calculator.dll
1.Run CalcUI.exe from the command prompt by typing the following command:
CalcUI
2.Exit CalcUI.exe.
3.In a Visual Studio .NET Command Prompt window, run the Microsoft intermediate language (MSIL) Disassembler on Calculator.dll by typing the following command:
ildasm /adv Calculator.dll
4.To display the contents of the manifest, double-click MANIFEST.
5.Notice the assembly Calculator that appears near the end of the manifest. It should look similar to the following example:
.assembly Calculator
{
.custom instance void! [mscorlib]System.Runtime.InteropServices.GuidAttribute::
.ctor(string) = (…
.custom instance void! [mscorlib]System.Runtime.InteropServices.! ImportedFromTypeLibAttribute::.ctor(string) = …! // ...TLBIMPDemo..
.hash algorithm 0x00008004
.ver 1:0:0:0
}
6.This entry in the manifest demonstrates the referencing of an unmanaged COM component from managed code through the interoperability services provided by the .NET Framework.
vi |
Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
Demonstration: Using the Type Library Exporter
In this demonstration, students will see how to use the Type Library Exporter to generate a type library that describes the types defined in a common language runtime assembly.
!To generate a type library from an assembly by using the Type Library Exporter
1.In a Visual Studio .NET Command Prompt window, change the current folder to <install folder>\Democode\Mod15\Demo15.2.
2.To create a type library for Calculator.dll, type the following command:
tlbexp Calculator.dll /out:Calculator.tlb
3. The Type Library Exporter creates the Calculator.tlb file.
!To view the contents of the type library
1.Start a Visual Studio .NET Command Prompt.
2.Type oleview and press Enter.
3.In the OLE/COM Object Viewer window, on the File menu, click View TypeLib.
4.In the Open dialog box, move to <install_folder>\Democode\ Mod15\Demo15.2, select Calculator.tlb, and then click Open. The ITypeLib Viewer window appears with the information that is found in the type library.
Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
vii |
|
|
|
Module Strategy
Use the following strategy to present this module:
!Integration Services
This module provides an introduction to COM interoperability and platform invoke. Ensure that students understand the difference between calling an API function that is implemented in a DLL and calling a COM component.
!Platform Invoke
Concentrate on the information in the slides titled “How Platform Invoke Works” and “Calling Unmanaged Functions”.
!Calling COM Objects from Managed Code
Concentrate on the procedure for generating runtime callable wrappers, the use of the type library importer, and the process for signature translation and error handling.
!Calling .NET Objects from COM Objects
Ensure that students understand the export process when the Type Library Exporter is used to generate a type library.
Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
1 |
|
|
|
Overview
Topic Objective
To provide an overview of the module topics and objectives.
Lead-in
In this module, you will learn about interoperating between managed and unmanaged code.
!Integration Services
!Platform Invoke
!Calling COM Objects from Managed Code
!Calling .NET Objects from COM Objects
*****************************ILLEGAL FOR NON-TRAINER USE******************************
After completing this module, you will be able to:
!State the need for interoperability between Microsoft® .NET assemblies and COM components.
!Use platform invoke to call a function in a dynamic-link library (DLL).
!Expose the methods and properties of a COM object to an assembly.
!Describe the three ways to generate runtime callable wrappers.
!Use Microsoft Visual Studio® .NET to call a COM object.
!Use the Type Library Importer to generate metadata from a type library.
!Expose the methods and properties of a .NET Framework class to a COM client.
!Use the Type Library Exporter to generate a type library for an assembly.
!Use the ClassInterfaceAttribute to control and modify the type of interface that is generated for a .NET Framework class.
2Module 15 (Optional): Interoperating Between Managed and Unmanaged Code
" Integration Services
Topic Objective
To introduce the services provided by the .NET Framework that allow managed code to interoperate with unmanaged code.
Lead-in
The .NET Framework provides two services that enable managed code to interoperate with unmanaged code. This section provides an overview of these two services.
!Introduction to Platform Invoke
!Introduction to COM Interoperability
!Interop Marshaling Overview
*****************************ILLEGAL FOR NON-TRAINER USE******************************
When you adopt the .NET Framework to develop your applications, you may still have a lot of unmanaged code libraries implemented as DLLs and COM components. Code executing under the control of the runtime is called managed code. Conversely, code that runs outside the runtime is called unmanaged code. COM components, Microsoft ActiveX® interfaces, and Microsoft Win32® API functions are examples of unmanaged code.
You do not have to convert all of your existing unmanaged code to managed code before you can use it with .NET Framework applications. The Microsoft
.NET Framework promotes interaction with COM components, COM+ services, external type libraries, and many operating system services. Data types, method signatures, and error-handling mechanisms vary between managed and unmanaged object models. The .NET Framework offers two services—platform invoke and COM interoperability services—that allow managed code to interoperate with existing unmanaged code.
Note The ActiveX Control Importer (Aximp.exe) can be used to convert type definitions in a COM type library for an ActiveX control into a Microsoft Windows® Forms control. For additional information, see “Tools and Debuggers; Windows Forms ActiveX Control Importer (Aximp.exe)” in the
.NET Framework SDK documentation.
Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
3 |
|
|
|
Introduction to Platform Invoke
Topic Objective
To provide an overview of platform invoke.
Lead-in
Platform invoke allows managed code to interoperate with unmanaged APIs.
!Allows Managed Code to Call Unmanaged Functions That Are Implemented in a DLL
!Provides the Mechanism for
#Finding and invoking unmanaged functions
#Marshaling managed arguments to and from unmanaged code
*****************************ILLEGAL FOR NON-TRAINER USE******************************
Platform invoke allows managed code to call unmanaged functions that are implemented in a DLL. Platform invoke provides the mechanism for finding and invoking unmanaged functions, as well as marshaling managed arguments to and from unmanaged code.
When managed code calls an unmanaged function that is implemented in a DLL, platform invoke locates the DLL that implements the function, loads the DLL into memory, and locates the function address in memory. It then pushes the function’s arguments onto the stack, marshals any data that has to be marshaled, enables pre-emptive garbage collection, and transfers control to the address of the unmanaged code.
4Module 15 (Optional): Interoperating Between Managed and Unmanaged Code
Introduction to COM Interoperability
Topic Objective
To provide an overview of COM interoperability services provided by the
.NET Framework.
Lead-in
COM interoperability services allow managed code to interoperate with COM components.
!COM and .NET Differences Include:
#Lifetime management - reference counting versus garbage collection
#Service discovery - QueryInterface versus Reflection
#Object memory – fixed versus movable
!Runtime Callable Wrapper (RCW)
#Used by managed clients to call a method on a COM object
!COM Callable Wrapper (CCW)
#Used by COM clients to call a method on a managed object
*****************************ILLEGAL FOR NON-TRAINER USE******************************
COM differs from the .NET Framework object model in several important ways:
!Clients of COM objects must manage the lifetime of those objects; the common language runtime manages the lifetime of objects in its environment.
!Clients of COM objects discover whether a service is available by requesting an interface that provides that service and getting back an interface pointer, or not. Clients of .NET objects can obtain a description of an object's functionality using Reflection.
!Managed objects reside in memory managed by the .NET Framework execution environment. The execution environment can move objects around in memory for performance reasons and update all references to the objects it moves. Unmanaged clients, having obtained a pointer to an object, rely on the object to remain at the same location. These clients have no mechanism for dealing with an object whose location is not fixed.
To overcome these differences, the runtime provides wrapper classes to make both managed and unmanaged clients think they are calling objects within their respective environment. Whenever your managed client calls a method on a COM object, the runtime creates a runtime callable wrapper (RCW). RCWs abstract the differences between managed and unmanaged reference mechanisms, among other things. The runtime also creates a COM callable wrapper (CCW) to reverse the process, enabling a COM client to call a method on a .NET object.