SenseLock开发手记(三)时间:2021-10-09 09:44:09c#调用vc dll 1.添加一个c#项目,建一个窗体。拉3个文本框,一个按钮过来。 2.添加dll函数声明 /// <summary> /// SenseLock hash加密方法声明 /// </summary> /// <param name="pbInData">输入数据,明文</param> /// <param name="inLen">数据数据长度</param> /// <param name="pbOutData">输出数据,密文</param> /// <param name="outLen">输出数据长度</param> /// <param name="iOsutSize">输出数据实际使用长度</param> /// <returns>返回0为调用成功,返回非0为调用失败</returns> [DllImport("SenseLockVC.dll", CharSet = CharSet.Auto)] public static extern int SenseLock_Hash(IntPtr pbInData, int inLen, IntPtr pbOutData, int outLen, IntPtr iOsutSize); 3.在按钮的clikck事件中写代码: /// <summary> /// hash 算法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Btn_Hash_Click(object sender, EventArgs e) { this.TxtResult.Text = ""; this.TxtResult.Text = ""; // UserName + Password string c = this.TxtUserName.Text.Trim() + this.TxtPassword.Text.Trim(); byte[] bplainData = Encoding.Unicode.GetBytes(c); //数据初始化,申请内存 //输入数据 IntPtr pbInData = Marshal.AllocHGlobal(bplainData.Length); Marshal.Copy(bplainData, 0, pbInData, bplainData.Length); //输出数据内存单元 IntPtr pbOutData = Marshal.AllocHGlobal(OUT_HASH_DATA_LENGTH); //数据数据的实际大小 IntPtr iOutSize = Marshal.AllocHGlobal(sizeof(int)); //调用SenseLock int iReturn = SenseLock.SenseLock_Hash(pbInData, bplainData.Length, pbOutData, OUT_HASH_DATA_LENGTH, iOutSize); this.TxtReturn.Text = iReturn.ToString(); if (iReturn == 0 ) { //处理返回结果 byte[] bResult = new byte[OUT_HASH_DATA_LENGTH]; Marshal.Copy(pbOutData, bResult, 0, OUT_HASH_DATA_LENGTH); this.TxtResult.Text = Convert.ToBase64String(bResult); } //释放内存 Marshal.FreeHGlobal(pbInData); Marshal.FreeHGlobal(pbOutData); Marshal.FreeHGlobal(iOutSize); }