c#调用 c的dll
有两个传出参数 char* password,char* corp_iddll内部定义
unsigned long user_num;
.....
sprintf(corp_id,"%06ld",user_num);unsigned char cur_word,chk_word,pwd_word[17] = ""
.....
sprintf(password,"%s",pwd_word);
我在c#中用StringBuilder,password得不到值,corp_id得出的值是正确的,是什么原因啊???

解决方案 »

  1.   

    c原型
    int ReadMsg(char* szServerName,char* organ,char* password,char* corp_id,char* distract,int nature)szServerName,organ,nature输入参数
    password,corp_id,distract输出参数
      

  2.   

    corp_id,distract输出是正确的,只是password输出为空
      

  3.   

    这样定义:[DllImport("xxx.dll",CharSet = CharSet.Ansi,CallingConvention=CallingConvention.Cdecl)]
    int ReadMsg(string szServerName,string  organ,StringBuilder password,StringBuilder  corp_id,StringBuilder  distract,int nature)
      

  4.   


    我就是这样定义的,但password得不到,其他都对,返回值也正确,不晓得是啥原因
      

  5.   

    DllImport的属性也是这样:
    [DllImport("xxx.dll",CharSet = CharSet.Ansi,CallingConvention=CallingConvention.Cdecl)]
    还有,你把C#这边使用的几行代码贴出来
      

  6.   

             [DllImport("CrmDll.dll", EntryPoint = "RbMsg", CallingConvention = CallingConvention.Cdecl,CharSet=CharSet.Ansi)]
            public static extern int ReadMsg(string szServerName, string organ, StringBuilder password, StringBuilder corp_id, StringBuilder distract, int nature);
    public static string GetPassword()
    {
        string organ='0001';
        string serverName = '192.168.0.1';
                StringBuilder password = new StringBuilder("",50);
                StringBuilder corpid = new StringBuilder("",50);
                StringBuilder distract = new StringBuilder("",20);
                string pwd = "";
                int pdanum = 0;
                int nature = 1;
                try
                {
                       pdanum = ReadMsg(serverName, organ, password, corpid, distract, nature);
                       pwd = password.ToString();
                }
                catch 
                {
                    pwd = "";
                }
                return pwd;
    }
      

  7.   

    看上去没什么问题,而且“password得不到,其他都对,返回值也正确”!这可能要在dll这边进行调试了!
    你把这个dll的工程要过来,在VC那边进行调试
      

  8.   

    我觉得是VC里面的PWD返回值有问题吧,要不怎么会只有它出问题。
      

  9.   

    不是跟你说了,在VC这边进行调试,断点设置在函数入口。
    把net的应用程序,设置为这个dll工程的外挂,F5启动调试