From: mike on 24 Aug 2005 10:29 Can anyone point me to an example (that works) or specific information on how to enumerate the usb ports, retrieve a device name that will work with CreateFile()? I've been able to enumerate all of the classes with SeteupDiGetClassDevs() but have not been able to get SetupDiEnumDeviceInterfaces() to succeed to retrieve the PSP_DEVICE_INTERFACE_DATA needed for SetupDiGetDeviceInterfaceDetails(). What does SetupDiEnumDeviceInterfaces() want? HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; DWORD i; // Create a HDEVINFO with all present devices. hDevInfo = SetupDiGetClassDevs(NULL, 0,0,DIGCF_PRESENT | DIGCF_ALLCLASSES ); if (hDevInfo == INVALID_HANDLE_VALUE) { // Insert error handling here. printf("Invalid handle\n"); _getch(); return 1; } // Enumerate through all devices in Set. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i, &DeviceInfoData);i++) { DWORD DataT; LPTSTR buffer = NULL; DWORD buffersize = 0; // // Call function with null to begin with, // then use the returned buffer size // to Alloc the buffer. Keep calling until // success or an unknown failure. // while (!SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, &DataT, (PBYTE)buffer, buffersize, &buffersize)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. if (buffer) LocalFree(buffer); buffer = (LPSTR)LocalAlloc(LPTR,buffersize); } else { // Insert error handling here. break; } } CString str = buffer; // I know this is incorrect so please don't chew me out. // Not sure what to do now. This code looks for either the "USB Root Hub" or "DBM USB" which is a partial string //for the Intel usb port on my computer. if ( str.Find( "USB Root Hub" ) >= 0 ) //if ( str.Find( "DBM USB" ) >= 0 ) { SP_DEVICE_INTERFACE_DATA ifdata; ifdata.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); for ( int k=0; ; k++ ) { if ( !SetupDiEnumDeviceInterfaces( hDevInfo, &DeviceInfoData, &DeviceInfoData.ClassGuid, k, &ifdata ) ) { // This error is always 259 (no more interfaces) TRACE("Err %d\n", GetLastError()); break; } } } else printf("Result:[%s]\n",buffer); if (buffer) LocalFree(buffer); }
From: Joseph M. Newcomer on 24 Aug 2005 21:01 There is no such thing as "enumerating USB ports". USB ports are not devices like top-level devices. There is nothing you can open if you know the USB port, because a USB port has no "real" existence at the user level. Only USB devices exist. You can enumerate those. Opening a "USB Port" is a completely meaningless activity. There's nothing you could do with it, even if you could open it! Only the low-level USB drivers are allowed to talk to the USB port, thus it has no visiblity at the user level. joe On Wed, 24 Aug 2005 10:29:46 -0400, "mike" <nospamplease.com> wrote: >Can anyone point me to an example (that works) or specific information on >how to enumerate the usb ports, retrieve a device name that will work with >CreateFile()? I've been able to enumerate all of the classes with >SeteupDiGetClassDevs() but have not been able to get >SetupDiEnumDeviceInterfaces() to succeed to retrieve the >PSP_DEVICE_INTERFACE_DATA needed for SetupDiGetDeviceInterfaceDetails(). >What does SetupDiEnumDeviceInterfaces() want? > > HDEVINFO hDevInfo; > SP_DEVINFO_DATA DeviceInfoData; > DWORD i; > > // Create a HDEVINFO with all present devices. > hDevInfo = SetupDiGetClassDevs(NULL, 0,0,DIGCF_PRESENT | >DIGCF_ALLCLASSES ); > > if (hDevInfo == INVALID_HANDLE_VALUE) > { > // Insert error handling here. > printf("Invalid handle\n"); > _getch(); > return 1; > } > > // Enumerate through all devices in Set. > > DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); > > for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i, &DeviceInfoData);i++) > { > DWORD DataT; > LPTSTR buffer = NULL; > DWORD buffersize = 0; > > // > // Call function with null to begin with, > // then use the returned buffer size > // to Alloc the buffer. Keep calling until > // success or an unknown failure. > // > while (!SetupDiGetDeviceRegistryProperty( > hDevInfo, > &DeviceInfoData, > SPDRP_DEVICEDESC, > &DataT, > (PBYTE)buffer, > buffersize, > &buffersize)) > { > if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) > { > // Change the buffer size. > if (buffer) LocalFree(buffer); > > buffer = (LPSTR)LocalAlloc(LPTR,buffersize); > } > else > { > // Insert error handling here. > break; > } > } > > CString str = buffer; > >// I know this is incorrect so please don't chew me out. >// Not sure what to do now. This code looks for either the "USB Root Hub" or >"DBM USB" which is a partial string >//for the Intel usb port on my computer. > > if ( str.Find( "USB Root Hub" ) >= 0 ) > //if ( str.Find( "DBM USB" ) >= 0 ) > { > SP_DEVICE_INTERFACE_DATA ifdata; > > ifdata.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); > > for ( int k=0; ; k++ ) > { > if ( !SetupDiEnumDeviceInterfaces( hDevInfo, &DeviceInfoData, > &DeviceInfoData.ClassGuid, k, &ifdata ) ) > { > // This error is always 259 (no more interfaces) > TRACE("Err %d\n", GetLastError()); > break; > } > } > } > else > printf("Result:[%s]\n",buffer); > > if (buffer) LocalFree(buffer); > } > > Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: mike on 25 Aug 2005 09:34 Thanks for your input. "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message news:hv5qg1946t2n1qcussb12pbujfhkurq7f8(a)4ax.com... > There is no such thing as "enumerating USB ports". USB ports are not > devices like > top-level devices. There is nothing you can open if you know the USB > port, because a USB > port has no "real" existence at the user level. Only USB devices exist. > You can > enumerate those. Opening a "USB Port" is a completely meaningless > activity. There's > nothing you could do with it, even if you could open it! Only the > low-level USB drivers > are allowed to talk to the USB port, thus it has no visiblity at the user > level. > joe > > On Wed, 24 Aug 2005 10:29:46 -0400, "mike" <nospamplease.com> wrote: > >>Can anyone point me to an example (that works) or specific information on >>how to enumerate the usb ports, retrieve a device name that will work with >>CreateFile()? I've been able to enumerate all of the classes with >>SeteupDiGetClassDevs() but have not been able to get >>SetupDiEnumDeviceInterfaces() to succeed to retrieve the >>PSP_DEVICE_INTERFACE_DATA needed for SetupDiGetDeviceInterfaceDetails(). >>What does SetupDiEnumDeviceInterfaces() want? >> >> HDEVINFO hDevInfo; >> SP_DEVINFO_DATA DeviceInfoData; >> DWORD i; >> >> // Create a HDEVINFO with all present devices. >> hDevInfo = SetupDiGetClassDevs(NULL, 0,0,DIGCF_PRESENT | >>DIGCF_ALLCLASSES ); >> >> if (hDevInfo == INVALID_HANDLE_VALUE) >> { >> // Insert error handling here. >> printf("Invalid handle\n"); >> _getch(); >> return 1; >> } >> >> // Enumerate through all devices in Set. >> >> DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); >> >> for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i, &DeviceInfoData);i++) >> { >> DWORD DataT; >> LPTSTR buffer = NULL; >> DWORD buffersize = 0; >> >> // >> // Call function with null to begin with, >> // then use the returned buffer size >> // to Alloc the buffer. Keep calling until >> // success or an unknown failure. >> // >> while (!SetupDiGetDeviceRegistryProperty( >> hDevInfo, >> &DeviceInfoData, >> SPDRP_DEVICEDESC, >> &DataT, >> (PBYTE)buffer, >> buffersize, >> &buffersize)) >> { >> if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) >> { >> // Change the buffer size. >> if (buffer) LocalFree(buffer); >> >> buffer = (LPSTR)LocalAlloc(LPTR,buffersize); >> } >> else >> { >> // Insert error handling here. >> break; >> } >> } >> >> CString str = buffer; >> >>// I know this is incorrect so please don't chew me out. >>// Not sure what to do now. This code looks for either the "USB Root Hub" >>or >>"DBM USB" which is a partial string >>//for the Intel usb port on my computer. >> >> if ( str.Find( "USB Root Hub" ) >= 0 ) >> //if ( str.Find( "DBM USB" ) >= 0 ) >> { >> SP_DEVICE_INTERFACE_DATA ifdata; >> >> ifdata.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); >> >> for ( int k=0; ; k++ ) >> { >> if ( !SetupDiEnumDeviceInterfaces( hDevInfo, &DeviceInfoData, >> &DeviceInfoData.ClassGuid, k, &ifdata ) ) >> { >> // This error is always 259 (no more interfaces) >> TRACE("Err %d\n", GetLastError()); >> break; >> } >> } >> } >> else >> printf("Result:[%s]\n",buffer); >> >> if (buffer) LocalFree(buffer); >> } >> >> > Joseph M. Newcomer [MVP] > email: newcomer(a)flounder.com > Web: http://www.flounder.com > MVP Tips: http://www.flounder.com/mvp_tips.htm
|
Pages: 1 Prev: Webcam & createfile Next: How to change the font in Static Text |