问题描述
我正在使用 COM 互操作到我们自己的 C++ COM DLL 并有如下方法
[id(1)] HRESULT EncryptData([in,size_is(cbDataLength)] BYTE *lpbData,[in] ULONG cbDataLength,[in] BSTR Key,[out,size_is(,*pcbDataEnc)] BYTE **ppbDataEnc,[out] ULONG * pcbDataEnc);
该方法分配数据并通过ppbDataEnc参数传出去。
当我通过 Interop 包装它时,它给了我(在 C# 中)
[dispId(1)] void EncryptData(ref byte lpbData,uint cbDataLength,string Key,IntPtr ppbDataEnc,out uint pcbDataEnc);
ppbDataEnc 已被解释为顶级指针 (IntPtr)
我们需要做的是将其放入 C# 字节数组 (byte[])
我已经使用以下代码运行它,只是想知道这是最好的方法还是 .NET 提供了更好的方法?
// Sample data to encrypt
byte[] test = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10 };
IntPtr ppData = Marshal.AllocHGlobal(4); // allocate space for the 32bit data pointer
uint cbData = 0;
// Encrypt the string
dataCrypt.EncryptData3DES(ref test[0],10,"012345678901234567890",ppData,out cbData);
// Allocate the byte array to receive the encrypted data
byte[] encryptedData = new byte[cbData];
unsafe
{
fixed (byte* pEncData = encryptedData)
{
byte* ptr = *((byte**)ppData);
Buffer.Memorycopy((void*)ptr,(void*)pEncData,cbData,cbData);
}
}
谢谢 凯文
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)