P/Invoke是C#调用非托管代码的机制,通过DllImport声明外部方法,如调用MessageBox或GetSystemInfo,需注意参数类型映射、结构体布局及字符串编码,推荐使用pinvoke.net等工具辅助开发。

P/Invoke(Platform Invocation Services)是C#中用于调用非托管代码(如Win32 API、C/C++编写的DLL)的一种机制。它允许托管代码与本地系统库进行交互,比如调用Windows操作系统提供的API函数。
基本原理
当你在C#中使用P/Invoke时,.NET运行时会帮你封送(marshal)参数和返回值,在托管类型和非托管类型之间进行转换。你只需声明一个静态外部方法,并通过DllImport指定目标DLL和函数名。如何调用Win32 API
以调用MessageBox为例,展示如何使用P/Invoke:
using System;
using System.Runtime.InteropServices;
class Program
{
// 声明外部方法
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
static void Main()
{
MessageBox(IntPtr.Zero, "Hello from C#!", "Greeting", 0);
}
}
说明:
- DllImport:指定要调用的DLL名称(如user32.dll)
- CharSet:定义字符串编码方式,Auto会根据平台自动选择ANSI或Unicode版本
- extern:表示该方法在外部实现
- 参数类型需匹配Win32 API的定义,例如string会被自动转为LPCWSTR或LPCSTR
常见注意事项
成功调用Win32 API需要注意以下几点:- 确保函数名正确,有些API有A(ANSI)和W(Unicode)两个版本,推荐使用CharSet控制
- 数据类型的映射要准确,例如DWORD对应uint,BOOL对应bool(但需设置[MarshalAs])
- 复杂结构体需要用StructLayout标记布局方式
- 涉及指针或缓冲区时,注意内存管理和生命周期
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
public ushort processorArchitecture;
private ushort reserved;
public uint pageSize;
public IntPtr minimumApplicationAddress;
public IntPtr maximumApplicationAddress;
public IntPtr activeProcessorMask;
public uint numberOfProcessors;
public uint processorType;
public uint allocationGranularity;
public uint processorLevel;
public uint processorRevision;
}
[DllImport("kernel32.dll")]
static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo);
static void Main()
{
GetSystemInfo(out SYSTEM_INFO sysInfo);
Console.WriteLine($"处理器数量: {sysInfo.numberOfProcessors}");
}
工具辅助
手动查找API声明比较麻烦,可以借助以下资源:- pinvoke.net:社区维护的常用Win32 API C#封装
- Visual Studio的 IntelliSense 支持部分提示
- 使用工具如DllExport可反向从C#生成可被调用的DLL










