Why does DeviceIoControl fail with “Incorrect Function”

心已入冬 提交于 2019-12-11 05:39:43

问题


I am trying to communicate with my USB driver. I am able to get a handle, but once I use DeviceIoControl it fails, GetLastError() says error is an incorrect function. I am stumped on how to debug this. I am using XP 32bit machine.

Handle =     CREATEFILE(   DevicePath1,
                            GENERIC_READ | GENERIC_WRITE,
                 FILE_SHARE_READ,                                                        
                             NULL,
                             OPEN_EXISTING,
                             FILE_FLAG_OVERLAPPED,
                             NULL);
                    if (INVALID_HANDLE_VALUE == Handle)
                    {
                        printf("INVALIDHANDLE USB\n");
                        return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_DEVICE);
                    }
                    else
                    {

                        //  Call device IO Control interface (USB_TEST_IOCTL_VERSION_NUMBER) in driver
                        if ( !DeviceIoControl(Handle,
                                            USB_TEST_IOCTL_VERSION_NUMBER,
                                            NULL,
                                            0,
                                            version,
                                            sizeof(version),
                                            &lenght,
                                            NULL)
                        )
                        {

//Display the last error killing my program

void* lpBuffer;

FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
               NULL,
              GetLastError(),
              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
              (LPTSTR) &lpBuffer,
              0,
              NULL );
printf(" Version: %x\n", version);
printf("USB_TEST_IOCTL_VERSION_NUMBER, %x\n", USB_TEST_IOCTL_VERSION_NUMBER);
printf(" &lenght: %x\n", &lenght);
MessageBox( NULL, (LPCTSTR)lpBuffer, TEXT("LastRrror"), MB_OK );
LocalFree( lpBuffer );

            printf("USB HIO Control interface FAIL\n");
                            return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_DEVICE);

回答1:


The most likely cause (as Xearinox pointed out) is that the newer version of the device driver does not have that particular control code. You need to obtain updated documentation and/or header files from the vendor.

Also, you are opening an asynchronous handle and then trying to use it for synchronous I/O. From the documentation for DeviceIoControl:

If hDevice was opened with the FILE_FLAG_OVERLAPPED flag, the operation is performed as an overlapped (asynchronous) operation. In this case, lpOverlapped must point to a valid OVERLAPPED structure that contains a handle to an event object.



来源:https://stackoverflow.com/questions/11919165/why-does-deviceiocontrol-fail-with-incorrect-function

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