Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285/* * PROJECT: Safely Remove Hardware Applet * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later) * PURPOSE: Device enumeration * COPYRIGHT: Copyright 2020 Eric Kohl <eric.kohl@reactos.org> * Copyright 2023 Thamatip Chitpong <thamatip.chitpong@reactos.org> */
#include "hotplug.h"
#include <initguid.h>#include <devguid.h>
#define MAX_DEVICE_DISPLAYNAME_LEN 256
staticVOIDGetDeviceDisplayInfo( _In_ DEVINST DevInst, _In_ PHOTPLUG_DATA pHotplugData, _Out_writes_z_(cchDesc) LPWSTR pszDesc, _In_ ULONG cchDesc, _Out_ PINT pImageIndex){ WCHAR szGuidString[MAX_GUID_STRING_LEN]; GUID ClassGuid; ULONG ulSize; CONFIGRET cr;
/* Get the device description */ ulSize = cchDesc * sizeof(WCHAR); cr = CM_Get_DevNode_Registry_PropertyW(DevInst, CM_DRP_FRIENDLYNAME, NULL, pszDesc, &ulSize, 0); if (cr != CR_SUCCESS) { ulSize = cchDesc * sizeof(WCHAR); cr = CM_Get_DevNode_Registry_PropertyW(DevInst, CM_DRP_DEVICEDESC, NULL, pszDesc, &ulSize, 0); if (cr != CR_SUCCESS) LoadStringW(hApplet, IDS_UNKNOWN_DEVICE, pszDesc, cchDesc); }
/* Get the class GUID */ ulSize = sizeof(szGuidString); cr = CM_Get_DevNode_Registry_PropertyW(DevInst, CM_DRP_CLASSGUID, NULL, szGuidString, &ulSize, 0); if (cr == CR_SUCCESS) { pSetupGuidFromString(szGuidString, &ClassGuid); } else { ClassGuid = GUID_DEVCLASS_UNKNOWN; }
/* Get the image for the class this device is in */ SetupDiGetClassImageIndex(&pHotplugData->ImageListData, &ClassGuid, pImageIndex);}
staticHTREEITEMInsertDeviceTreeItem( _In_ HTREEITEM hParent, _In_ DEVINST DevInst, _In_ PHOTPLUG_DATA pHotplugData){ WCHAR szDisplayName[MAX_DEVICE_DISPLAYNAME_LEN]; INT nClassImage; TVINSERTSTRUCTW tvItem;
GetDeviceDisplayInfo(DevInst, pHotplugData, szDisplayName, ARRAYSIZE(szDisplayName), &nClassImage);
ZeroMemory(&tvItem, sizeof(tvItem)); tvItem.hParent = hParent; tvItem.hInsertAfter = TVI_LAST;
tvItem.item.mask = TVIF_STATE | TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE; tvItem.item.state = TVIS_EXPANDED; tvItem.item.stateMask = TVIS_EXPANDED; tvItem.item.pszText = szDisplayName; tvItem.item.iImage = nClassImage; tvItem.item.iSelectedImage = nClassImage; tvItem.item.lParam = (LPARAM)DevInst;
return TreeView_InsertItem(pHotplugData->hwndDeviceTree, &tvItem);}
staticVOIDDevTreeRecursiveInsertSubDevices( _In_ HTREEITEM hParentItem, _In_ DEVINST ParentDevInst, _In_ PHOTPLUG_DATA pHotplugData){ HTREEITEM hTreeItem; DEVINST ChildDevInst; CONFIGRET cr;
cr = CM_Get_Child(&ChildDevInst, ParentDevInst, 0); if (cr != CR_SUCCESS) return;
hTreeItem = InsertDeviceTreeItem(hParentItem, ChildDevInst, pHotplugData); if (hTreeItem != NULL) { DevTreeRecursiveInsertSubDevices(hTreeItem, ChildDevInst, pHotplugData); }
for (;;) { cr = CM_Get_Sibling(&ChildDevInst, ChildDevInst, 0); if (cr != CR_SUCCESS) return;
hTreeItem = InsertDeviceTreeItem(hParentItem, ChildDevInst, pHotplugData); if (hTreeItem != NULL) { DevTreeRecursiveInsertSubDevices(hTreeItem, ChildDevInst, pHotplugData); } }}
VOIDEnumHotpluggedDevices( _In_ PHOTPLUG_DATA pHotplugData){ SP_DEVINFO_DATA did = { 0 }; HDEVINFO hdev; int idev; DWORD dwCapabilities, dwSize; ULONG ulStatus, ulProblem; HTREEITEM hTreeItem; CONFIGRET cr;
TreeView_DeleteAllItems(pHotplugData->hwndDeviceTree);
hdev = SetupDiGetClassDevs(NULL, NULL, 0, DIGCF_ALLCLASSES | DIGCF_PRESENT); if (hdev == INVALID_HANDLE_VALUE) return;
did.cbSize = sizeof(did);
/* Enumerate all the attached devices */ for (idev = 0; SetupDiEnumDeviceInfo(hdev, idev, &did); idev++) { ulStatus = 0; ulProblem = 0;
cr = CM_Get_DevNode_Status(&ulStatus, &ulProblem, did.DevInst, 0); if (cr != CR_SUCCESS) continue;
dwCapabilities = 0, dwSize = sizeof(dwCapabilities); cr = CM_Get_DevNode_Registry_Property(did.DevInst, CM_DRP_CAPABILITIES, NULL, &dwCapabilities, &dwSize, 0); if (cr != CR_SUCCESS) continue;
/* Add devices that require safe removal to the device tree */ if ( (dwCapabilities & CM_DEVCAP_REMOVABLE) && !(dwCapabilities & CM_DEVCAP_DOCKDEVICE) && !(dwCapabilities & CM_DEVCAP_SURPRISEREMOVALOK) && ((dwCapabilities & CM_DEVCAP_EJECTSUPPORTED) || (ulStatus & DN_DISABLEABLE)) && ulProblem == 0) { hTreeItem = InsertDeviceTreeItem(TVI_ROOT, did.DevInst, pHotplugData);
if ((hTreeItem != NULL) && (pHotplugData->dwFlags & HOTPLUG_DISPLAY_DEVICE_COMPONENTS)) { DevTreeRecursiveInsertSubDevices(hTreeItem, did.DevInst, pHotplugData); } } }
SetupDiDestroyDeviceInfoList(hdev);}
staticVOIDInsertConfirmDeviceListItem( _In_ HWND hwndCfmDeviceList, _In_ DEVINST DevInst, _In_ PHOTPLUG_DATA pHotplugData){ WCHAR szDisplayName[MAX_DEVICE_DISPLAYNAME_LEN]; INT nClassImage; LVITEMW lvItem;
GetDeviceDisplayInfo(DevInst, pHotplugData, szDisplayName, ARRAYSIZE(szDisplayName), &nClassImage);
ZeroMemory(&lvItem, sizeof(lvItem)); lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM; lvItem.iItem = ListView_GetItemCount(hwndCfmDeviceList); lvItem.pszText = szDisplayName; lvItem.iImage = nClassImage; lvItem.lParam = (LPARAM)DevInst;
ListView_InsertItem(hwndCfmDeviceList, &lvItem);}
staticVOIDCfmListRecursiveInsertSubDevices( _In_ HWND hwndCfmDeviceList, _In_ DEVINST ParentDevInst, _In_ PHOTPLUG_DATA pHotplugData){ DEVINST ChildDevInst; CONFIGRET cr;
cr = CM_Get_Child(&ChildDevInst, ParentDevInst, 0); if (cr != CR_SUCCESS) return;
InsertConfirmDeviceListItem(hwndCfmDeviceList, ChildDevInst, pHotplugData); CfmListRecursiveInsertSubDevices(hwndCfmDeviceList, ChildDevInst, pHotplugData);
for (;;) { cr = CM_Get_Sibling(&ChildDevInst, ChildDevInst, 0); if (cr != CR_SUCCESS) return;
InsertConfirmDeviceListItem(hwndCfmDeviceList, ChildDevInst, pHotplugData); CfmListRecursiveInsertSubDevices(hwndCfmDeviceList, ChildDevInst, pHotplugData); }}
VOIDCfmListEnumDevices( _In_ HWND hwndCfmDeviceList, _In_ PHOTPLUG_DATA pHotplugData){ DEVINST DevInst;
DevInst = GetDeviceInstForRemoval(pHotplugData); if (DevInst != 0) { InsertConfirmDeviceListItem(hwndCfmDeviceList, DevInst, pHotplugData); CfmListRecursiveInsertSubDevices(hwndCfmDeviceList, DevInst, pHotplugData); }}