SetupDiEnumDeviceInterfaces가 false WIN7/WIN8을 반환합니다.

9962 단어 interfaces
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;

	}

}

마지막으로 원본 코드를 업로드하여 보기: 파일.cnblogs.com/royaldesigner/usb.zip

좋은 웹페이지 즐겨찾기