如何存储UINT8_T*变量从C#中的C 返回
How to store a uint8_t* variable returned from a C++ function in c#?
我正在从我的C#程序中调用C DLL。DLL由几个功能组成,我可以称其为大多数功能。
C 功能如下:
__declspec(dllexport) uint8_t* myHash(const char *filename)
{
uint8_t *hash = (unsigned char*)malloc(72*sizeof(uint8_t));
//some processing on hash
return hash;
}
可以在上述代码中看到,哈希函数存储一个字符数组。我想在我的C#程序中收到值,但我无法做到。
我的C#代码如下:
[DllImport("myHash.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr myHash(string filename);
IntPtr ptr = myHash(fileA);
char[] result = new char[72];
Marshal.Copy(ptr, result, 0, 72);
问题是C#中的char
是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 代码中导出Deallocator。我会像这样写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。这可能是合理的,但是也可以通过缓冲区的长度也有意义,以便无管理的代码可以防御缓冲区的超越。p>请注意,尽管您将此功能的输出称为字符数组,但使用uint8_t*
的使用使其似乎更有可能是字节数组。如果确实是字符数组,则可以使用Encoding.GetString()
转换为字符串。
相关文章:
- 算术运算的结果类似于:C浮点变量中的1/3
- 变量中的模板类型
- C++ Code 在 for 循环期间不会累积变量中的总和,仅提供最终迭代值
- 按字母顺序对结构内数组变量中的名称进行排序
- 我可以在初始化之前使用 std::array 成员变量中的 data() 指针吗?发出警告
- 返回实例变量的c++方法可以访问变量中的数据,但不能更改它,但在编译时不会生成错误
- 使用指针选择长无符号变量中的数字
- 为什么我的程序没有更改结构变量中的值?
- 野牛堆栈变量中的值分配
- 全局变量中的静态常量与常量
- Eclipse 编译器有没有办法将变量中的地址字段显示为整数值?
- 在 C++ 中通过字符串指针复制字符串变量中的字符串
- 比较 JSON::Value 变量中的数据,然后更新到文件
- 为什么存储在变量中的值比原始答案少 1?
- 为什么 al_draw_textf() 打印数字而不是存储在字符串变量中的字母
- 更改GTK ::输入存储在特定变量中的颜色
- 是否可以执行存储在变量中的机器代码
- 如何计算长长类型变量中的位数
- 如何有效地检测4个整数变量中的对称性
- 成员变量中的类模板参数扣除