【问题标题】:C# Marshalling unsigned char* array from C++ DLL (in and out)C# Marshalling unsigned char* array from C++ DLL (in and out)
【发布时间】:2015-01-22 19:54:45
【问题描述】:

我在 C# 应用程序和现有 C++ DLL 之间编组数据时遇到问题。使这变得困难的警告是它是一个指向数组的无符号字符指针,我需要在 C# 中调用之后访问数据(来自所有字段)。如果可能的话,我想避免使用不安全的代码。

这是 C++ 签名:

BYTE GetData(unsigned char *Data_Type, unsigned char *Data_Content, unsigned int *Data_Length);

我在 C# 中尝试了很多东西,但现在是这样:

[DllImport("somecpp.dll")]
public static extern byte GetData([In][Out] ref byte Data_Type, [In][Out] ref byte[] Data_Content, [In][Out] ref int Data_Length);

然后调用它,我正在尝试这个:

byte retrievedData = GetData(ref data_type, ref data_content, ref data_length);

这绝对行不通,我不确定接下来要尝试什么。有任何想法吗?谢谢!

【问题讨论】:

  • uint 将是 Data_Length 的正确类型,但我不知道为什么会导致此问题。与往常一样,在谈论无法正常工作的事情时,请具体说明,如果是,您是否收到错误消息。

标签: c# pinvoke marshalling dllimport


【解决方案1】:

您的 ref byte[] 参数与 unsigned char** 匹配。那是太多的间接级别。

p/invoke 应该是

[DllImport("somecpp.dll")]
public static extern byte GetData(
    ref byte Data_Type,
    [In,Out] byte[] Data_Content,
    ref uint Data_Length
);

该函数使用 cdecl 是合理的。我们不能从这里说出来。

我还怀疑Data_Type 参数应该是out 而不是ref

【讨论】:

    猜你喜欢
    • 2011-07-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2022-12-01
    • 2019-07-19
    • 2011-04-29
    相关资源
    最近更新 更多