Question

I am converting c/c++ structures into C# standards.

C/C++ Structures:

 typedef struct _sta_conn_info{
STA_CONNECT_STATE               connect_state;//Enum
STA_ASSOC_STATE                 assoc_state;//Enum
unsigned char               bssid[6];
unsigned char               ssid[34];   
unsigned long               channel;
enum mode               mode;//Enum
unsigned long               signalStrength; 
    unsigned long               noiseLevel;     
STA_AUTH_ALG                    auth_alg;//enum
STA_ENCRYPT_ALG                 encrypt_alg;//enum  
}STA_CONN_INFO;


 typedef struct _NDISUIO_QUERY_OID
         {
           NDIS_OID        Oid;
           PTCHAR          ptcDeviceName;  
           UCHAR           Data[sizeof(ULONG)];
         } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;

Respective C# structures:

     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct _sta_conn_info
    {
        public _sta_connect_state       connect_state;      
        public _sta_assoc_state         assoc_state; 

            [MarshalAs(UnmanagedType.ByValArray,SizeConst = 6)]
        public char[]               bssid ;//= new char[6];

            [MarshalAs(UnmanagedType.ByValArray,SizeConst = 34)]
        public char[]               ssid ;//= new char[34]  

        public uint         channel;
        public mode             mode;
        public uint             signalStrength; 
            public uint             noiseLevel;     
        public _sta_auth_alg            auth_alg;
        public _sta_encrypt_alg         encrypt_alg;
         }

    QUERY STRUCT:   
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

    public struct _NDISUIO_QUERY_OID
    {
        public uint        Oid;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string          ptcDeviceName;
        public byte[] Data;
    };

I converted the data types using this >>>reference

Marshal.SizeOf() is working in WIN CE. I tested it.

If my structure conversion is fine then definitely Marshal.SizeOf() will work to get the size of the structure, but it is throwing exceptions and returning error code 87 in DeviceIoControl() API.

Can anyone clarify me about the conversions and let me know If I did anything wrong.

Was it helpful?

Solution

For bssid and ssid the C++ declarations are:

unsigned char bssid[6];
unsigned char ssid[34];   

Now, unsigned char is a single byte and is typically used for byte arrays rather than text. So the C# should be:

[MarshalAs(UnmanagedType.ByValArray,SizeConst = 6)]
public byte[] bssid ;//= new byte[6];

[MarshalAs(UnmanagedType.ByValArray,SizeConst = 34)]
public byte[] ssid ;//= new byte[34]  

Your use of char in the C# is not correct because char is two bytes wide in C#.

In _NDISUIO_QUERY_OID where you have

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = sizeof(uint))]
public byte[] Data;

I believe that you need to use ByValArray rather than ByValTStr. But as we have discussed in many of your recent questions, the exact meaning of this member is unclear. Is it really a fixed length byte array, or is it a variable length buffer? Do you have sample C++ code that works? That would settle the debate once and for all.


OK, from the header nuiouser.h header file I have this:

//
//  Structure to go with IOCTL_NDISUIO_QUERY_OID_VALUE.
//  The Data part is of variable length, determined by
//  the input buffer length passed to DeviceIoControl.
//
typedef struct _NDISUIO_QUERY_OID
{
    NDIS_OID        Oid;

#ifdef UNDER_CE
    //
    //  In CE land app is allowed to query without having to do
    //  IOCTL_NDISUIO_OPEN_DEVICE
    //  Hence the device name to query argument needed..
    //  For app that does IOCTL_NDISUIO_OPEN_DEVICE this argument
    //  is then not necessary..
    //

    PTCHAR          ptcDeviceName;  
#endif

    UCHAR           Data[sizeof(ULONG)];

} NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;

Which tells you conclusively that Data is variable length. You'll need to allocate the struct with AllocHGlobal and do all the marshalling by hand I am afraid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top