
C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#
.pdfModule 15 (Optional): Interoperating Between Managed and Unmanaged Code |
35 |
|
|
|
|
Although the import process generally produces accurate interop assemblies, there are cases when you can (or must) modify the interop assembly to produce a custom RCW.
There are several reasons why you might customize an RCW, such as:
!One or more types require additional marshaling information.
!A type library contains many specialized types that are unrecognizable to the marshaler.
!A large type library can include types that are unnecessary for an application.
You can eliminate the need to deploy unnecessary types by creating an interop assembly from managed source code.
If you must customize the runtime callable wrapper with additional or different marshaling instructions, you can either:
1.Edit the interop assembly, searching for problematic syntax and replacing it with alternative syntax.
This option is best for minor marshaling changes.
2.Create a wrapper manually, based on an existing Interface Definition Language (IDL) file or type library.
Declaring COM types manually is a difficult activity that requires working knowledge of the Type Library Importer (Tlbimp.exe), the default behavior of the interop marshaler, and COM. This approach is best used when you have an entire library of specialized types or require the RCW source code.
For more information about implementing these choices, see the .NET
Framework Software Development Kit (SDK) documentation.

Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
37 |
|
|
|
|
The SuppressUnmanagedCodeSecurityAttribute attribute helps improve code performance by suppressing asserts and demands at run time. These run time asserts and demands are the usual security checks performed when calling unmanaged code. Only code that has been granted the UnmanagedCode permission can use the SuppressUnmanagedCodeSecurityAttribute attribute, however. Using this attribute in a class or module applies to all methods contained in the class or module.
When you use the SuppressUnmanagedCodeSecurityAttribute attribute in a class, code that does not have permission to access unmanaged code can call methods in the class to access unmanaged code. Therefore, you must make sure that when you use the SuppressUnmanagedCodeSecurityAttribute attribute, you write secure code. Otherwise, your code can be misused by malicious code.


Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
39 |
|
|
|
|
Lab 15.2: Calling COM Objects
Topic Objective
To introduce the lab.
Lead-in
In this lab, you will call a COM object from managed code.
*****************************ILLEGAL FOR NON-TRAINER USE******************************
Objectives
After completing this lab, you will be able to call a COM object from managed code.
Estimated time to complete this lab: 30 minutes

40 |
Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
Exercise 1
Calling a COM Object From Managed Code
In this exercise, you will call a COM object from managed code. This includes registering the COM DLL, adding a reference to the COM object in a
Visual Studio .NET project, and then adding code to call the COM object.
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.
!Register the COM object
1.At the command prompt, change the current folder to <install folder>\Labs\ Lab15\Lab15.2\Starter.
2.To register the COM object, run regsvr32 VBUnmanagedCalculator.dll.
3.A dialog box appears informing you that the registration succeeded. Click OK to dismiss the dialog box.
!Open the Visual Studio .NET project for this exercise and examine the application UI
1.In Windows Explorer, move to <install folder>\Labs\Lab15\Lab15.2\ Starter.
2.Double-click CallingComFromDotNet.csproj to open the project for this exercise in Visual Studio .NET.
3.In the Solution Explorer pane, double click CalcUI.cs. The UI design for the Windows Forms application appears. Examine the UI for a moment to become familiar with it.
4.Press F7 to edit the code that implements the Windows Form application.
Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
41 |
|
|
|
|
! Add the reference to the COM object and attempt to build the project
1.On the Project menu, click Add Reference.
2.In the Add Reference dialog box, click the COM tab. In the list of registered COM objects, click VBUnmanagedCalculator.
3.To add VBUnmanagedCalculator to the Selected Components window, click Select.
4.To add the reference, click OK. Notice that the VBUnmanagedCalculator DLL is added to the project references.
5.On the Build menu, click Build Solution. Your application does not build successfully yet, but the runtime callable wrapper assembly for the
VBUnmanagedCalculator.dll is generated.
6.In a Visual Studio .NET Command Prompt window, change the current folder to <install folder>\Labs\Lab15\Lab15.2\ Starter\bin\Debug.
7.To view the metadata in the runtime callable wrapper assembly, at the command prompt, type ildasm /adv Interop.VBUnmanagedCalculator.dll
Use the MSIL Disassembler to view the namespace and classes created for the COM object.
! Modify the code in the project to call the COM object
1.In the CalcUI.cs file, find the first ‘TODO’ comment that instructs you to add a using statement for VBUnmanagedCalculator. Add the correct using statement. Notice that you can use the Task List tab in the lower-left hand pane to find the ‘TODO’ comments. Click View, select Show Tasks, and then click All if the ‘TODO’ comments do not appear in the task list.
2.Find the next ‘TODO’ comment that instructs you to instantiate a CalcEngine object. Add the code to instantiate a CalcEngine object and assign it to the calcEngine private member variable.
3.Scroll down to the next ‘TODO’ comment that instructs you to call GetVersion in CalcEngine. Add the code to make the call described and assign the results of the call to the VersionInfo text box.
4.Scroll down to the KeyDate_Click method. Complete the code in this method by replacing the ‘TODO’ comment with code to obtain the date by calling the GetDate method in CalcEngine and assign the results of the call to the OutputDisplay text box. Then, add code to reset the engine for the next equation by calling the CalcReset method in CalcEngine.
5.In the KeyClear_Click method, complete the code by replacing the ‘TODO’ comment with code that calls the CalcReset method in
CalcEngine and clears the OutputDisplay text box.

42Module 15 (Optional): Interoperating Between Managed and Unmanaged Code
!Build and test your application
1.On the File menu, click Save All.
2.On the Build menu, click Build Solution. Your application should build successfully. The following statement should appear as the last line of the output window:
Build: 1 succeeded, 0 failed, 0 skipped.
3.On the Debug menu, click Start. When the calculator appears, check the version number. It should read “Calculator – VB6 Unmanaged Component”. Enter some equations to test the application.
4.Close the application.
Note If you wish to test the solution project, you will first need to follow the steps in the “Register the COM object”, “Open the Visual Studio .NET project for this exercise and examine the application UI”, and “Add the reference to the COM object and attempt to build the project” procedures above.

