1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include <Windows.h>
#include <iostream>
#include <vector>
#include <Setupapi.h>
#pragma comment(lib, "setupapi.lib")
using namespace std;
HANDLE GetDeviceInfoSet() {
HANDLE DeviceInfoSet = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (DeviceInfoSet == INVALID_HANDLE_VALUE) {
cout << "invalid handle";
}
return DeviceInfoSet;
}
vector<SP_DEVINFO_DATA> GetDeviceInfoData(HANDLE DeviceInfoSet) {
SP_DEVINFO_DATA DeviceInfoData;
ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA));
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
size_t DeviceIndex = 0;
vector<SP_DEVINFO_DATA> v_Device_Info_Data{};
while (SetupDiEnumDeviceInfo(DeviceInfoSet, DeviceIndex, &DeviceInfoData)) {
v_Device_Info_Data.push_back(DeviceInfoData);
DeviceIndex++;
}
return v_Device_Info_Data;
}
vector<vector<DEVPROPKEY>> GetDevicePropertyKeys(HANDLE DeviceInfoSet, vector<SP_DEVINFO_DATA> v_Device_Info_Data) {
vector<vector<DEVPROPKEY>> v_key{};
for (int x = 0; x < v_Device_Info_Data.size(); x++) {
vector<DEVPROPKEY> k{};
DWORD size = 0;
SetupDiGetDevicePropertyKeys(DeviceInfoSet, &v_Device_Info_Data[x], NULL, 0, &size, 0);
BYTE* lp = new BYTE[size * sizeof(DEVPROPKEY)];
DEVPROPKEY* key = (DEVPROPKEY*)lp;
SetupDiGetDevicePropertyKeys(DeviceInfoSet, &v_Device_Info_Data[x], key, size, &size, 0);
for (int y = 0; y < size; y++) {
k.push_back(key[y]);
}
v_key.push_back(k);
delete[] lp;
}
return v_key;
}
vector<string> GetDeviceInstanceIDs(HANDLE DeviceInfoSet, vector<SP_DEVINFO_DATA> v_Device_Info_Data) {
vector<string> v_InstanceID{};
for (int y = 0; y < v_Device_Info_Data.size(); y++) {
DWORD size{};
SetupDiGetDeviceInstanceIdW(DeviceInfoSet, &v_Device_Info_Data[y], NULL, NULL, &size);
WCHAR* ID = new WCHAR[size];
SetupDiGetDeviceInstanceIdW(DeviceInfoSet, &v_Device_Info_Data[y], ID, size, &size);
string s{};
for (int x = 0; x < size; x++) {
s.push_back(ID[x]);
}
v_InstanceID.push_back(s);
delete[] ID;
}
return v_InstanceID;
}
vector<string> GetDeviceLocation(HANDLE DeviceInfoSet, vector<SP_DEVINFO_DATA> v_Device_Info_Data) { //SetupDiGetDeviceRegistryProperty example <~~~~~~~~~~~~~~~~~
vector<string> v_Device_Location{};
for (int x = 0; x < v_Device_Info_Data.size(); x++) {
DWORD size{};
SetupDiGetDeviceRegistryPropertyA(DeviceInfoSet, &v_Device_Info_Data[x], SPDRP_LOCATION_PATHS, NULL, NULL, NULL, &size);
char* pb = new char[size];
SetupDiGetDeviceRegistryPropertyA(DeviceInfoSet, &v_Device_Info_Data[x], SPDRP_LOCATION_PATHS, NULL, (PBYTE)pb, size, &size);
string s{};
for (int y = 0; y < size; y++) {
s.push_back(pb[y]);
}
v_Device_Location.push_back(s);
delete[] pb;
}
return v_Device_Location;
}
vector<vector<BYTE>> GetDeviceRegistryProperty(HANDLE DeviceInfoSet, vector<SP_DEVINFO_DATA> v_Device_Info_Data, DWORD Property) {
vector<vector<BYTE>> v_Registry_Property{};
for (int x = 0; x < v_Device_Info_Data.size(); x++) {
DWORD size{};
SetupDiGetDeviceRegistryPropertyA(DeviceInfoSet, &v_Device_Info_Data[x], Property, NULL, NULL, NULL, &size);
BYTE* pb = new BYTE[size];
SetupDiGetDeviceRegistryPropertyA(DeviceInfoSet, &v_Device_Info_Data[x], Property, NULL, pb, size, &size);
vector<BYTE> v_Byte{};
for (int y = 0; y < size; y++) {
v_Byte.push_back(pb[y]);
}
v_Registry_Property.push_back(v_Byte);
delete[] pb;
}
return v_Registry_Property;
}
vector<SP_DRVINFO_DATA> GetDeviceDriverInfoData(HANDLE DeviceInfoSet, vector<SP_DEVINFO_DATA> v_Device_Info_Data) {
vector<SP_DRVINFO_DATA> v_Driver_Info_Data{};
SP_DRVINFO_DATA DriverInfoData{};
if (!SetupDiBuildDriverInfoList(DeviceInfoSet, NULL, SPDIT_CLASSDRIVER)) {
cout << "failed to build list\n";
}
for (int x = 0; x < v_Device_Info_Data.size(); x++) {
ZeroMemory(&DriverInfoData, sizeof(SP_DRVINFO_DATA));
DriverInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
if (!SetupDiEnumDriverInfoW(DeviceInfoSet, NULL, SPDIT_CLASSDRIVER, x, &DriverInfoData)) {
cout << "failed";
}
auto error = GetLastError();
v_Driver_Info_Data.push_back(DriverInfoData);
}
return v_Driver_Info_Data;
}
vector<SP_DRVINFO_DETAIL_DATA> GetDeviceDriverDetail(HANDLE DeviceInfoSet, vector<SP_DRVINFO_DATA> v_Driver_Info_Data) {
vector<SP_DRVINFO_DETAIL_DATA> v_Driver_Info_Detail_Data{};
for (int x = 0; x < v_Driver_Info_Data.size(); x++) {
DWORD size{};
SetupDiGetDriverInfoDetailW(DeviceInfoSet, NULL, &v_Driver_Info_Data[x], NULL, 0, &size);
void* lp = malloc(size);
PSP_DRVINFO_DETAIL_DATA DriverInfoDetailData = (PSP_DRVINFO_DETAIL_DATA)lp;
ZeroMemory(DriverInfoDetailData, size);
DriverInfoDetailData->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
SetupDiGetDriverInfoDetailW(DeviceInfoSet, NULL, &v_Driver_Info_Data[x], DriverInfoDetailData, size, &size);
v_Driver_Info_Detail_Data.push_back(*DriverInfoDetailData);
VirtualFree(lp, size, MEM_RELEASE);
delete lp;
}
return v_Driver_Info_Detail_Data;
}
|