Send IOCTL to Windows device driver - CreateFile failed

I want to send an IOCTL command to a PC / SC reader connected to my computer (win7 64 bit). To send an IOCTL command, I need a HANDLE for a device that I cannot create.

The device is listed as "OMNIKEY 1021" in the device manager, the name of the physical device is "\ Device \ USBPDO-15". Using the WinObj tool, I can detect 2 symbolic links: USB- # VID_076B & PID_1021 # 5 & 291f6990 & 0 & 1 # {50dd5230-ba8a-11d1-bf5d-0000f805f530} USB- # VID_076B & PID_1021 # 5 & 291f6990 0 & 1 # {a5dcbf10-6530-11d2-901f-00c04fb951ed}

My problem: I cannot create a valid handle to this device using the CreateFile function:

I found several possible formats on MSDN / Google to use the CreateFile function as the lpFileName parameter, but none of them work:

\\?\Device\USBPDO-15 \\.\Device\USBPDO-15 \\GLOBAL??\Device\USBPDO-15 \GLOBAL??\Device\USBPDO-15 \\.\USBPDO-15 \\?\USB#VID_076B&PID_1021#5&291f6990&0&1#{50dd5230-ba8a-11d1-bf5d-0000f805f530} \\.\USB#VID_076B&PID_1021#5&291f6990&0&1#{50dd5230-ba8a-11d1-bf5d-0000f805f530} \\GLOBAL??\USB#VID_076B&PID_1021#5&291f6990&0&1#{50dd5230-ba8a-11d1-bf5d-0000f805f530} \GLOBAL??\USB#VID_076B&PID_1021#5&291f6990&0&1#{50dd5230-ba8a-11d1-bf5d-0000f805f530} \\?\USB#VID_076B&PID_1021#5&291f6990&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed} \\.\USB#VID_076B&PID_1021#5&291f6990&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed} \\GLOBAL??\USB#VID_076B&PID_1021#5&291f6990&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed} \GLOBAL??\USB#VID_076B&PID_1021#5&291f6990&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed} 

Code example :

 #include <iostream> #include <Windows.h> int main (int argc, char* argv[]) { HANDLE handle = CreateFile ( L"\\\\.\\Device\\USBPDO-15", 0, FILE_SHARE_READ, //FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, //FILE_FLAG_OVERLAPPED, NULL ); if (handle == INVALID_HANDLE_VALUE) std::cout << "INVALID HANDLE" << std::endl; else std::cout << "HANDLE: " << std::hex << handle << std::endl; } 

Notes:

  • The returned handle is always invalid.
  • Always works as an administrator, so privileges should not be a problem

edit:

Decision:

  • The PC / SC service takes exclusive ownership of the devices, so any attempt to call "CreateFile" will always fail.
  • The solution is a kernel space driver, this allows you to pass IRP to the driver. (I was able to implement the KMDF filter driver to change the data sent / received to / from the device).
+8
c ++ windows device driver ioctl
source share
2 answers

Try your way. I use the Setup API to list all active USB devices in the system and get the paths. That way, you can find out if the path or other arguments that CreateFile do not like.

I will add a few comments a little later if anyone is interested.

 HDEVINFO hDevInfo = SetupDiGetClassDevs( &_DEVINTERFACE_USB_DEVICE, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); if(hDevInfo == INVALID_HANDLE_VALUE) { return ERR_FAIL; } std::vector<SP_INTERFACE_DEVICE_DATA> interfaces; for (DWORD i = 0; true; ++i) { SP_DEVINFO_DATA devInfo; devInfo.cbSize = sizeof(SP_DEVINFO_DATA); BOOL succ = SetupDiEnumDeviceInfo(hDevInfo, i, &devInfo); if (GetLastError() == ERROR_NO_MORE_ITEMS) break; if (!succ) continue; SP_INTERFACE_DEVICE_DATA ifInfo; ifInfo.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA); if (TRUE != SetupDiEnumDeviceInterfaces(hDevInfo, &devInfo, &(_DEVINTERFACE_USB_DEVICE), 0, &ifInfo)) { if (GetLastError() != ERROR_NO_MORE_ITEMS) break; } interfaces.push_back(ifInfo); } std::vector<SP_INTERFACE_DEVICE_DETAIL_DATA*> devicePaths; for (size_t i = 0; i < interfaces.size(); ++i) { DWORD requiredSize = 0; SetupDiGetDeviceInterfaceDetail(hDevInfo, &(interfaces.at(i)), NULL, NULL, &requiredSize, NULL); SP_INTERFACE_DEVICE_DETAIL_DATA* data = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc(requiredSize); assert (data); data->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &(interfaces.at(i)), data, requiredSize, NULL, NULL)) { continue; } devicePaths.push_back(data); } 
+7
source share

Just try CreateFile(L"\\\\.\\{GUID}",etc..

0
source share

All Articles