获取设备管理器中设备状态SetupDiGetDeviceRegistryProperty
2023-06-03
115
0
#include<windows.h>
#include <winioctl.h>
#include <setupapi.h>
#include <initguid.h>
#include <stdio.h>
#include<hidsdi.h>
#include<tchar.h>
#pragma comment(lib,"Setupapi.lib ")
#pragma comment(lib,"hid.lib ")
BOOL USBEnumDevice(OUT PTCHAR pDeviceName, IN LPGUID pGuid, IN int instance)
{
HDEVINFO info = SetupDiGetClassDevs((GUID*)pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if (info == INVALID_HANDLE_VALUE)
{
printf("No HDEVINFO available for this GUID\n");
return FALSE;
}
// Get interface data for the requested instance
SP_INTERFACE_DEVICE_DATA ifdata;
ifdata.cbSize = sizeof(ifdata);
if (!SetupDiEnumDeviceInterfaces(info, NULL, (GUID*)pGuid, instance, &ifdata))
{
_tprintf(TEXT("No SP_INTERFACE_DEVICE_DATA available for this GUID instance\n"));
SetupDiDestroyDeviceInfoList(info);
return FALSE;
}
// Get size of symbolic link name
DWORD ReqLen;
SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new TCHAR[ReqLen]);
if (ifDetail == NULL)
{
SetupDiDestroyDeviceInfoList(info);
return FALSE;
}
// Get symbolic link name
ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL))
{
SetupDiDestroyDeviceInfoList(info);
delete[] ifDetail;
return FALSE;
}
//printf("Symbolic link is %s\n", ifDetail->DevicePath);
memcpy(pDeviceName, ifDetail->DevicePath, _tcslen(ifDetail->DevicePath) * sizeof(TCHAR));
pDeviceName[_tcslen(ifDetail->DevicePath)] = TEXT('\0');
delete[] ifDetail;
SetupDiDestroyDeviceInfoList(info);
return TRUE;
}
#define TO_SEARCH_DEVICE_UUID L"{A5DCBF10-6530-11D2-901F-00C04FB951ED}"
#include <windows.h>
#include <tchar.h>
#include <stdlib.h>
#include <stdio.h>
#include <setupapi.h>
#include <regstr.h>
#include <infstr.h>
#include <cfgmgr32.h>
#include <string.h>
#include <malloc.h>
#include <newdev.h>
#include <objbase.h>
#include <strsafe.h>
int main()
{
//GUID hidGuid;
//::HidD_GetHidGuid((LPGUID)&hidGuid);
GUID AGuid;
CLSIDFromString(TO_SEARCH_DEVICE_UUID, &AGuid);
HDEVINFO hDevInfo = SetupDiGetClassDevs(&AGuid, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return 0;
}
SP_DEVINFO_DATA DeviceInfoData;
for (int iIndex = 0;; iIndex++)
{
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if (!SetupDiEnumDeviceInfo(hDevInfo, iIndex, &DeviceInfoData))
{
break;
}
char szHardWareID[512] = { 0 };
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_HARDWAREID,
NULL, (PBYTE)szHardWareID, sizeof(szHardWareID), NULL))
{
continue;
}
//本人的一个自定义设备
if (strstr(szHardWareID, "root\\pnpon") == NULL)
{
continue;
}
// {
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
ULONG status = 0;
ULONG problem = 0;
BOOL hasInfo = FALSE;
BOOL isPhantom = FALSE;
CONFIGRET cr = 0;
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
if ((!SetupDiGetDeviceInfoListDetail(hDevInfo, &devInfoListDetail)) ||
((cr = CM_Get_DevNode_Status_Ex(&status, &problem, DeviceInfoData.DevInst, 0, devInfoListDetail.RemoteMachineHandle)) != 0))
{
cr = cr;
}
printf("problem=%d\r\n", problem);
// }
{
printf("%s\r\n", szHardWareID);
typedef enum _DEVICE_INSTALL_STATE {
InstallStateInstalled,
InstallStateNeedsReinstall,
InstallStateFailedInstall,
InstallStateFinishInstall
} DEVICE_INSTALL_STATE, * PDEVICE_INSTALL_STATE;
ULONG v = -1;
//未知状态是2,安装了是0(包括未启动)
if (SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_INSTALL_STATE, NULL, (PBYTE)&v, sizeof(v), NULL))\
{
printf( ":%d\r\n", v);
}
if (SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_DEVICE_POWER_DATA, NULL, (PBYTE)&v, sizeof(v), NULL))\
{
printf( ":%d\r\n", v);
}
}
#define SHOW_PRO(X) \
{ \
if (SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, X,NULL, (PBYTE)szHardWareID, sizeof(szHardWareID), NULL))\
{\
printf(#X ":%s\r\n", szHardWareID);\
}\
}
SHOW_PRO(SPDRP_DEVICEDESC);
SHOW_PRO(SPDRP_HARDWAREID);
SHOW_PRO(SPDRP_COMPATIBLEIDS);
SHOW_PRO(SPDRP_UNUSED0);
SHOW_PRO(SPDRP_SERVICE);
SHOW_PRO(SPDRP_UNUSED1);
SHOW_PRO(SPDRP_UNUSED2);
SHOW_PRO(SPDRP_CLASS);
SHOW_PRO(SPDRP_CLASSGUID);
SHOW_PRO(SPDRP_DRIVER);
SHOW_PRO(SPDRP_CONFIGFLAGS);
SHOW_PRO(SPDRP_MFG);
SHOW_PRO(SPDRP_FRIENDLYNAME);
SHOW_PRO(SPDRP_LOCATION_INFORMATION);
SHOW_PRO(SPDRP_PHYSICAL_DEVICE_OBJECT_NAME);
SHOW_PRO(SPDRP_CAPABILITIES);
SHOW_PRO(SPDRP_UI_NUMBER);
SHOW_PRO(SPDRP_UPPERFILTERS);
SHOW_PRO(SPDRP_LOWERFILTERS);
SHOW_PRO(SPDRP_BUSTYPEGUID);
SHOW_PRO(SPDRP_LEGACYBUSTYPE);
SHOW_PRO(SPDRP_BUSNUMBER);
SHOW_PRO(SPDRP_ENUMERATOR_NAME);
SHOW_PRO(SPDRP_SECURITY);
SHOW_PRO(SPDRP_SECURITY_SDS);
SHOW_PRO(SPDRP_DEVTYPE);
SHOW_PRO(SPDRP_EXCLUSIVE);
SHOW_PRO(SPDRP_CHARACTERISTICS);
SHOW_PRO(SPDRP_ADDRESS);
SHOW_PRO(SPDRP_UI_NUMBER_DESC_FORMAT);
SHOW_PRO(SPDRP_DEVICE_POWER_DATA);
SHOW_PRO(SPDRP_REMOVAL_POLICY);
SHOW_PRO(SPDRP_REMOVAL_POLICY_HW_DEFAULT);
SHOW_PRO(SPDRP_REMOVAL_POLICY_OVERRIDE);
SHOW_PRO(SPDRP_INSTALL_STATE);
SHOW_PRO(SPDRP_LOCATION_PATHS);
SHOW_PRO(SPDRP_BASE_CONTAINERID);
}
getchar();
printf("pk");
return 0;
}