1

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);
tenorsax
  • 21,123
  • 9
  • 60
  • 107
IPA8654
  • 23
  • 1
  • 4
  • And your driver code is where? – Xearinox Aug 12 '12 at 13:45
  • @Xearinox If I under stand you correctly, my control code `#define USB_TEST_IOCTL_VERSION_NUMBER CTL_CODE(FILE_DEVICE_UNKNOWN, 0x807, METHOD_BUFFERED, FILE_ANY_ACCESS) ` where `CTL_CODE` is`#define CTL_CODE( DeviceType, Function, Method, Access ) ( \ ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \ ) ` This is passed to the device driver and eventually to the USB. My driver is located under `WINDOWS\system32\DRIVERS\myDriver.sys` – IPA8654 Aug 12 '12 at 21:43
  • OK. But where is code for respond for this IOCTL in driver??? – Xearinox Aug 12 '12 at 21:58
  • The driver is provided by the vendor, I did not write any of the driver code. Are you referring to the `.inf` file? `DeviceIoControl` is part of Microsoft's `winapi` and after reading their API, I am not quite sure what `DeviceIoControl` actually communicates with. – IPA8654 Aug 12 '12 at 22:45
  • "I am trying to communicate with my USB driver" - Is not your driver??? – Xearinox Aug 13 '12 at 07:50
  • If is not your driver, device vendor not create routine for this I/O control in IRP_MJ_DEVICE_CONTROL. – Xearinox Aug 13 '12 at 08:04
  • My USB driver is the one I am using, not one I wrote. I am updating the portion of a SDK which communicates with a USB driver for a previous chipset. With the newer chipset, I have managed to update the SDK until I use `DeviceIoControl`. At this point the code returns `FALSE` and `GetLastError` says the error is an incorrect function. – IPA8654 Aug 14 '12 at 01:42
  • I answer you: Device vendor not create routine for this I/O control in IRP_MJ_DEVICE_CONTROL. – Xearinox Aug 16 '12 at 06:52

1 Answers1

0

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.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158