gpt4 book ai didi

c# - 如何在 C# 中编码这个嵌套的、可变长度的 C 结构

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:50 25 4
gpt4 key购买 nike

typedef struct pt_bir {
PT_BIR_HEADER Header;
BYTE Data[1]; //variable length based on pt_bir_header.Length
} PT_BIR

typedef struct pt_bir_header {
DWORD Length;
BYTE HeaderVersion;
BYTE Type;
WORD FormatOwner;
WORD FormatID;
CHAR Quality;
BYTE Purpose;
DWORD FactorsMask;
} PT_BIR_HEADER

我的 C 函数是:

PT_STATUS LoadFinger (
IN PT_CONNECTION hConnection,
IN PT_LONG lSlotNr,
IN PT_BOOL boReturnPayload,
OUT PT_BIR **ppStoredTemplate
)

现在我需要在 C# 中对上述 C 函数进行包装。

我应该如何编码 PT_BIR** 结构以及在该函数返回后我应该如何解编它?

请帮帮我...

最佳答案

您将需要手动解码它。首先在 C# 中声明 header 结构

[StructLayout(LayoutKind.Sequential)]
public struct PT_BIR_HEADER
{
public int Length;
public byte HeaderVersion;
public byte Type;
public ushort FormatOwner;
public ushort FormatID;
public char Quality;
public byte Purpose;
public uint FactorsMask;
}

然后对于函数声明,声明 ppStoredTemplate 参数,如下所示:

out IntPtr ppStoredTemplate

一旦函数返回并且您有 ppStoredTemplate,您就可以解码它。首先拉出表头:

PT_BIR_HEADER header = (PT_BIR_HEADER)Marshal.PtrToStructure(ppStoredTemplate, 
typeof(PT_BIR_HEADER));

然后你就可以解压数据了:

byte[] data = new byte[header.Length];
Marshal.Copy(ppStoredTemplate + Marshal.SizeOf(typeof(PT_BIR_HEADER)), data, 0,
header.Length);

关于c# - 如何在 C# 中编码这个嵌套的、可变长度的 C 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27186722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com