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);
}

Re: Not unsafe P/Invoke by ctacke/>

ctacke/>
Tue Dec 26 10:42:43 CST 2006

Yes, a ref short is the same as a UInt16 *


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


"skindiver" <xd@ttt.com> wrote in message
news:ep2SjpiJHHA.3268@TK2MSFTNGP04.phx.gbl...
>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);
> }
>
>