Platform invoke is a service that enables managed code to call unmanaged functions implemented in dynamic link libraries (DLLs), such as those in the Win32 API. It locates and invokes an exported function and marshals its arguments (integers, strings, arrays, structures, and so on) across the interoperation boundary as needed. For more information about this service, see A Closer Look at Platform Invoke.
This section introduces several tasks associated with consuming unmanaged DLL functions. In addition to the following tasks, there are general considerations and a link providing additional information and examples.
To consume exported DLL functions
-
Minimally, you must specify the name of the function and name of the DLL that contains it.
-
Create a class to hold DLL functions.
You can use an existing class, create an individual class for each unmanaged function, or create one class that contains a set of related unmanaged functions.
-
Create prototypes in managed code.
[Visual Basic] Use the Declare statement with the Function and Lib keywords. In some rare cases, you can use the DllImportAttribute with the Shared Function keywords. These cases are explained later in this section.
[C#] Use the DllImportAttribute to identify the DLL and function. Mark the method with the static and extern modifiers.
[C++] Use the DllImportAttribute to identify the DLL and function. Mark the wrapper method or function with extern "C".
-
Call the method on your managed class as you would any other managed method. Passing structures and implementing callback functions are special cases.
For examples that demonstrate how to construct .NET-based declarations to be used with platform invoke, see Marshaling Data with Platform Invoke.
一个简单的示例:
The following examples demonstrate how to define and call the MessageBox function in User32.dll, passing a simple string as an argument. In the examples, the DllImportAttribute.CharSet Field field is set to Auto to let the target platform determine the character width and string marshaling.
The same example appears in Visual Basic, C#, and C++. To show all examples, click the Language Filter button in the upper-left corner of the page. For additional examples, see Marshaling Data with Platform Invoke.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace CSharpUseUnmanagedDll
{
public class Win32
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr MessageBox(int hWnd, String text,
String caption, uint type);
}
public class HelloWorld
{
public static void Main()
{
Win32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0);
}
}
}
可以在托管代码中重命名win32 导出的函数。
The following example demonstrates how to replace MessageBoxA with MsgBox in your code by using the EntryPoint field.
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll", EntryPoint="MessageBoxA")]
public static extern int MsgBox(int hWnd, String text, String caption,
uint type);
}
Following is a list of possible reasons to rename a DLL function:
-
To avoid using case-sensitive API function names
-
To comply with existing naming standards
-
To accommodate functions that take different data types (by declaring multiple versions of the same DLL function)
-
To simplify using APIs that contain ANSI and Unicode versions
You can use the DllImportAttribute..::.EntryPoint field to specify a DLL function by name or ordinal. If the name of the function in your method definition is the same as the entry point in the DLL, you do not have to explicitly identify the function with the EntryPoint field. Otherwise, use one of the following attribute forms to indicate a name or ordinal:
[DllImport("dllname", EntryPoint="Functionname")]
[DllImport("dllname", EntryPoint="#123")]
Notice that you must prefix an ordinal with the pound sign (#).