I am working on C#.Net CF for WIN-CE platform. In my code I am using
int size = Marshal.SizeOf(typeof(struct_Obj));
IntPtr newptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(struct_Obj, newptr, false);
I am trying to send this struct info:
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
public struct __NIC_STAT
{
ulong Size; // Of this structure.
public Char[] ptcDeviceName; // The device name to be queried..
ulong DeviceState; // DEVICE_STATE_XXX above
ulong DeviceState; // DEVICE_STATE_XXX above
ulong MediaType; // NdisMediumXXX
ulong MediaState; // MEDIA_STATE_XXX above
ulong PhysicalMediaType;
ulong LinkSpeed; // In 100bits/s. 10Mb/s = 100000
UInt64 PacketsSent;
UInt64 PacketsReceived;
ulong InitTime; // In milliseconds
ulong ConnectTime; // In seconds
UInt64 BytesSent; // 0 - Unknown (or not supported)
UInt64 BytesReceived; // 0 - Unknown (or not supported)
UInt64 DirectedBytesReceived;
UInt64 DirectedPacketsReceived;
ulong PacketsReceiveErrors;
ulong PacketsSendErrors;
ulong ResetCount;
ulong MediaSenseConnectCount;
ulong MediaSenseDisconnectCount;
} ;
when I run the code in WIN-CE machine, I am getting "not supported exception".Those two methods are throwing exceptions.
Can anybody tell me how to find the structure size and how to convert structure to Ptr with out any issues for WIN-CE.
Thanks!!
Your struct is declared incorrectly. The C++
ULONG
is a 32 bit unsigned type. But in C#,ulong
is 64 bits. That's clearly a huge problem.On top of that, I must admit to being slightly sceptical about using
char[]
in the way you do. I would do it as a string withUnmanagedType.LPWStr
.So I would have your struct like this:
I'm not sure why
Marshal.SizeOf
is failing for you. You may need to declareptcDeviceName
asIntPtr
and useMarshal.StringToHGlobalUni
to set the value. That at least makes the struct blittable and ifMarshal.SizeOf
still fails then you can fall back onsizeof
.