Marshal.StructureToPtr throws exception

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The following snippet throws a "ArgumentOutOfRangeException" exception in the
last line--
Int32 sizeWo=Marshal.SizeOf(wo);
ptrUWO = Marshal.AllocHGlobal(sizeWo*2);
Marshal.StructureToPtr(wo, ptrUWO, true);

It occurs if the last parameter in StructureToPtr is false as well.
I am using the OpenNetcf code in the actual app but it occurs if you use the
available Netcf methods as well.

The parameter "wo" is defined as--
Win32Com.OVERLAPPED wo = new Win32Com.OVERLAPPED();

OVERLAPPED is defined as follows--
[StructLayout( LayoutKind.Sequential )] internal struct OVERLAPPED
{
internal UIntPtr Internal;
internal UIntPtr InternalHigh;
internal UInt32 Offset;
internal UInt32 OffsetHigh;
internal IntPtr hEvent;
}

Any suggestions would be appreciated.
 
Does this mean that the approach and classes used in the Msdn article "Use
P/Invoke to Develop a .NET Base Class Library for Serial Device
Communications" cannot be used in Netcf?

If they cannot be used is there another recourse. I realize there are serial
IO classes in OpenNetcf but this would mean re-writing the module. Since this
is migration of code from Net to Netcf I would like to avoid this if possible.

I also realize that Net2.0 has serial IO classes but the management does not
yet want us to transistion to Net2.0 at this point.

Any suggestions would be appreciated.



--
John Olbert



Graham McKechnie said:
John

There is no overlapped in CE serial

Graham
John Olbert said:
The following snippet throws a "ArgumentOutOfRangeException" exception in
the
last line--
Int32 sizeWo=Marshal.SizeOf(wo);
ptrUWO = Marshal.AllocHGlobal(sizeWo*2);
Marshal.StructureToPtr(wo, ptrUWO, true);

It occurs if the last parameter in StructureToPtr is false as well.
I am using the OpenNetcf code in the actual app but it occurs if you use
the
available Netcf methods as well.

The parameter "wo" is defined as--
Win32Com.OVERLAPPED wo = new Win32Com.OVERLAPPED();

OVERLAPPED is defined as follows--
[StructLayout( LayoutKind.Sequential )] internal struct OVERLAPPED
{
internal UIntPtr Internal;
internal UIntPtr InternalHigh;
internal UInt32 Offset;
internal UInt32 OffsetHigh;
internal IntPtr hEvent;
}

Any suggestions would be appreciated.
 
Refactor the original to not use Overlapped, but passed IntPtr.Zero instead.
Delete the OVERLAPPED struct definition, change the CreateFile P/Invoke,
compile, then fix the errors. It should be a pretty painless change.

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate


John Olbert said:
Does this mean that the approach and classes used in the Msdn article "Use
P/Invoke to Develop a .NET Base Class Library for Serial Device
Communications" cannot be used in Netcf?

If they cannot be used is there another recourse. I realize there are
serial
IO classes in OpenNetcf but this would mean re-writing the module. Since
this
is migration of code from Net to Netcf I would like to avoid this if
possible.

I also realize that Net2.0 has serial IO classes but the management does
not
yet want us to transistion to Net2.0 at this point.

Any suggestions would be appreciated.



--
John Olbert



Graham McKechnie said:
John

There is no overlapped in CE serial

Graham
John Olbert said:
The following snippet throws a "ArgumentOutOfRangeException" exception
in
the
last line--
Int32 sizeWo=Marshal.SizeOf(wo);
ptrUWO = Marshal.AllocHGlobal(sizeWo*2);
Marshal.StructureToPtr(wo, ptrUWO, true);

It occurs if the last parameter in StructureToPtr is false as well.
I am using the OpenNetcf code in the actual app but it occurs if you
use
the
available Netcf methods as well.

The parameter "wo" is defined as--
Win32Com.OVERLAPPED wo = new Win32Com.OVERLAPPED();

OVERLAPPED is defined as follows--
[StructLayout( LayoutKind.Sequential )] internal struct OVERLAPPED
{
internal UIntPtr Internal;
internal UIntPtr InternalHigh;
internal UInt32 Offset;
internal UInt32 OffsetHigh;
internal IntPtr hEvent;
}

Any suggestions would be appreciated.
 
John,

You can modify the existing code, to not use Overlapped.

Graham


John Olbert said:
Does this mean that the approach and classes used in the Msdn article "Use
P/Invoke to Develop a .NET Base Class Library for Serial Device
Communications" cannot be used in Netcf?

If they cannot be used is there another recourse. I realize there are
serial
IO classes in OpenNetcf but this would mean re-writing the module. Since
this
is migration of code from Net to Netcf I would like to avoid this if
possible.

I also realize that Net2.0 has serial IO classes but the management does
not
yet want us to transistion to Net2.0 at this point.

Any suggestions would be appreciated.



--
John Olbert



Graham McKechnie said:
John

There is no overlapped in CE serial

Graham
John Olbert said:
The following snippet throws a "ArgumentOutOfRangeException" exception
in
the
last line--
Int32 sizeWo=Marshal.SizeOf(wo);
ptrUWO = Marshal.AllocHGlobal(sizeWo*2);
Marshal.StructureToPtr(wo, ptrUWO, true);

It occurs if the last parameter in StructureToPtr is false as well.
I am using the OpenNetcf code in the actual app but it occurs if you
use
the
available Netcf methods as well.

The parameter "wo" is defined as--
Win32Com.OVERLAPPED wo = new Win32Com.OVERLAPPED();

OVERLAPPED is defined as follows--
[StructLayout( LayoutKind.Sequential )] internal struct OVERLAPPED
{
internal UIntPtr Internal;
internal UIntPtr InternalHigh;
internal UInt32 Offset;
internal UInt32 OffsetHigh;
internal IntPtr hEvent;
}

Any suggestions would be appreciated.
 
Back
Top