【问题标题】:Calling a dll with a void* from C#从 C# 调用带有 void* 的 dll
【发布时间】:2018-06-18 19:39:59
【问题描述】:

我已经阅读了大部分提示,但我无法让它发挥作用。

我有一个带有这个原型的原生 C dll:

int utl_Conv_HexString(U8 u8_Mode, void* DataIn, void* DataOut, int *piInOutLen, int maxOutLen);

这个 dll 转换字节数组中的几种字符串格式:

该dll用于具有非托管代码(用C编写)的系统中

现在我想在 C#/WPF 环境中使用这个 dll。

我仍然在 C# 中使用其他 dll,但都有没有 void* 的原型。

来自 C 的示例:

//ByteArr to Telegramm

u8_Dst[0] = 0xAA;
u8_Dst[1] = 0xBB;
u8_Dst[2] = 0xCC;

u32_InOutLen = 3;
s32_res = utl_Conv_HexString(UTL_CONV_BYTEARR_TO_TELEGRAM, u8_Dst, ac8_Src, &u32_InOutLen, sizeof(ac8_Src));

strcpy(ac8_Src, "0xAA,0xBB,0xCC");
memset(u8_Dst, 0, sizeof(u8_Dst));
s32_res = utl_Conv_HexString(UTL_CONV_TELEGRAM_TO_BYTEARR, ac8_Src, u8_Dst, &u32_InOutLen, sizeof(u8_Dst));

我的问题是我不知道如何在 C# 中使用它

【问题讨论】:

  • byte[] 用于void*ref int 用于int*

标签: c# c void


【解决方案1】:

你应该像这样使用它:

public enum U8
{
    UTL_CONV_BYTEARR_TO_TELEGRAM = 1, // TODO
    UTL_CONV_TELEGRAM_TO_BYTEARR = 2,
}

(你必须把你的常量放在这里...)

[DllImport("SomeDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int utl_Conv_HexString(U8 u8Mode, byte[] dataIn, byte[] dataOut, ref int piInOutLen, int maxOutLen);

(注意CallingConvention 可能是StdCall...你必须检查你的代码)

然后:

byte[] src = Encoding.UTF8.GetBytes("0xAA, 0xBB, 0xCC");
byte[] dest = new byte[64];
int lenSrc = src.Length;
int res = utl_Conv_HexString(U8.UTL_CONV_TELEGRAM_TO_BYTEARR, src, dest, ref lenSrc, dest.Length);

void* 通常被翻译成byte[]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多