Skip to content

Instantly share code, notes, and snippets.

@katopz
Last active January 27, 2021 16:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katopz/5734124 to your computer and use it in GitHub Desktop.
Save katopz/5734124 to your computer and use it in GitHub Desktop.
Will get all entries of all USB devices, the USB host controllers, the USB Root hubs , hubs and usb devices.
// refer to : http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/76315dfa-6764-4feb-a3e2-1f173fc5bdfd
// will get all entries of all USB devices, the USB host controllers, the USB Root hubs , hubs and usb devices.
// The results that are located at "USB\VID_xxx&PID_yyyy\zzzz"
// it may happen that you have more than one USB device with the same VID and PID on the system.
// Therfore you must also check for the serial# (the zzzz in the bold text). A USB device is only unique with VID+PID+seial#.
// with some decated fixed working via VS express 2012
// 2013/06/08
// @author katopz
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <Setupapi.h>
#include <devguid.h>
#include "conio.h"
#include "tchar.h"
#pragma comment (lib, "Setupapi.lib")
int _tmain(int argc, _TCHAR* argv[])
{
// init deviceInfoSet
HDEVINFO deviceInfoSet;
GUID *guidDev = (GUID*) &GUID_DEVCLASS_USB;
deviceInfoSet = SetupDiGetClassDevs(guidDev, NULL, NULL, DIGCF_PRESENT | DIGCF_PROFILE);
TCHAR buffer [4000];
DWORD buffersize = 4000;
int memberIndex = 0;
// loop all deviceInfoData in deviceInfoSet
while (true)
{
// init deviceInfoData
SP_DEVINFO_DATA deviceInfoData;
ZeroMemory(&deviceInfoData, sizeof(SP_DEVINFO_DATA));
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
// eof
if (SetupDiEnumDeviceInfo(deviceInfoSet, memberIndex, &deviceInfoData) == FALSE)
{
if (GetLastError() == ERROR_NO_MORE_ITEMS)
{
break;
}
}
// get id
DWORD nSize = 0 ;
SetupDiGetDeviceInstanceId (deviceInfoSet, &deviceInfoData, buffer, sizeof(buffer), &nSize);
buffer [nSize] = '\0';
// echo
_tprintf (_T("%s\n"), buffer);
// next
memberIndex++;
}
// release
if (deviceInfoSet)
{
SetupDiDestroyDeviceInfoList(deviceInfoSet);
}
// keep output
_getch();
// bye!
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment