1#include "system_devices.h"
2#include <cgv/utils/convert.h>
8#pragma comment (lib, "Setupapi.lib")
15bool enumerate_system_devices(std::vector<std::pair<std::string, cgv::utils::guid>>& devices,
const std::string& selector,
bool present)
19 const TCHAR* enumerator = 0;
21 std::wstring selector_w;
23 if (!selector.empty()) {
26 enumerator = selector_w.c_str();
28 enumerator = selector.c_str();
31 HDEVINFO deviceInfo = SetupDiGetClassDevs(NULL, enumerator, NULL, present ? (DIGCF_PRESENT | DIGCF_ALLCLASSES) : DIGCF_ALLCLASSES);
32 if (deviceInfo == INVALID_HANDLE_VALUE) {
33 std::cerr <<
"Error: " << GetLastError() << std::endl;
37 SP_DEVINFO_DATA deviceInfoData;
38 deviceInfoData.cbSize =
sizeof(SP_DEVINFO_DATA);
39 DWORD memberIndex = 0;
40 while (SetupDiEnumDeviceInfo(deviceInfo, memberIndex, &deviceInfoData)) {
42 TCHAR devicePathBuffer[500];
43 DWORD devicePathLength = 0;
44 if (!SetupDiGetDeviceInstanceId(deviceInfo, &deviceInfoData, devicePathBuffer,
sizeof(devicePathBuffer), &devicePathLength))
47 std::string device_path =
cgv::utils::wstr2str(std::wstring(devicePathBuffer, devicePathLength));
49 std::string device_path(devicePathBuffer, devicePathLength);
53 if (!SetupDiGetDevicePropertyW(deviceInfo, &deviceInfoData, &DEVPKEY_Device_ContainerId, &propType,
reinterpret_cast<BYTE*
>(&container_id),
sizeof(GUID),
nullptr, 0))
54 std::cerr <<
"Error: " << GetLastError() << std::endl;
55 devices.push_back({ device_path,container_id });
57 SetupDiDestroyDeviceInfoList(deviceInfo);
60 std::cerr <<
"cgv::os::enumerate_system_devices() only implemented on Windows" << std::endl;
std::wstring str2wstr(const std::string &s)
convert a 8-bit string to a 16-bit string
std::string wstr2str(const std::wstring &ws)
convert a 16-bit string to a 8-bit string
simple struct to represent guid