如题
原型:BOOL WINAPI HsmTripleDesCrypt(UINT hSocket,LPCSTR szKeyID,BYTE bEnc,WORD &len,BYTE *buf);
本人的代码:
一开始写成这样:
原型:BOOL WINAPI HsmTripleDesCrypt(UINT hSocket,LPCSTR szKeyID,BYTE bEnc,WORD &len,BYTE *buf);
本人的代码:
一开始写成这样:
[DllImport("JM.dll")] public static extern bool HsmTripleDesCrypt(uint hSocket, string szKeyID, Byte bEnc, int len, Byte[] buf); private unsafe void button2_Click(object sender, EventArgs e) { bool a = HsmOpen(0); string keyID = "KEKEY"; Byte b1 = 0x01;//解密 int len = 0x08; Byte[] b2 = { 31, 32, 33, 34, 35, 36, 37, 38 }; bool b = HsmTripleDesCrypt(0, keyID, b1, len, b2); HsmClose(0); }
一直报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
做了一些尝试改成下面这样 开始报句柄未初始化···
[DllImport("JM.dll")] public static extern unsafe bool HsmTripleDesCrypt(uint hSocket, string szKeyID, Byte bEnc, int len, IntPtr buf); private unsafe void button2_Click(object sender, EventArgs e) { bool a = HsmOpen(0); string keyID = "KEKEY"; Byte b1 = 0x01;//解密 int len = 0x08; Byte[] b2 = { 31, 32, 33, 34, 35, 36, 37, 38 }; //byte[] test = new byte[5]; GCHandle hObject = GCHandle.Alloc(b2, GCHandleType.Pinned); IntPtr pObject = hObject.AddrOfPinnedObject(); if (hObject.IsAllocated) hObject.Free(); //IntPtr b3 = b2; //IntPtr* b2 = &2; bool b = HsmTripleDesCrypt(0, keyID, b1, len, pObject); HsmClose(0); }
现在也不知道怎么改了··
麻烦高手帮帮忙吧~万分感谢~
解决方案
40
尝试用
HsmTripleDesCrypt(uint hSocket,string szKeyID,byte bEnc,short &len,byte[] buf);
HsmTripleDesCrypt(uint hSocket,string szKeyID,byte bEnc,short &len,byte[] buf);