Marshal.SizeOf() & Marshal.StructureToPtr() Throwing Exceptions in .NET CF WIN-CE

StackOverflow https://stackoverflow.com/questions/22500622

  •  17-06-2023
  •  | 
  •  

Question

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!!

Était-ce utile?

La solution

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 with UnmanagedType.LPWStr.

So I would have your struct like this:

[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
public struct __NIC_STAT
{
    uint Size;   
    [MarshalAs(UnmanagedType.LPWStr)]        
    public string ptcDeviceName; 
    uint DeviceState;      
    uint DeviceState;      
    uint MediaType;        
    uint MediaState;       
    uint PhysicalMediaType;
    uint LinkSpeed;        
    ulong PacketsSent;
    ulong PacketsReceived;
    uint InitTime;      
    uint ConnectTime;   
    ulong BytesSent;    
    ulong BytesReceived; 
    ulong DirectedBytesReceived;
    ulong DirectedPacketsReceived;
    uint PacketsReceiveErrors;
    uint PacketsSendErrors;
    uint ResetCount;
    uint MediaSenseConnectCount;
    uint MediaSenseDisconnectCount; 
};

I'm not sure why Marshal.SizeOf is failing for you. You may need to declare ptcDeviceName as IntPtr and use Marshal.StringToHGlobalUni to set the value. That at least makes the struct blittable and if Marshal.SizeOf still fails then you can fall back on sizeof.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top