c# - 在 C# 中处理 C++ 结构

标签 c# c++ struct arrays

我有一个关于在 C# 中处理来自 C++ 的结构的问题。

我有一个来自第三方的 C++ 结构,例如:

typedef struct 
{
//For application usage, will not access by the core
void* file;         //Application
void* lock;         //Application
void* appParam1;    //Application, maybe filename
unsigned int appParam2;     //Application, maybe length
void* appParam3;    //Application, maybe folder

//Will be used by the core and the hosting application
unsigned char streamMode;       //Read, Write or Append
unsigned int fileOffset;        //Actual file offset
unsigned char buf[DC_STREAM_BUF_SIZE];      //Buffer for reading and writing data
unsigned short bufSize;     //Buffer size, how much is filled from the buffer
unsigned short  bufOffset;      //Offset in reading and writing in the buffer
unsigned char queryData;        //Indicator if actual data are queried

//For internal use only, do not touch
unsigned char requestType;
signed int deviceId;
signed int param1;
unsigned short crc;
void* callback;
unsigned char objType;
void* obj;

} STREAM_HANDLE;

在 C# 中,此结构定义为:

[StructLayoutAttribute(LayoutKind.Sequential), Serializable]
public class STREAM_HANDLE
{

    public IntPtr file;         
    public object lockobj;      
    public IntPtr appParam1;

    [MarshalAsAttribute(UnmanagedType.U4)] public uint appParam2;
    public IntPtr appParam3;

    [MarshalAsAttribute(UnmanagedType.U1)]public byte streamMode;

    [MarshalAsAttribute(UnmanagedType.U4)]public uint fileOffset;       

    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 0x0FFF)]public byte[] buf;

    [MarshalAsAttribute(UnmanagedType.U2)]public ushort bufSize;
    [MarshalAsAttribute(UnmanagedType.U2)]public ushort bufOffset;  
    [MarshalAsAttribute(UnmanagedType.U1)]public byte queryData;

    //For internal use only, do not touch
    [MarshalAsAttribute(UnmanagedType.U1)]public byte requestType;
    [MarshalAsAttribute(UnmanagedType.I4)]public int deviceId;
    [MarshalAsAttribute(UnmanagedType.I4)]public int param1;
    [MarshalAsAttribute(UnmanagedType.U2)]public ushort crc;

    public IntPtr callback;
    [MarshalAsAttribute(UnmanagedType.U1)]public byte objType;
    public IntPtr obj;
}

我在 C# 中也有这个结构的 IntPtr,这样我就可以用这个指针调用 C++ 函数。这非常有效。

现在的问题是我想从 c# 中填充数组“buf”,以便 c++ 函数可以读取它。 这不起作用。

这是我用来在 C# 中创建句柄的更多代码

STREAM_HANDLE Handle = new STREAM_HANDLE
    {
        file = Marshal.StringToHGlobalAnsi(name),
        appParam1 = Marshal.StringToHGlobalAnsi(name),
        buf = new byte[0x0FFF],
        lockobj = new object(),
    };

 IntPtr HandlePointer = Marshal.AllocHGlobal(Marshal.SizeOf(Handle));
 Marshal.StructureToPtr(Handle, HandlePointer, true);

我必须使用 HandlePointer 调用一些 c++ 函数,并且我必须修改 c# 中“buf”的内容。

最佳答案

MarshalAs 属性将仅确保 buf 字段在使用 Marshal.AllocHGlobal 分配时将被编码到内联数组。该结构的 C# 表示有一个常规的 .Net 数组引用作为字段。您已经像往常一样分配和初始化它。 如果您想按照自己的方式使用它,则必须在 C# 表示和非托管句柄之间来回切换。

另一种方法是将其声明为固定 数组。这仅在 unsafe 上下文中才有可能:

unsafe struct InplaceArrayUnsafe
{
    public fixed byte Buf[10];
}

unsafe
{
    InplaceArrayUnsafe a;
    Console.WriteLine(a.Buf[4]);
    Debug.Assert(sizeof(a)); // can even do this
    UnmanagedFunction(&a); // and this
}

关于c# - 在 C# 中处理 C++ 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14334802/

相关文章:

c# - 保存发送给机器人的用户消息并将完成的表格发送给其他用户

c++ - 在 OpenCV 中隐藏鼠标光标

c - 在C编程中,有没有办法在子结构体(结构体中的结构体)中获取函数的返回值?

c++ - 对结构进行索引是否合法?

c - 解释 c 结构

c - 为结构数组元素(字符串)分配内存

c# - 将 Get/Set 属性重写为 Get-Only 仍允许您设置它

c# - 我需要显示我的 ListViewItems 的整个文本(未截断)

c# - EndDraw() 在 Direct2D 中占用 80% 的工作时间

c++ - 一旦上下文打开,纹理仅适用于 compat 和 glGetError Always INVALID_ENUM