C++中调用的DLL格式如下
DLLIMPORT BOOL PTP_SetDefaultPath(CString path);C#中如何写?主要是CString如何定义?

解决方案 »

  1.   

    要是不行就用StringBuilder看看,C#就2种字符串了
      

  2.   

    CString 对应 const char* 或const wchar_t* 所以按规则在c#里他表现为stringps:需要注意的是需要知道到底是ANSI还是unicode的字符串,这个可以在声明的时候由CharSet枚举提供
      

  3.   

    string或者用StringBuilde两个都试试吧
      

  4.   

    C#绝大部分下无法利用C++的class
    用c++再做一次包装,把cstring 转为 char* or 直接转为system::string^
      

  5.   

    完了,dll是厂商提供的,无法修改,悲剧
    是不是意味着无法使用C#了?
      

  6.   

    如果是ANSI,方法定义为     
     
        [System.Runtime.InteropServices.DllImportAttribute("dll文件名", EntryPoint="PTP_SetDefaultPath")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public static extern  bool PTP_SetDefaultPath([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string path) ;如果是unicode 方法定义为:
      [System.Runtime.InteropServices.DllImportAttribute("你的dll文件名", EntryPoint="PTP_SetDefaultPath")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public static extern  bool PTP_SetDefaultPath([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string path) ;
      

  7.   

    肯定不可以了。CString 在 c++里面是类。
    用c语中间写个过渡的。
      

  8.   

    要厂家提供不基于MFC的接口
    像这样用MFC做接口,就算大家都用同一个版本的MFC,换个编译配置就不兼容了。
      

  9.   


    话说接口参数类型换成BSTR不就了吗?
      

  10.   

    LPSTR或者LPWSTR也可以,只要参数的类型的内存布局不随VC版本变化就行。
      

  11.   

    完了,dll是厂商提供的,无法修改,悲剧
    是不是意味着无法使用C#了?
    不是让你修改他的,是再做一个C++dll,写一个函数,那个CString参数在这里用char*,你这个函数负责把char*转换成Cstring的给那个厂商的dll,你的C#代码调用你新写的这个dll
      

  12.   

    完了,dll是厂商提供的,无法修改,悲剧
    是不是意味着无法使用C#了?
    不是让你修改他的,是再做一个C++dll,写一个函数,那个CString参数在这里用char*,你这个函数负责把char*转换成Cstring的给那个厂商的dll,你的C#代码调用你新写的这个dll
    已经这样搞了