I have the following structure in 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]; }
And the function that I use to convert to a byte array [] is
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; }
When I compile it, it gives
Type 'System.Byte[]' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
What could be the problem? Thanks in advance!
source share