The setupdienumdeviceinterfaces function enumerates the device interfaces that is contained in a device Informat Ion set.
BOOL setupdienumdeviceinterfaces ( _in_ hdevinfo deviceinfoset, _in_opt_ psp_devinfo_data DeviceInfoData,
_in_ Const GUID *interfaceclassguid, _in_ DWORD memberindex, _out_ psp_device_interface_d ATA deviceinterfacedata);
Question 1: The first return is true, but the second return is false, so strange ...
Problem code:
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_datapdevdetaildata; 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);D evattributes.size=sizeof (devattributes); Hidd_gethidguid (&hidguid); Hdevinfoset=setupdigetclassdevs (&hidguid, NULL, NULL, digcf_deviceinterface| Digcf_present); memberindex=0; int i=0; while (1) {//prevents entering the dead loop i++; if (i>3) {return FALSE;} Sleep (20); try {//when running here, Memberindex returns False when it is 2 o'clock.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)//If the device is found, the startup thread reads {/*//starts not reading data, sends a packet to read a packet of DWORD dwhthread; if (M_hreaddatahandle ==invalid_handle_value) {bRe Addata =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; }
Workaround:
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_datapdevdetaildata; 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);D evattributes.size=sizeof (devattributes); Hidd_gethidguid (&hidguid); Hdevinfoset=setupdigetclassdevs (&hidguid, NULL, NULL, digcf_deviceinterface| Digcf_present); memberindex=0; int i=0; while (1) {//prevents entering the dead loop i++; if (i>3) {return FALSE;} Sleep (20); try {/*result = SetupdienumdeviceinterfacEs (Hdevinfoset, NULL, &hidguid, Memberindex, &devinterfacedata), if (FALSE = = Result) break; */ memberindex = 100;//loop count while (memberindex--) {Result = Setupdienumdeviceinterfaces ( Hdevinfoset, NULL, &hidguid, Memberindex, &devinterfacedata); if (Result = = TRUE) {break;}} if (FALSE = = Result) {char szerror[256];D Word 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)//if foundDevice, start thread read {/*//Start not reading data, send a packet to read a packet of 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; }
Issue 2: normal operation under WIN7, error in WIN8.
Workaround:
Determine if it is a WIN8 system
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;}}
Finally upload source for view: Files.cnblogs.com/royaldesigner/usb.zip
Setupdienumdeviceinterfaces return False Win7/win8