The SetupDiEnumDeviceInterfaces function enumerates the device interfaces that are contained in a device information set.
BOOL SetupDiEnumDeviceInterfaces( _In_ HDEVINFO DeviceInfoSet, _In_opt_ PSP_DEVINFO_DATA DeviceInfoData, _In_ const GUID *InterfaceClassGuid, _In_ DWORD MemberIndex, _Out_ PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData );
注意:检查设备pid,vid是否与代码中得到的pid,vid一致!
问题1:第一次返回的是TRUE,但第二次返回的却是FALSE了,好奇怪...
问题代码:
BOOL Cusb::openDevice(DWORD dwVid, DWORD dwPid)
{
m_hReadHandle=INVALID_HANDLE_VALUE;
m_hWriteHandle=INVALID_HANDLE_VALUE;
m_hReadEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
m_hWriteEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
TCHAR MyDevPathName[MAX_PATH];
GUID HidGuid;
HDEVINFO hDevInfoSet;
DWORD MemberIndex;
SP_DEVICE_INTERFACE_DATA DevInterfaceData;
BOOL Result;
DWORD RequiredSize;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetailData;
hDevHandle =INVALID_HANDLE_VALUE;
HIDD_ATTRIBUTES DevAttributes;
BOOL MyDevFound=FALSE;
bOpScucess =false;
m_hReadHandle=INVALID_HANDLE_VALUE;
m_hWriteHandle=INVALID_HANDLE_VALUE;
_tcscpy(DevPathName, "");
try
{
DevInterfaceData.cbSize=sizeof(DevInterfaceData);
DevAttributes.Size=sizeof(DevAttributes);
HidD_GetHidGuid(&HidGuid);
hDevInfoSet=SetupDiGetClassDevs(&HidGuid, NULL, NULL, DIGCF_DEVICEINTERFACE|DIGCF_PRESENT) ;
MemberIndex=0 ;
int i=0 ;
while(1)
{
//防止进入死循环
i++ ;
if(i>3)
{
return FALSE ;
}
Sleep(20) ;
try
{
//这里运行的时候,当MemberIndex为2时就返回FALSE了.
Result = SetupDiEnumDeviceInterfaces(hDevInfoSet, NULL, &HidGuid, MemberIndex, &DevInterfaceData);
if(FALSE == Result)
break ;
MemberIndex++ ;
Result = SetupDiGetDeviceInterfaceDetail(hDevInfoSet, &DevInterfaceData, NULL, NULL, &RequiredSize, NULL) ;
pDevDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize) ;
if(NULL == pDevDetailData)
{
SetupDiDestroyDeviceInfoList(hDevInfoSet) ;
return FALSE;
}
pDevDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
Result = SetupDiGetDeviceInterfaceDetail(hDevInfoSet, &DevInterfaceData, pDevDetailData, RequiredSize, NULL, NULL) ;
_tcscpy(MyDevPathName, pDevDetailData->DevicePath);
_tcscpy(DevPathName, pDevDetailData->DevicePath);
free(pDevDetailData) ;
if(FALSE == Result)
{
continue ;
}
}
catch (CException* e)
{
return MyDevFound;
}
try
{
hDevHandle=CreateFile(MyDevPathName,
NULL,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL) ;
if(INVALID_HANDLE_VALUE == hDevHandle)
{
continue ;
}
PHIDP_PREPARSED_DATA preparseData;
HidD_GetPreparsedData(hDevHandle,&preparseData);
HIDP_CAPS caps;
HidP_GetCaps(preparseData,&caps);
Result = HidD_GetAttributes(hDevHandle, &DevAttributes);
CloseHandle(hDevHandle);
if(Result == FALSE)
continue ;
}
catch (CException* e)
{
return MyDevFound;
}
try
{
if( (DevAttributes.VendorID == dwVid) && (DevAttributes.ProductID == dwPid) )
{
MyDevFound=TRUE;
m_hReadHandle=CreateFile(MyDevPathName,
NULL,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL) ;
// int nError =GetLastError();
if(m_hReadHandle==INVALID_HANDLE_VALUE)
{
return FALSE ;
}
m_hWriteHandle=CreateFile(MyDevPathName, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL) ;
if(m_hWriteHandle==INVALID_HANDLE_VALUE)
{
return FALSE;
}
break;
}
SetupDiDestroyDeviceInfoList(hDevInfoSet);
}
catch (CException* e)
{
return MyDevFound;
}
}
if (MyDevFound) //如果找到设备,启动线程读取
{
/* //启动不读取数据,发一包读一包
DWORD dwhThread;
if (m_hReadDataHandle ==INVALID_HANDLE_VALUE)
{
bReadData =true;
m_hReadDataHandle =::CreateThread(NULL,0,ReadUsbData,this,0,&dwhThread);
}
*/
bOpScucess =true;
StartThread();
}
}
catch (CMemoryException* e)
{
return MyDevFound;
}
catch (CFileException* e)
{
return MyDevFound;
}
catch (CException* e)
{
return MyDevFound;
}
bOpScucess =true;
return MyDevFound;
}
解决办法:
BOOL Cusb::openDevice(DWORD dwVid, DWORD dwPid)
{
m_hReadHandle=INVALID_HANDLE_VALUE;
m_hWriteHandle=INVALID_HANDLE_VALUE;
m_hReadEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
m_hWriteEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
TCHAR MyDevPathName[MAX_PATH];
GUID HidGuid;
HDEVINFO hDevInfoSet;
DWORD MemberIndex;
SP_DEVICE_INTERFACE_DATA DevInterfaceData;
BOOL Result;
DWORD RequiredSize;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetailData;
hDevHandle =INVALID_HANDLE_VALUE;
HIDD_ATTRIBUTES DevAttributes;
BOOL MyDevFound=FALSE;
bOpScucess =false;
m_hReadHandle=INVALID_HANDLE_VALUE;
m_hWriteHandle=INVALID_HANDLE_VALUE;
_tcscpy(DevPathName, "");
try
{
DevInterfaceData.cbSize=sizeof(DevInterfaceData);
DevAttributes.Size=sizeof(DevAttributes);
HidD_GetHidGuid(&HidGuid);
hDevInfoSet=SetupDiGetClassDevs(&HidGuid, NULL, NULL, DIGCF_DEVICEINTERFACE|DIGCF_PRESENT) ;
MemberIndex=0 ;
int i=0 ;
while(1)
{
//防止进入死循环
i++ ;
if(i>3)
{
return FALSE ;
}
Sleep(20) ;
try
{
/* Result = SetupDiEnumDeviceInterfaces(hDevInfoSet, NULL, &HidGuid, MemberIndex, &DevInterfaceData) ;
if(FALSE == Result)
break ;*/
MemberIndex = 100;//循环次数
while(MemberIndex--){
Result = SetupDiEnumDeviceInterfaces(hDevInfoSet, NULL, &HidGuid, MemberIndex, &DevInterfaceData) ;
if(Result == TRUE){
break;
}
}
if(FALSE == Result)
{
char szError[256];
DWORD dwError = GetLastError();
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
0,
szError,
sizeof(szError),
NULL);
AfxMessageBox(szError);
break ;
}
MemberIndex++ ;
Result = SetupDiGetDeviceInterfaceDetail(hDevInfoSet, &DevInterfaceData, NULL, NULL, &RequiredSize, NULL) ;
pDevDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize) ;
if(NULL == pDevDetailData)
{
SetupDiDestroyDeviceInfoList(hDevInfoSet) ;
return FALSE;
}
pDevDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
Result = SetupDiGetDeviceInterfaceDetail(hDevInfoSet, &DevInterfaceData, pDevDetailData, RequiredSize, NULL, NULL) ;
_tcscpy(MyDevPathName, pDevDetailData->DevicePath);
_tcscpy(DevPathName, pDevDetailData->DevicePath);
free(pDevDetailData) ;
if(FALSE == Result)
{
continue ;
}
}
catch (CException* e)
{
return MyDevFound;
}
try
{
hDevHandle=CreateFile(MyDevPathName,
NULL,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL) ;
if(INVALID_HANDLE_VALUE == hDevHandle)
{
continue ;
}
PHIDP_PREPARSED_DATA preparseData;
HidD_GetPreparsedData(hDevHandle,&preparseData);
HIDP_CAPS caps;
HidP_GetCaps(preparseData,&caps);
Result = HidD_GetAttributes(hDevHandle, &DevAttributes);
CloseHandle(hDevHandle);
if(Result == FALSE)
continue ;
}
catch (CException* e)
{
return MyDevFound;
}
try
{
if( (DevAttributes.VendorID == dwVid) && (DevAttributes.ProductID == dwPid) )
{
MyDevFound=TRUE;
m_hReadHandle=CreateFile(MyDevPathName,
NULL,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL) ;
// int nError =GetLastError();
if(m_hReadHandle==INVALID_HANDLE_VALUE)
{
return FALSE ;
}
m_hWriteHandle=CreateFile(MyDevPathName, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL) ;
if(m_hWriteHandle==INVALID_HANDLE_VALUE)
{
return FALSE;
}
break;
}
SetupDiDestroyDeviceInfoList(hDevInfoSet);
}
catch (CException* e)
{
return MyDevFound;
}
}
if (MyDevFound) //如果找到设备,启动线程读取
{
/* //启动不读取数据,发一包读一包
DWORD dwhThread;
if (m_hReadDataHandle ==INVALID_HANDLE_VALUE)
{
bReadData =true;
m_hReadDataHandle =::CreateThread(NULL,0,ReadUsbData,this,0,&dwhThread);
}
*/
bOpScucess =true;
StartThread();
}
}
catch (CMemoryException* e)
{
return MyDevFound;
}
catch (CFileException* e)
{
return MyDevFound;
}
catch (CException* e)
{
return MyDevFound;
}
bOpScucess =true;
return MyDevFound;
}
问题2:在WIN7下运行正常,在WIN8下就出错。
解决办法:
判断其是否为Win8系统
bool GetOsWin8(){ OSVERSIONINFOEX osvi; BOOL bOsVersionInfoEx; ZeroMemory(&osvi,sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi); if(VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 6 && osvi.dwMinorVersion == 0){ return true; } else if(VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 2){ return true; } else{ return false; } }
最后上传源码供查看:files.cnblogs.com/royaldesigner/usb.zip