Professional Documents
Culture Documents
70-536 Chapitre 13
Plan
Interoperation
Mohamed Romdhani
INSAT, Octobre 2007 2
M. Romdhani, Novembre 2007
Lesson 1 : Using COM Objects Unlike pure .NET components, COM components must be registered before they
can be used. After they are registered, these components need to be imported
by using Visual Studio 2005 or the Type Library Importer tool (TlbImp.exe).
Perform the following steps to execute the Regsvr32 command to ensure that a
COM DLL you are using is registered:
Open a new Command window and Execute Regsvr32 yourdll.dll.
Now that the DLL has been registered, you have two ways to import it:
1. Visual Studio 2005
2. TlbImp.exe
Strictly speaking, you can also use some of the services available in the
System.Runtime.InteropServices namespace, but doing so is cumbersome
and error prone. For the sake of this lesson, we'll focus on the two approaches
M. Romdhani, Novembre 2007 3 just listed.
M. Romdhani, Novembre 2007 4
With one minor exception, importing a COM Using the Type Library Importer utility (TlbImp.exe) is a little more
library is virtually indistinguishable from intricate but still quite straightforward. To import a library using the
importing any other type. TlbImp.exe, do the following:
The exception is that most of the DLLs you 1. Open the Visual Studio 2005 command prompt.
reference will be located on the COM tab of
the Add Reference dialog box, as illustrated 2. Navigate to the location of the DLL you want to import.
in front. 3. Type tlbimp <dllname>.dll
4. This will import the DLL and create a .NET assembly with its original
name. For example, Person.dll will be imported as Person.dll, MyObject
All registered COM components will be visible will be imported as MyObject.dll, and so forth.
there, so simply do the following to finalize the process:
5. If you want a name to be used other than the original DLL name, type
1. Create a new blank solution named COMDemos. tlbimp <dllname>.dll /out:<DesiredName>.dll.
2. Create a new Visual Basic 2005 or C# 2005 project, and name it
TypeDemoVB or TypeDemoCS, depending on the language you use. Now add a reference to the assembly name you chose just as you
3. Expand the Project list in Solution Explorer, and right-click the would for any other .NET assembly.
References node. The important thing to remember is that TlbImp.exe is creating a new
4. Choose the Add Reference option, and select the COM tab. (With most assembly for you from the COM library. So now you have a brand new
configurations, this will be the second tab of the dialog box, located next .NET assembly, and it will be visible only under the .NET tab of the Add
to the .NET tab.) Reference dialog box.
5. Find the component you want to register, and click OK.
M. Romdhani, Novembre 2007 5 M. Romdhani, Novembre 2007 6
1
Chapitre 1- Introduction aux technologies J2EE
Lesson 1 Summary
You can use the COM tab of the Add Reference dialog box to add a reference to
registered COM components.
2
Chapitre 1- Introduction aux technologies J2EE
Then you recompile the application with the new resource file added, as shown
here:
csc /t:library ComVisiblePerson.cs /win32res:ComVisiblePerson.res
In the following example, we're going to use the GetWindowText Windows API.
To do this, we need to ensure that we are accurately referencing the active
window running on the operating system, which might not be our application.
3
Chapitre 1- Introduction aux technologies J2EE
Using a Callback with Unmanaged Code Using a Callback with Unmanaged Code
using System;
Callback functions are an extremely important tool in any developer's using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
library. namespace NetForComDemoCS{
Callbacks are used throughout the .NET Framework and Framework Class public class UnmanagedCallbackDemo {
Library extensively, and most nontrivial applications will employ them in public delegate Boolean DemoCallback(IntPtr hWnd, Int32 lParam);
some fashion or another. private const String UserReference = "user32.dll";
private const Int32 BufferSize = 100;
Just as callbacks are important in a totally managed environment, they are
also important in an unmanaged environment. [DllImport(UserReference)]
public static extern Int32 EnumWindows(DemoCallback callback,Int32 param);
4
Chapitre 1- Introduction aux technologies J2EE
Exceptions in unmanaged code are markedly different from those Since the advent of .NET, there have been some shortcomings with using
thrown in managed code. unmanaged code. They are largely related to inherent differences between .NET
and previous development methodologies. Following is a list of those
In the earlier COM days, you could use the GetLastError function to get shortcomings:
the last error that was raised. This approach won't work in a managed Performance
application because the return value of GetLastError might not be the
correct one. Code that isn't managed by a runtime will typically have the ability to perform faster
than equivalent code that is managed. However, this benefit might not necessarily be
Why? Because the GetLastError method can be set by either a .NET realized. This is because of the overhead associated with marshaling information
Framework object or the common language runtime (CLR). between the unmanaged code and the .NET 2.0 runtime. It's important to remember
that unmanaged code can easily introduce issues such as memory leaks.
Because you can't use the GetLastError method, you need to do something Type safety
else. After all, the only thing more problematic than no exception handling is
bad exception handling. Unmanaged code is sometimes not type safe. This deficiency can have multiple
implications, including decreased readability and security issues.
Code security
The .NET Framework security model didn't exist previously. There's no way that code
written prior to this model can take advantage of it. Features such as declarative
security are not available in unmanaged code, which can mean your new .NET code
will be forced to accommodate this inconsistency.
Versioning
As is the case with security, versioning (which incidentally was a huge issue in prior
development environments) didn't exist in the form it does now. Therefore, side-by-side
execution might not be available when using unmanaged code.
The .NET Framework provides a mechanism to call Windows API calls and Because of its dependence on the Windows registry, COM Interop limits the
unmanaged code through Platform Invoke. platforms an application can run on.
To use P/Invoke, you use the DllImport attribute and the name of the DLL you The TlbImp.exe tool is a command-line mechanism you can use to import a COM
are referencing. component.
You must use the private and static/shared attributes for P/Invoke calls. The default behavior when catching System.Exception objects now catches
both CLS-compliant and non-CLS-compliant exceptions.
To allow default positioning in a structure that's to be marshaled, you can use
the Layout.Sequential attribute. The ComVisible attribute can be set to apply to an entire assembly, an entire
class, or individual members.
To specify a value for the positioning in a structure, you can use the
Layout.Explicit attribute. To use P/Invoke, you use the DllImport attribute and the name of the DLL you
are referencing.
Error messages from unmanaged code behave differently from managed
exceptions. To trap them correctly, the Windows API can be used. You must use the private and static/shared attributes for P/Invoke calls.