Hi All!
I try to communicate with USB devices. I successfully get a device path for calling CreateFile() to communicate with USB device. But I have trouble with calling ReadFile() and WriteFile(). The calling of these functions always end with 5 code error.
I don't understand what I do wrong. Maybe the drivers of my devices just don't allow to communicate with device or I made a mistake in my code.
Please help.
There is my programming code:
Thank you much for your consideration.Code:void _tmain(int argc, _TCHAR* argv[])
{
//GUID hidGuid = CLSID_NULL; // Disk Device.
//CLSIDFromString(L"{53f56307-b6bf-11d0-94f2-00a0c91efb8b}", &hidGuid);
GUID hidGuid = CLSID_NULL; // Human Interface Device (HID).
HidD_GetHidGuid(&hidGuid);
//GUID hidGuid = CLSID_NULL; // USB Device.
//CLSIDFromString(L"{36FC9E60-C465-11CF-8056-444553540000}", &hidGuid);
//GUID hidGuid = CLSID_NULL; // USB Raw Device.
//CLSIDFromString(L"{A5DCBF10-6530-11D2-901F-00C04FB951ED}", &hidGuid);
HDEVINFO devInfo = SetupDiGetClassDevs(&hidGuid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for(int i = 0; SetupDiEnumDeviceInfo(devInfo, i, &devInfoData); i++)
{
//printf("Iteration Number: %d\n", i);
//ShowDeviceProperty(devInfo, devInfoData);
SP_DEVICE_INTERFACE_DATA devInterfaceData;
devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for(int j = 0; SetupDiEnumDeviceInterfaces(devInfo, &devInfoData, &hidGuid, j, &devInterfaceData); j++)
{
DWORD bufferSize = 0;
SetupDiGetDeviceInterfaceDetail(devInfo, &devInterfaceData, NULL, 0, &bufferSize, &devInfoData);
PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(bufferSize);
pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(devInfo, &devInterfaceData, pDeviceInterfaceDetailData, bufferSize, &bufferSize, &devInfoData);
SECURITY_ATTRIBUTES securityAttributes;
memset(&securityAttributes, 0, sizeof(SECURITY_ATTRIBUTES));
securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
securityAttributes.bInheritHandle = true;
HANDLE fileHandle = CreateFile(pDeviceInterfaceDetailData->DevicePath, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
&securityAttributes, OPEN_EXISTING, 0, NULL);
printf("File handle value %d\n", fileHandle);
if ((int)fileHandle != -1)
{
/*HIDP_CAPS Capabilities;
PHIDP_PREPARSED_DATA PreparsedData;
HidD_GetPreparsedData(fileHandle, &PreparsedData);
printf("HidD_GetPreparsedData error: %d\n", GetLastError());
HidP_GetCaps(PreparsedData, &Capabilities);*/
HIDD_ATTRIBUTES attributes;
memset(&attributes, 0, sizeof(HIDD_ATTRIBUTES));
attributes.Size = sizeof(HIDD_ATTRIBUTES);
HidD_GetAttributes(fileHandle, &attributes);
printf("Device attribute: %d, %d, %d\n", attributes.ProductID, attributes.VendorID, attributes.VersionNumber);
char bufRead[256] = {0};
DWORD countRead = 0;
if(!ReadFile(fileHandle, bufRead, 5, &countRead, NULL))
{
printf("Reading file error %d\n", GetLastError());
}
char bufWrite[] = {'7', '8', '9', ' '};
DWORD countWritten = 0;
if(!WriteFile(fileHandle, bufWrite, 3, &countWritten, NULL))
{
printf("Writing file error %d\n", GetLastError());
}
}
else
{
printf("Error code: %d\n", GetLastError());
}
free(pDeviceInterfaceDetailData);
}
printf("____________________\n");
}
SetupDiDestroyDeviceInfoList(devInfo);
}