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

柔情痞子 提交于 2019-12-24 08:37:01

问题


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?


回答1:


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



回答2:


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.


来源:https://stackoverflow.com/questions/22454350/deviceiocontrol-ioctl-ndisuio-open-device-is-failed-in-wince7-c-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!