[DllImport("SCICommSys.dll",
               EntryPoint = "SelectVat",
                    CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
 public static extern bool SelectVat(string sendtoECU);
字符串参数无法传入,后又改为字符数组.
VC++: extern "C"  BYTE DllExport SelectVatChar(char send[100]);  
[DllImport("SCICommSys.dll",
               EntryPoint = "SelectVat",
                    CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
 public static extern bool SelectVat(char send[100]);    
就是这样一个VC++写的DLL,函数参数带有字符串,调用不成功,后人家在VC++里又改成字符数组,我还是调用不成功.并且再试了很多方法时候,有些还会报尝试读取或写入受保护的内存,这通常指示其他内存已损坏.
完全按字符数组传递就会出现尝试读取或写入受保护的内存,这通常指示其他内存已损坏.请问有过此类经验的朋友.(不再改VC++的函数,如何顺利传递此类参数)我在网上查过很多贴,都有类似情况,看来C#传递字符串参数给VC++的DLL是存在些问题.解决方法一\http://www.80diy.com/home/20030916/14/2265493.html
该贴提到: VC++里是字符数组,C#用string做参数传递能行,   用stringbuild给string分配空间就行了.
我的疑惑:如何 用stringbuild给string分配空间
解决方法二\http://topic.csdn.net/t/20061206/11/5210227.html解决办法三\http://topic.csdn.net/t/20051025/11/4348590.html总之,我发现网上遇到此问题的也不少,我试过指针传递,StringBulider传递,以及还有人说是CharSet = CharSet.Unicode
字符封装类型,AUTO,ANSI我都试过,都不能成功调用DLL.看来这是个老大难问题.请大家发表一下自己的看法和解决方法,如若我顺利解决此问题,另+100分.最好给些示例.传字符串怎么传,字符数组又怎么传

解决方案 »

  1.   

    字符数组参数是
    [DllImport("SCICommSys.dll", 
                   EntryPoint = "SelectVat", 
                        CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] 
     public static extern bool SelectVat(char[] send); 写错了,不好意思.
      

  2.   

    字符数组参数是
    [DllImport("SCICommSys.dll", 
                   EntryPoint = "SelectVat", 
                        CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] 
     public static extern bool SelectVat(char[] send); 写错了,不好意思.
      

  3.   

    VC++: extern "C"  BYTE DllExport SelectVatChar(char* send);   [DllImport("SCICommSys.dll",  
                   EntryPoint = "SelectVat",  
                        CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]  
     public static extern bool SelectVat(string send);  这样不行吗?有没有c++的源代码 我看一看
      

  4.   

    char* GetString(char string[10])
    {

    string[0]='1';
    return string;
    }        [DllImport("t2.dll")]
            public static extern string GetString(char[] x);     char[] c = "aaaa".ToCharArray();
                string s = GetString(c);
    ....每什么问题..
      

  5.   

    ericzhangbo1982111 提供的方法,我试一下,成功后马上给分!如不行,麻烦大家继续给我想办法.
      

  6.   

    ericzhangbo1982111 给的方法小弟不能理解,
    char* GetString(char string[10]) 
    { string[0]='1'; 
    return string; 

    这段是什么意思?是一个函数体,后面又调用它 string s = GetString(c)?
    但是这格式不是个函数,C#里报错 是不是函数指针哟,用委托?
      

  7.   


    [DllImport("SCICommSys.dll", 
                   EntryPoint = "SelectVat", 
                        CharSet = CharSet.Unicode//字符集改为ansi
    , CallingConvention = CallingConvention.StdCall)] 
     public static extern bool SelectVat(string sendtoECU); 在C++中
    void cunc(char*a);
    void func(char a[100]);
    等价
      

  8.   

    char[] c = "aaaa".ToCharArray(); 
                string s = GetString(c); 
    我传入一个char数组
    在GetString这个方法中修改了第一个字符
    然后把这个char数组返回回去..我的意思就是楼上说的
    在C++中 
    void cunc(char*a); 
    void func(char a[100]); 
    等价
    所以我传入char[10]返回char*
      

  9.   

    stringbuild给string分配空间
    StringBuilder sb = new StringBuilder(100); //初始化100个字符的StringBuilder,可以作为参数传入。
    还有,我怎么决定EnrtryPoint应该是SelectVatChar?你C++的声明是什么?
      

  10.   

    hanjun1024说的SelectVatChar,是的,我把入口函数写错了,但是改过来用stringbuild,传stringbuild函数依然报尝试读取或写入受保护的内存,这通常指示其他内存已损坏. 
    c++ 定义extern "C"  BYTE DllExport SelectVatChar(char chMoHe[100], int intSCICodeStyle);
    字符数组char chMoHe[100] 长度100  结束位“FFFF”  不足的位置用FFFF补足。  我的引入函数:
      [DllImport("SCICommSys.dll",
                              EntryPoint = "SelectVat",
                                   CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
    public static extern bool SelectVatChar(char[] chMoHe, int intSCICodeStyle);
    我的调用函数,传入参数03000000000000000000000000FFFF.......FFF;(后面都是F);
     char[] sendtoECU = new char[100];
     sendtoECU[0] = '0';
     sendtoECU[1] = '3';
     for (int i = 2; i < 26; i++)
     {
        sendtoECU[i] = '0';
     }                   
     for (int j = 26; j < 100; j++)
     {
        sendtoECU[j] = 'F';
     }
     iResult = SelectVatChar(sendtoECU,0);//第二个参数为整型0ericzhangbo1982111 ,请问我在C#里该怎么写?谢谢
      

  11.   

    以我写的代码现在进入不报任何错误,但是在执行iResult = SelectVatChar(sendtoECU,0)时,明显程序执行此句时间很长,最后过了此句后iResult得到了TRUE值,但是硬件并没响应我的命令,我想C++里面多半也有错
      

  12.   

    你的执行时间很长,基本上可以肯定调用已经成功。至于返回true,应该是你声明方法的问题,返回值应该声明为byte。0代表成功还是1代表成功要看c++里怎么约定。如果封装回来的是bool型,不一定会得到准确结果,因为win32的bool型占用4个字节。