问题
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