Not unsafe P/Invoke

  • Thread starter Thread starter skindiver
  • Start date Start date
S

skindiver

I am passing a struct to unmanaged code. One of the parameters contains a
pointer that should point to a local variable to return some 16 bit data.
I can do this in an "unsafe" context and it works.
Is there a way to do the same thing without using "unsafe"?

[StructLayout(LayoutKind.Sequential)]
private struct MyParameters2
{
public Int32 DeviceId;
public Int32 Message;
public Int32 User;
public Int32 Parameter1;
public UInt16 *Parameter2;
}


public UInt16 GetIODataRegister()
{
MyParameters2 wp = new MyParameters2();
UInt16 data;
byte[] outbuffer = new byte[4];
wp.Message = WPDM_PRIVATE_READ;
wp.Parameter1 = IODATA;
wp.Parameter2 = &data;
this.DeviceIoControl(IOCTL_MY_MESSAGE, SerializeToByteArray(wp),
outbuffer);
if (BitConverter.ToUInt32(outbuffer, 0) != 0)
{
throw new
System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "IOControl
call failed");
}
return(data);
}
 
Back
Top