Struct marshal in C#

前端 未结 2 2135
深忆病人
深忆病人 2021-02-09 22:17

I have the following struct in C#

unsafe public struct control
    {
        public int bSetComPort;
        public int iComPortIndex;
        public int iBaudRa         


        
相关标签:
2条回答
  • 2021-02-09 22:34

    SizeOf doesn't work on arrays. Use array.Length * Marshal.SizeOf(elementType) instead.

    0 讨论(0)
  • 2021-02-09 22:48

    The error you are reporting as a compile error is in fact a runtime error (an ArgumentException). When you want to use structtobyte to convert a control to byte[] you should pass the method a reference to control, not a byte array (byte[]).

    control ctrl = new control();
    byte[] bytes = structtobyte(ctrl);
    
    0 讨论(0)
提交回复
热议问题