Search code examples
c#windows-ce

DeviceIoControl (IOCTL_NDISUIO_OPEN_DEVICE) is failed in WINCE7 C#.net


I am working on WIN CE platform, developing windows forms in C#.Net. Successfully created handle for NDISUIO ("UIO1:").

Check the API:

                string AUB_NAME = "PCI\\ManiXX1";

                byte[] toBytes = Encoding.ASCII.GetBytes(AUB_NAME);              

                int IOCTL_NDIS_QUERY = new int();
                IOCTL_NDIS_QUERY = IOCTL_NDISUIO_OPEN_DEVICE;

                IoctlResult = DeviceIoControl(
                                    hFileHandle,
                                    IOCTL_NDIS_QUERY,
                                    toBytes,
                                    (int)(11 * sizeof(UInt16)),//It should be 11 or 22 bytes?
                                    null,
                                    0,
                                    ref dwReturnedBytes,
                                    0);

The above syntax is corresponding to the first prototype as mentioned below.

Prototype for the Above API:

//deviceIoControl - overloaded
    [DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool DeviceIoControl(int hDevice, int dwIoControlCode, 
                                              byte[] InBuffer, int nInBufferSize,
                                              byte[] OutBuffer, int nOutputBufferSize, 
                                              ref int pBytesReturned, int pOverlapped);

When I run this code in WIN CE machine I am getting the error code 87, (The parameter is incorrect.ERROR_INVALID_PARAMETER)

I am checking the parameters, I feel those are correct, can anybody tell me regarding the parameters info and its type, which prototype should be followed for deviceiocontrol API in C#.net Compact framework?


Solution

  • This looks wrong to me:

    byte[] toBytes = Encoding.ASCII.GetBytes(AUB_NAME);
    

    Windows CE is very, very heavily biased toward Unicode. It should probably be:

    byte[] toBytes = Encoding.Unicode.GetBytes(AUB_NAME);