Chapter 25. Native and COM Interoperability
This chapter describes how to integrate with native (unmanaged) DLLs and COM components. Unless otherwise
stated, the types mentioned in this chapter exist in either the System or the System.Runtime.InteropServices namespace.
Calling into Native DLLs
P/Invoke, short for Platform Invocation
Services, allows you to access functions, structs, and
callbacks in unmanaged DLLs. For example, consider the MessageBox function, defined in the Windows
DLL user32.dll as follows:
int MessageBox (HWND hWnd, LPCTSTR lpText, LPCTSTR lpCation, UINT uType);
You can call this function directly by declaring a static method
of the same name, applying the extern
keyword, and adding the DllImport
attribute:
using System;
using System.Runtime.InteropServices;
class MsgBoxTest
{
[DllImport("user32.dll")]
static extern int MessageBox (IntPtr hWnd, string text, string caption,
int type);
public static void Main()
{
MessageBox (IntPtr.Zero,
"Please do not press this again.", "Attention", 0);
}
}The MessageBox classes in the
System.Windows and System.Windows.Forms namespaces themselves
call similar unmanaged methods.
The CLR includes a marshaler that knows how to convert parameters
and return values between .NET types and unmanaged types. In this
example, the int parameters translate
directly to 4-byte integers that the function expects, and the string
parameters are converted into null-terminated arrays of 2-byte Unicode
characters. IntPtr is a struct designed to encapsulate ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access