Martin

Martin

  • NA
  • 2
  • 0

Marshal.Copy Struct -> ushort[]

Feb 23 2006 5:59 PM
Hi,

Here is a stuct that I have to fill using a dll function :

CODE

struct InfoRegister
{
   public uint TargetIdBitField;
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9) ]
   public char[] Label;
   public byte Mode;
   public byte Index;
   public byte CS;
}

The function signature :  (Standart for ModBus communication protocol)

CODE

public MODBUS_ERROR ReadRegisters( ..., ushort[] pun16RegArray );

I would like to use it like :

CODE

InfoRegister ir = new InfoRegister();
ReadRegisters( ir );
 
But how can I convert my struct to a ushort[] reference ? And after that, how can I convert my ushort[] to a InfoRegister struct ?

I can't use the UNSAFE mode for technical reasons. So I hope Marshal can do the trick ?

Thanks for you help.