关于C#调用C++ dll, 以前写的博客找不到了,只能重新再写。
1)可以简单的参考:
Calling an unmanaged dll from .NET (C#)
https://blogs.msdn.microsoft.com/jonathanswift/2006/10/02/calling-an-unmanaged-dll-from-net-c/
注意点:
- extern "C" 必须加,否则导出的函数名会加入乱七八糟的后缀用于overloaded。
- dumpbin 命令可以看到导出的函数名
- [StructLayout(LayoutKind.Sequential)]
2)CLI/C++可以当做bridge来连接MFC和.net framework
创建一个MFC project后,利用下面方法可以支持CLR
https://msdn.microsoft.com/en-us/library/ms235211.aspx?f=255&MSPPError=-2147217396
http://www.dorodnic.com/blog/2014/12/10/calling-cpp-by-example/
注: MFC的CString转化成string: String^ str = marshal_as<String^>(strRungComment);
另外如果在CLI C++头文件定义函数后:
namespace Cpp
{
// This is our native implementation
// It's marked with __declspec(dllexport)
// to be visible from outside the DLL boundaries
class __declspec(dllexport) Logic
{
public:
int Get() const; // That's where our code goes
};
}
CPP文件可用下列方式实现:
#include "Logic.h"
int Cpp::Logic::Get() const
{
return 42; // Really, what else did you expect?
}
3) Marshal
https://msdn.microsoft.com/en-us/library/bb384865.aspx
https://msdn.microsoft.com/en-us/library/ms235282.aspx