My program is a virtual USB bus driver to control the scanners on the LAN. My question is how to process the asynchronous calls to DeviceIoControl coming from the user mode programs. The codes in the caller are like below:
HANDLE Handle = CreateFile(“\\.\Usbscan0”, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
If (Handle != INVALID_HANDLE_VALUE)
{
OVERLAPPED overlapped;
BYTE byValue;
DWORD bytes, result;
memset(&overlapped, 0, sizeof(OVERLAPPED));
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
byValue = 0;
ResetEvent(overlapped.hEvent);
DeviceIoControl(m_hInterrupt, (DWORD) IOCTL_WAIT_ON_DEVICE_EVENT, NULL, 0, &byValue, 1, &bytes, &overlapped);
result = WaitForSingleObject(overlapped.hEvent, 200);
CancelIo(Handle);
CloseHandle(Handle);
}
What happened to my program is that the above program is blocked on DeviceIoControl until the request is completed by the bus driver.
For the scanner being connected on the PC and on the LAN, IrpTracker shows the differences, about executing DeviceIoControl, as below:
PC:
NTAPI, NtDeviceIoControlFile, Scanner Utility, \Device\Usbscan0, DEVICE_CONTROL
Call, 0x859568C0-37180, (UNKNOWN), \Device\Usbscan0, DEVICE_CONTROL
Call, 0x859568C0-37180, \Device\Usbscan0, \Device\USBPDO-2, INTERNAL_DEVICE_CONTROL
Call, 0x859568C0-37180, \Device\USBPDO-2, \Device\USBPDO-1, INTERNAL_DEVICE_CONTROL
NTAPIRet, NtDeviceIoControlFile, Scanner Utility, \Device\Usbscan0, DEVICE_CONTROL, PENDING
LAN:
NTAPI, NtDeviceIoControlFile, Scanner Utility, \Device\Usbscan0, DEVICE_CONTROL
Call, 0x848D6C50-58, (UNKNOWN), \Device\Usbscan0, DEVICE_CONTROL
Call, 0x848D6C50-58, \Device\Usbscan0, (0x84C8E9A0)\Driver\Usb2Net, INTERNAL_DEVICE_CONTROL
It seems the system bus driver, used when connected on the PC, does not return immediately. Am I right? I really do not know what’s wrong in my program to make the flow different. Any suggestions would be greatly appreciated.
Thanks.