-1

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

arya2arya
  • 291
  • 2
  • 21
  • According to the docs they are supported. What does `struct_Obj` look like? – David Heffernan Mar 19 '14 at 09:36
  • struct_obj is the object for PNIC_STATISTICS structure in nuiouser.h – arya2arya Mar 19 '14 at 10:04
  • Yes. But what does your translation look like? – David Heffernan Mar 19 '14 at 10:05
  • I need to convert the structure to a pointer and have to send the pointer to DEviceIO control API please check my Qn here [DeviceIoControl (IOCTL_NDISUIO_NIC_STATISTICS) is failed in WINCE7 C#.net](http://stackoverflow.com/questions/22479009/deviceiocontrol-ioctl-ndisuio-nic-statistics-is-failed-in-wince7-c-net) – arya2arya Mar 19 '14 at 10:07
  • yes, we can modify the prototype, but it should match with the corresponding IOCTL in the Win CE driver.. so I need to send the structure pointer to the DeviceIoControl. – arya2arya Mar 19 '14 at 10:43
  • What I am proposing does exactly that – David Heffernan Mar 19 '14 at 10:47
  • yup I modified the Declaration to support for structure objects but getting exception: Not supported exception and last error code 87 in winCE machine – arya2arya Mar 19 '14 at 11:09
  • I cannot comment with seeing the code. Show an SSCCE please/. – David Heffernan Mar 19 '14 at 11:16
  • It has the example both in C++ and C# (my code) [Read_Code](http://stackoverflow.com/questions/22479009/deviceiocontrol-ioctl-ndisuio-nic-statistics-is-failed-in-wince7-c-net) – arya2arya Mar 19 '14 at 11:23
  • That is not an SSCCE. It has partial code. And it's at a different place. Code should stand alone. – David Heffernan Mar 19 '14 at 11:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50032/discussion-between-arya2arya-and-david-heffernan) – arya2arya Mar 19 '14 at 12:53
  • 1
    You're trying to hard to have the marshaler do whatcan be done by hand. The size is simple, just manually calculate it since it's not going to be changing. (15 * 4) + (6 * 8) – ctacke Mar 19 '14 at 16:36
  • 1
    And why the shenanigans on `StructToPtr`? Just pass it to the API as a `ref __NIC_STAT` and you're done. – ctacke Mar 19 '14 at 16:39

1 Answers1

1

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Sir, Whether WIN CE Platform will support these APIs?? Marshal.SizeOf() & Marshal.StructureToPtr() I am trying to calculate the struct size & converting to Pointer. any other way of sending the struct info to DeviceIoControl? – arya2arya Mar 19 '14 at 13:05
  • I'm not sure. I'm not a user of WinCE so I only know what I can read in the docs. – David Heffernan Mar 19 '14 at 13:06
  • 1
    Yeah, the CF isn't going to like that MarshalAs. – ctacke Mar 19 '14 at 16:36
  • 1
    @David: yep. No other option (well it could be done unsafe with a pointer, or a uint, but you understand). – ctacke Mar 19 '14 at 16:44
  • 1
    `StringToHGlobalUni` is also unsupported in the CF. I posted a solution in the duplicate of this question (before I realized you had answered over here) – ctacke Mar 19 '14 at 17:16
  • @DavidHeffernan can you please clarify my doubt [Conversion of C/C++ Struct To C#.Net CF WinCE](http://stackoverflow.com/questions/22560958/conversion-of-c-c-struct-to-c-net-cf-wince) – arya2arya Mar 21 '14 at 14:03
  • @ctacke According to http://msdn.microsoft.com/en-us/library/ms172512.aspx `MarshalAs(UnmanagedType.LPWStr)` is supported – David Heffernan Mar 21 '14 at 14:16
  • Certainly an interesting article. Sometimes MSDN is wrong, and sometimes I am (not sure who has a better track record). If it is supported, I've never used it. Now that may well be because I've been doing CF work since pre 1.0 and I just got used to the marshaler sucking ass and having to do everything by hand, and therefor continued doing it manually even after they added some features (this would likely be a 3.5 addition because I know if didn't work in 2.0 when I spent a month doing WZC work that would have used this). It would certainly be worth the OP's time to test this possibility. – ctacke Mar 21 '14 at 17:18
  • @DavidHeffernan [Sir Can you please suggest me for this Marshalling C Struct with array of structures in it *C#.Net CF for WIN CE](http://stackoverflow.com/questions/22964241/marshalling-c-struct-with-array-of-structures-in-it-c-net-cf-for-win-ce) – arya2arya Apr 09 '14 at 13:53