SetupDiEnumDeviceInterfaces가 false WIN7/WIN8을 반환합니다.
9962 단어 interfaces
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;
}
}
마지막으로 원본 코드를 업로드하여 보기: 파일.cnblogs.com/royaldesigner/usb.zip
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SetupDiEnumDeviceInterfaces가 false WIN7/WIN8을 반환합니다.The SetupDiEnumDeviceInterfaces function enumerates the device interfaces that are contained in a device information set...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.