DllImportAttribute 类
作用:指示该属性化方法由非托管动态链接库 (DLL) 作为静态入口点公开。
命名空间:
System.Runtime.InteropServices
程序集:
System.Runtime.InteropServices.dll, mscorlib.dll, netstandard.dll
下面的代码示例演示如何使用DllImportAttribute属性导入 Win32 MessageBox函数。 然后, 该代码示例调用导入的方法。
using System;
using System.Runtime.InteropServices;
class Example
{
// Use DllImport to import the Win32 MessageBox function.
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
static void Main()
{
// Call the MessageBox function using platform invoke.
MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
}
}
字段 | 解释 |
---|---|
BestFitMapping | 将 Unicode 字符转换为 ANSI 字符时,启用或禁用最佳映射行为。 |
CallingConvention | 指示入口点的调用约定。 |
CharSet | 指示如何向方法封送字符串参数,并控制名称重整。 |
EntryPoint | 指示要调用的 DLL 入口点的名称或序号。 |
ExactSpelling | 控制 CharSet 字段是否使公共语言运行时在非托管 DLL 中搜索入口点名称,而不使用指定的入口点名称。 |
PreserveSig | 指示是否直接转换具有 HRESULT 或 retval 返回值的非托管方法,或是否自动将 HRESULT 或 retval 返回值转换为异常。 |
SetLastError | 指示被调用方在从属性化方法返回之前是否调用 SetLastError Windows API 函数。 |
ThrowOnUnmappableChar | 启用或禁用在遇到已被转换为 ANSI“?”字符的无法映射的 Unicode 字符时引发异常。 |