C++ dll中导出函数:
extern   "C"  __declspec(dllexport)  CString exportSession(CString  url)
{
 CString session=CRequest.GetSession(url);
return session;}
C#调用函数:  [DllImport("wh.dll")]
        static private extern string exportSession(string url);  string bb = exportSession("http://community.csdn.net/");
 结果传到C++函数中,看到URL是乱码,不知怎么回事?怎么修改

解决方案 »

  1.   

    [DllImport("wh.dll", CharSet = CharSet.Ansi)]
      static private extern string exportSession(string url);
      

  2.   

    试试charset   =   CharSet.Unicode或者charset   =   CharSet.Ansi   
      

  3.   

    那把 CallingConvention=CallingConvention.Cdecl也加上去看看
      

  4.   

    exportSession(CString  url),是不是参数不能是CString 类型?C#不支持?
      

  5.   

    CString 可是MFC特有的,C#不支持的,最好DLL中是使用char *型的.
      

  6.   

    修改方法的特性,修改CharSet看看.
      

  7.   

    [DllImport("wh.dll", CharSet = CharSet.Ansi)]
      static private extern string exportSession([MarshalAs(UnmanagedType.LPStr)]string url);
      

  8.   

    那把 CallingConvention=CallingConvention.Cdecl也加上去看看
      

  9.   

    引用 1 楼 hapen_zhang 的回复:
    [DllImport("wh.dll", CharSet = CharSet.Ansi)]
    static private extern string exportSession(string url);
    那把 CallingConvention=CallingConvention.Cdecl也加上去看看试了,也不行。是不是兼容的问题
      

  10.   

    可能和charset 无关,试遍了也不行
      

  11.   

    CString 用 stringBuilder 代替
      

  12.   

    CharSet 还要用 CharSet.Ansi 修饰.
      

  13.   

    导出的时候要加上 __stdcall
    extern "C" __declspec(dllexport) char * __stdcall exportSession(char *url)
    {
    }
    C#调用函数:  [DllImport("wh.dll")]
      static private extern StringBuilder exportSession(StringBuilder url);
      

  14.   


    不是说了吗,CString 可是MFC特有的,char *是C语言中的基本类型,其它的平台都支持,改成这样方便跨平台调用.CString 只有MFC平台才支持,其它平台使用就会出问题的,不通用.
    系统的DLL中也都是用char *的
      

  15.   

    导出的时候要加上 __stdcall
    extern "C" __declspec(dllexport) char * __stdcall exportSession(char *url)
    {
    }
    C#调用函数:  [DllImport("wh.dll")]
      private static  extern StringBuilder exportSession(StringBuilder url);
      

  16.   

    如果通过VC++编写的DLL欲被其他语言编写的程序调用,应将函数的调用方式声明为__stdcall方式,WINAPI都采用这种方式,而C/C++缺省的调用方式却为__cdecl。
      

  17.   

    __stdcall
    __cdecl
    __pascal
    在网上查找这几个关键字,看看都是干什么的。
    其他的就要靠经验的积累了。