日期:2014-05-18  浏览次数:21035 次

调用动态库的问题(高分)
这是动态库里面的原型函数
C/C++ code

int WINAPI MYGX(int port, char *Sign, char *BH, char *strReset, char *strMiWen3, char *strMac1, char *strKeyVer, char *strTime,char *strKey, char *strAuthenKey, char *strMac3);


 输入参数:
 port
 IntSign : 更新密钥的次数
 BH :表号
strReset : ESAM复位信息后8字节
 strMiWen3
 strMac1
 输出参数:
 strKeyVer :密钥版本号,为了和主站密钥版本做对比
 Time : 身份认证时效性
 strKey
 strMac3
 strAuthenKey
这是上面原型函数的说明

C# code

[DllImport("HNTestZhuZhan.dll")]
        public static extern int MYGX
        (int port, string Sign, string BH, string strReset, string strMiWen3, string strMac1, StringBuilder strKeyVer, StringBuilder strTime, StringBuilder strKey, StringBuilder strAuthenKey, StringBuilder strMac3);
        private void button1_Click(object sender, EventArgs e)
        {
            string Sign = "01";
            string BH = "000000000001";
            int port = comboBox1.SelectedIndex + 1;
            string strReset = "86935839aab50001";
            string strMiWen3 = "84aec3874ed6756642771ec6bad03a3e1404ab666645da5aed8b4f1b74e1bf30";
            string strMac1 = "15818dd0";
            StringBuilder strKeyVer = new StringBuilder();
            StringBuilder strTime = new StringBuilder();
            StringBuilder strKey = new StringBuilder();
            StringBuilder strAuthenKey = new StringBuilder();
            StringBuilder strMac3 = new StringBuilder();

            MYGX(port ,Sign ,BH ,strReset ,strMiWen3 ,strMac1 ,strKeyVer ,strTime ,strKey ,strAuthenKey ,strMac3);
           
            textBox5.Text = strKeyVer.ToString ();
            textBox4.Text = strTime.ToString();
            textBox6.Text = strMac3.ToString();
            textBox7.Text = strAuthenKey.ToString();
            textBox8.Text = strKey.ToString ();


点击button,不知道为什么只能得到strKeyVer的值.因为这个动态库里有好几个原型函数我调用其它的都能得到正确数据,不知道为什么调用上面这个原型函数的时候就得不到正确的数据,希望哪位高人指点指点..

------解决方案--------------------
StringBuilder strKeyVer = new StringBuilder(128);
StringBuilder strTime = new StringBuilder(128);
StringBuilder strKey = new StringBuilder(128);
StringBuilder strAuthenKey = new StringBuilder(128);
StringBuilder strMac3 = new StringBuilder(128);
创建StringBuilder实例时应该指定足够大的容量,否则如果返回的字符串比较长的时候就会出错了