答案是使用P/Invoke技术调用C++ DLL。首先在C++中用extern "C"和__declspec(dllexport)导出函数,如Add和PrintMessage;然后在C#中用[DllImport]声明对应方法,指定CallingConvention和CharSet;对于结构体需用[StructLayout]定义内存布局;最后确保平台匹配、DLL路径正确及函数名未被修饰。

在C#中调用C++编写的DLL,通常使用P/Invoke(Platform Invoke)技术。它允许托管代码调用非托管DLL中的函数,比如用C或C++编写的Win32 API或自定义DLL。下面详细介绍如何实现这一过程。
1. 编写C++ DLL导出函数
首先确保你的C++ DLL正确导出了可供外部调用的函数。推荐使用 extern "C" 防止C++命名修饰,并使用 __declspec(dllexport) 导出函数。
示例C++代码(MyCppDll.cpp):
立即学习“C++免费学习笔记(深入)”;
extern "C" __declspec(dllexport) int Add(int a, int b) {
return a + b;
}
extern "C" __declspec(dllexport) void PrintMessage(const char* msg) {
printf("Message: %s\n", msg);
}
编译为DLL后,例如生成 MyCppDll.dll。
2. 在C#中声明DLL导入函数
使用 [DllImport] 特性声明要调用的函数。注意指定DLL名称和参数类型映射。
C#代码示例:
using System; using System.Runtime.InteropServices;class Program { // 声明Add函数 [DllImport("MyCppDll.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int Add(int a, int b);
// 声明PrintMessage函数 [DllImport("MyCppDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public static extern void PrintMessage(string msg); static void Main() { int result = Add(5, 3); Console.WriteLine("5 + 3 = " + result); PrintMessage("Hello from C#!"); }}
关键点说明:
- DllImport属性:指定DLL文件名,无需路径则要求DLL在运行目录或系统路径中。
- CallingConvention:C++默认使用Cdecl调用约定,需显式指定。
- CharSet:字符串编码方式,Ansi对应char*,Unicode对应wchar_t*。
3. 处理复杂数据类型
当需要传递结构体或数组时,必须在C#中定义对应的布局。
例如C++结构体:
struct Point {
int x;
int y;
};
extern "C" __declspec(dllexport) double Distance(Point p1, Point p2);
C#中对应声明:
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public int x;
public int y;
}
[DllImport("MyCppDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double Distance(Point p1, Point p2);
如果传指针,可使用 ref 或 out 参数,或配合 Marshal 类手动管理内存。
4. 调试与常见问题
- 找不到DLL:确认DLL位于输出目录(如bin\Debug),或使用绝对路径。
- EntryPointNotFoundException:检查函数名是否被C++修饰,使用extern "C"避免名字损坏。
- 平台匹配:确保C#项目目标平台(x86/x64)与DLL一致。
- 字符集错误:若C++接收wchar_t*,应设CharSet.Unicode并使用IntPtr或MarshalAs处理。
可使用工具如dumpbin /exports MyCppDll.dll查看实际导出函数名。
基本上就这些。只要DLL导出规范,C#通过P/Invoke调用并不复杂,但要注意类型映射和调用约定的一致性。










