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

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

  •  15-06-2023
  •  | 
  •  

سؤال

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?

هل كانت مفيدة؟

المحلول

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);

نصائح أخرى

A bit late to the party here, but two other things to be aware of with IOCTL_NDISUIO_OPEN_DEVICE:

  1. The input buffer passed to DeviceIoControl must not be const, or the call will fail with error 87 (ERROR_INVALID_PARAMETER) as NDISUIO attempts to copy data back into the input buffer(!) So don't pass a string literal the way some MSDN samples do.
  2. The length of the input buffer must not include the terminating NUL. If it does, NDISUIO will fail to locate the adapter name in its internal list and return error 31 (ERROR_GEN_FAILURE). MSDN does point out that the NULL character should be disregarded, but the wording makes it sound optional when in reality it isn't.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top