C#中调试C++的DLL

zz from http://blog.csdn.net/xiaogelee/archive/2007/12/03/1914057.aspx

被C#调用的DLL一般只需要把导出的函数以适当的形式呈现即可调用,比如
extern "C" __declspec(dllexport)
BOOL Integrate3 (){...},这样的函数,在C#里面声明如:

[DllImport("xxx.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern bool Integrate3();,这里的调用相对是简单的,而有些数据类型则必须通过MarshalAs来做托管类型的转换,如:

extern "C" __declspec(dllexport)
BOOL Integrate (LPCWSTR file1, LPCWSTR file2, LPCWSTR outputFile){...}

由于数据类型不一致,所以在声明时要注意把类型转换过来。

[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#项目属性中“启用调试项”中一项:“启用非托管代码调试”,钩上这个,就万事大吉了,就像你调试一般的程序一样。

posted @ 2009-03-20 22:28  拒绝潜水的鱼  阅读(444)  评论(0编辑  收藏  举报