zoukankan      html  css  js  c++  java
  • 在c#中调用并调试C++的DLL

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

  • 相关阅读:
    小程序学习资料
    tomcat单应用多实例部署报错 应用jar不存在
    nginx windows版本 1024限制
    mysql连接数
    rocketmq
    nginx路径匹配
    war包的压缩解压缩
    IIS访问HTTP Error 400. The request hostname is invalid
    Microsoft 安全扫描程序
    vscode
  • 原文地址:https://www.cnblogs.com/lizi/p/2363171.html
Copyright © 2011-2022 走看看