问题是char
在C#中是一个16位字符元素。您的 C++ 代码返回一个 8 位数组uint8_t
价值观。您应该改用字节数组。
[DllImport("myHash.dll", CallingConvention=CallingConvention.Cdecl,
CharSet = CharSet.Ansi)]
public static extern IntPtr myHash(string filename);
....
IntPtr ptr = myHash(fileA);
byte[] result = new byte[72];
Marshal.Copy(ptr, result, 0, 72);
我指定了一个调用约定,因为正如所写,你的函数是__cdecl
。也许您在抄写问题时遗漏了某些内容,但上面的声明与问题中的非托管代码相匹配。
该函数的设计应该更好,以允许调用者分配缓冲区。这避免了您必须从 C++ 代码中导出释放器。我会这样写 C++:
__declspec(dllexport) int myHash(const char *filename, uint8_t* hash)
{
// calculate hash and copy to the provided buffer
return 0; // return value is an error code
}
以及相应的C#代码:
[DllImport("myHash.dll", CallingConvention=CallingConvention.Cdecl,
CharSet = CharSet.Ansi)]
public static extern int myHash(string filename, byte[] hash);
....
byte[] hash = new byte[72];
int retval = myHash(fileA, hash);
该函数在其接口中硬编码缓冲区长度为 72。这可能是合理的,但传递缓冲区的长度也可能有意义,以便非托管代码可以防止缓冲区溢出。
请注意,虽然您将此函数的输出称为字符数组,但使用uint8_t*
使它看起来更有可能是一个字节数组。如果它确实是一个字符数组,那么你可以使用Encoding.GetString()
转换为字符串。