Send IOCTL to Windows device driver - CreateFile fails

后端 未结 2 977
感情败类
感情败类 2021-01-04 23:39

I want to send an IOCTL command to a PC/SC reader connected to my computer (win7 64 bit). In order to send an IOCTL command I need a HANDLE to the device, which I\'m unable

2条回答
  •  既然无缘
    2021-01-04 23:42

    Try it my way. I'm using Setup API to enumerate all USB active devices in the system and get paths. That way you can find out whether it's the path or other arguments that CreateFile doesn't like.

    I'll add some comments a bit later, if anyone's interested.

    HDEVINFO hDevInfo = SetupDiGetClassDevs( &_DEVINTERFACE_USB_DEVICE, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
        if(hDevInfo == INVALID_HANDLE_VALUE)
        {
            return ERR_FAIL;
        }
    
        std::vector 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 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);
        }
    

提交回复
热议问题