在c#项目中调用及调试c++写的dll

来源:互联网 发布:世界上封锁网络的国家 编辑:程序博客网 时间:2024/05/21 11:04

调用:

c++中的导出函数:
extern "C" __declspec(dllexport)BOOL Integrate (LPCWSTR file1, LPCWSTR file2, LPCWSTR outputFile){...}

 

由于c++和c#数据类型不一致,所以在c#中声明时要注意把参数类型转换过来。
[DllImport("xxx.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]        public static extern bool Integrate([In, MarshalAs(UnmanagedType.LPWStr)]string file1,             [In, MarshalAs(UnmanagedType.LPWStr)]string file2, [In, MarshalAs(UnmanagedType.LPWStr)]string outputFile);
这样调用基本是没有问题了,重点在于数据类型的转换。
 

调试:

很多人都会遇到在C#中调试C++的DLL时,发现在C++的DLL中设置不了断点,也就无法跟进去调试,
其实只需要在C#项目属性-->调试-->启用调试项:“启用非托管代码调试”钩上就万事大吉了。
这是我的一点实践经验,希望对你能有帮助。
原创粉丝点击