如题有一个C#调用C++动态链接库的问题
原型:
原型:
BOOL WINAPI HsmOpen(UINT *phSocket); BOOL WINAPI HsmTripleDesCrypt(UINT hSocket,LPCSTR szKeyID,BYTE bEnc,WORD &len,BYTE *buf); void WINAPI HsmClose(UINT hSocket);
本人写的如下代码:
[DllImport("ZcPbocHsm.dll")] public static extern unsafe bool HsmOpen(byte[] phSocket); [DllImport("ZcPbocHsm.dll")] public static extern unsafe void HsmClose(uint hSocket); [DllImport("ZcPbocHsm.dll")] public static extern unsafe bool HsmTripleDesCrypt(uint hSocket, string szKeyID, byte bEnc, ref long len, Byte[] buf);
调用的时候是这样的:
rowstring = sr.ReadLine();// 获取当前行字符串 byte[] hsm = new byte[]{0}; bool a = HsmOpen(hsm); string keyID = "KEKEY"; Byte b = 0x01;//解密 string s = rowstring; Byte[] b2 = strToToHexByte(s); long len = b2.Length; uint hsm1 = uint.Parse(hsm[0].ToString()); bool c2 = HsmTripleDesCrypt(hsm1, keyID, b, ref(len), b2); HsmClose(hsm1);
这是一个连接加密机解密的过程,假如连本机测试是没问题的,但是一连真的加密机HsmTripleDesCrypt就会返回false,所以本人怀疑是不是端口那里参数类型还是不匹配?
那个 HsmOpen(hsm)的参数实际是个出参,但本人还是不知道哪里有问题···所以特来讨教大家~
望大家百忙之中帮看一下吧~~~谢谢啦~~~
:)
解决方案
30
BOOL WINAPI HsmOpen(UINT *phSocket);
C# =》public static extern bool HsmOpen(ref uint phSocket) ;
BOOL WINAPI HsmTripleDesCrypt(UINT hSocket,LPCSTR szKeyID,BYTE bEnc,WORD &len,BYTE *buf);
C#=》
public static extern bool HsmTripleDesCrypt(uint hSocket, string szKeyID, byte bEnc, ref ushort len, ref byte buf) ;
void WINAPI HsmClose(UINT hSocket);
C#=》public static extern void HsmClose(uint hSocket) ;
C# =》public static extern bool HsmOpen(ref uint phSocket) ;
BOOL WINAPI HsmTripleDesCrypt(UINT hSocket,LPCSTR szKeyID,BYTE bEnc,WORD &len,BYTE *buf);
C#=》
public static extern bool HsmTripleDesCrypt(uint hSocket, string szKeyID, byte bEnc, ref ushort len, ref byte buf) ;
void WINAPI HsmClose(UINT hSocket);
C#=》public static extern void HsmClose(uint hSocket) ;
10
[DllImport(“ZcPbocHsm.dll”)]
public static extern bool HsmOpen(ref uint phSocket);
[DllImport(“ZcPbocHsm.dll”)]
public static extern void HsmClose(uint hSocket);
[DllImport(“ZcPbocHsm.dll”)]
public static extern bool HsmTripleDesCrypt(uint hSocket, string szKeyID, byte bEnc, ref short len, Byte[] buf);
public static extern bool HsmOpen(ref uint phSocket);
[DllImport(“ZcPbocHsm.dll”)]
public static extern void HsmClose(uint hSocket);
[DllImport(“ZcPbocHsm.dll”)]
public static extern bool HsmTripleDesCrypt(uint hSocket, string szKeyID, byte bEnc, ref short len, Byte[] buf);