C#调用C/C++ DLL方式总结

#C#与C/C++ DLL交互方式总结

C#调用C/C++ DLL导出

C/C++的导出函数:

int __stdcall AFunc(wchar_t* str)
{
	//ugsdifgoisuhfgiosugdtfuywegouy
	return 0;
}

C#声明:

[DllImport("xxxx.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
internal static extern int AFunc(char[] str);

最前面的为dll模块名称

CharSet声明char[]字符格式,这里设置为Unicode(UTF-16);

CallingConvention = CallingConvention.Winapi 设置函数调用规则

使用方法很简单:

void func()
{
	string str = "HelloWorld";
	AFunc(str.ToArray());
}

如果C/C++的形参是多字节,就会麻烦一些:
C/C++:

int __stdcall AFunc(char* str)
{
	//ugsdifgoisuhfgiosugdtfuywegouy
	return 0;
}

C#声明:

[DllImport("xxxx.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Winapi)]
internal static extern int AFunc(char[] str);

C#使用:

void 

你可能感兴趣的:(Windows)