小编Ung*_*lad的帖子

C#中的struct marshal

我在C#中有以下结构

unsafe public struct control
    {
        public int bSetComPort;
        public int iComPortIndex;
        public int iBaudRate;
        public int iManufactoryID;
        public byte btAddressOfCamera;
        public int iCameraParam;
        public byte PresetNum;
        public byte PresetWaitTime;
        public byte Group;
        public byte AutoCruiseStatus;
        public byte Channel;
        public fixed byte Data[64];
    }
Run Code Online (Sandbox Code Playgroud)

我用来将它转换为字节数组[]的函数是

 static byte[] structtobyte(object obj)
    {
        int len = Marshal.SizeOf(obj);
        byte[] arr = new byte[len];
        IntPtr ptr = Marshal.AllocHGlobal(len);
        Marshal.StructureToPtr(obj, ptr, true);
        Marshal.Copy(ptr, arr, 0, len);
        Marshal.FreeHGlobal(ptr);
        return arr;
    }
Run Code Online (Sandbox Code Playgroud)

当我编译它给

Type 'System.Byte[]' cannot be marshaled …
Run Code Online (Sandbox Code Playgroud)

c# arrays interop struct marshalling

6
推荐指数
1
解决办法
3822
查看次数

标签 统计

arrays ×1

c# ×1

interop ×1

marshalling ×1

struct ×1