-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceExplorer.bas
430 lines (359 loc) · 15.6 KB
/
DeviceExplorer.bas
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
'DeviceExplorer.bas
' Copyright (c) 2024 CM.Wang
' Freeware. Use at your own risk.
' 通过windows api实现如device manager一样的update driver, uninstall device, eject device的功能
' 翻译了cfgmgr32, devguid, devpkey, devpropdef, newdev等相关头文件
' https://learn.microsoft.com/en-us/windows/win32/devinst/setupapi-h
' 参考了TwinBasic的样例
' https://github.com/fafalone/DeviceExplorer
#include once "DeviceExplorer.bi"
Private Sub pvRelase()
Dim i As Integer
For i = 0 To categoriesCount
If categoriesName(i) Then Deallocate(categoriesName(i))
If categoriesDescription(i) Then Deallocate(categoriesDescription(i))
Next
Erase categoriesHSet
Erase categoriesDevInfo
Erase categoriesGuid
Erase categoriesName
Erase categoriesDescription
categoriesCount = -1
For i = 0 To devicesCount
If devicesFriendlyName(i) Then Deallocate(devicesFriendlyName(i))
If devicesDescription(i) Then Deallocate(devicesDescription(i))
If devicesInstanceId(i) Then Deallocate(devicesInstanceId(i))
If devicesHardwareId(i) Then Deallocate(devicesHardwareId(i))
If devicesDriver(i) Then Deallocate(devicesDriver(i))
Next
Erase devicesIndexCategories
Erase devicesGUID
Erase devicesCapabilities
Erase devicesEnabled
Erase devicesPresent
Erase devicesProblem
Erase devicesStatus
Erase devicesFriendlyName
Erase devicesHardwareId
Erase devicesDriver
Erase devicesDescription
Erase devicesInstanceId
devicesCount = -1
End Sub
Private Sub pvInit()
Dim pIIDL As ITEMIDLIST Ptr
Dim pFileInfo As SHFILEINFO
SHGetSpecialFolderLocation(NULL, CSIDL_SYSTEMX86, @pIIDL)
SHGetPathFromIDList(pIIDL, @SystemPath)
End Sub
Private Sub pvInitIcon(tv As TreeView Ptr)
'初始化ImageList, 并从setupapi.dll文件获得Overlay Icon
'清空ImageList Icon
ImageList_Remove tv->Images->Handle, -1
Dim hico As HANDLE
Dim i As Integer
Dim j As Integer = 1
Dim k As Integer = ExtractIconEx(SystemPath & "\setupapi.dll", -1, 0, NULL, 0)
k -= 1
Dim o As Long
'最后3个图标是Overlay icon.
For i = k - 2 To k
ExtractIconEx(SystemPath & "\setupapi.dll", i, 0, @hico, 1)
o = ImageList_ReplaceIcon(tv->Images->Handle, -1, hico)
ImageList_SetOverlayImage(tv->Images->Handle, o, j)
DestroyIcon(hico)
j += 1
Next
End Sub
Private Function pvIndexBydevicesInstanceId(hwndParent As HWND, pGuid As GUID Ptr, pInstanceId As WString Ptr, ShowHide As Boolean, ByRef hSet As HDEVINFO, ByRef pDevInfo As PSP_DEVINFO_DATA) As BOOL
Dim i As Long = 0
Dim ret As BOOL
Dim cchReq As DWORD = 0
hSet = SetupDiGetClassDevs(pGuid, NULL, hwndParent, IIf(ShowHide, 0, DIGCF_PRESENT))
If hSet = INVALID_HANDLE_VALUE Then Return False
memset(pDevInfo, 0, SizeOf(SP_DEVINFO_DATA))
pDevInfo->cbSize = SizeOf(SP_DEVINFO_DATA)
Dim tInstanceId As WString Ptr
Do While SetupDiEnumDeviceInfo(hSet, i, pDevInfo)
ret = SetupDiGetDeviceInstanceId(hSet, pDevInfo, NULL, NULL, @cchReq)
If cchReq Then
tInstanceId = CAllocate(cchReq * 2, SizeOf(Byte))
ret = SetupDiGetDeviceInstanceId(hSet, pDevInfo, tInstanceId, cchReq, @cchReq)
If *tInstanceId = *pInstanceId Then
If tInstanceId Then Deallocate(tInstanceId)
Return True
End If
End If
i += 1
memset(pDevInfo, 0, SizeOf(SP_DEVINFO_DATA))
pDevInfo->cbSize = SizeOf(SP_DEVINFO_DATA)
Loop
If tInstanceId Then Deallocate(tInstanceId)
Return False
End Function
Private Function pvEnumClasses(hwndParent As HWND, tv As TreeView Ptr, ShowCategories As Boolean, ShowHide As Boolean) As Integer
Dim ret As BOOL
Dim cbReq As DWORD
tv->Nodes.Clear
pvRelase()
cbReq = 0
'SetupDiBuildClassInfoList返回本地计算机上安装的设备类别的 GUID 列表
ret = SetupDiBuildClassInfoList(NULL, NULL, 0, @cbReq)
categoriesCount = cbReq - 1
EnumCCount = 0
EnumDCount = 0
If cbReq < 1 Then Return 0
ReDim categoriesHSet(categoriesCount)
ReDim categoriesDevInfo(categoriesCount)
ReDim categoriesGuid(categoriesCount)
ReDim categoriesName(categoriesCount)
ReDim categoriesDescription(categoriesCount)
Dim cchReq As DWORD = 0
ret = SetupDiBuildClassInfoList(NULL, @categoriesGuid(0), cbReq, @cchReq)
If ret = False Then Return 0
Dim i As Long
Dim j As Integer = 0
Dim mIndex As DWORD
Dim regType As DWORD = REGTYPES.REG_NONE
Dim hicn As HICON
Dim ico As Long
Dim cbiReq As Integer
Dim nPropType As DEVPROPTYPE = DEVPROP_TYPE_BOOLEAN
For i = 0 To categoriesCount
categoriesHSet(i) = SetupDiGetClassDevs(@categoriesGuid(i), NULL, hwndParent, IIf(ShowHide, 0, DIGCF_PRESENT))
If categoriesHSet(i) = INVALID_HANDLE_VALUE Then Continue For
cchReq = 0
'SetupDiClassNameFromGuid根据设备类别 GUID 获取设备类名称
ret = SetupDiClassNameFromGuid(@categoriesGuid(i), NULL, NULL, @cchReq)
If cchReq Then
categoriesName(i) = CAllocate(cchReq * 2, SizeOf(Byte))
ret = SetupDiClassNameFromGuid(@categoriesGuid(i), categoriesName(i), cchReq, @cchReq)
End If
cchReq = 0
'SetupDiGetClassDescription获取设备类的描述
ret = SetupDiGetClassDescription(@categoriesGuid(i), NULL, NULL, @cchReq)
If cchReq Then
categoriesDescription(i) = CAllocate(cchReq * 2, SizeOf(Byte))
ret = SetupDiGetClassDescription(@categoriesGuid(i), categoriesDescription(i), cchReq, @cchReq)
End If
Dim pTNode As TreeNode Ptr = NULL
'显示所有设备类别
If ShowCategories Then
EnumCCount += 1
ret = SetupDiLoadClassIcon(@categoriesGuid(i), @hicn, NULL)
ico = ImageList_ReplaceIcon(tv->Images->Handle, -1, hicn)
DestroyIcon(hicn)
pTNode = tv->Nodes.Add(*categoriesDescription(i), WStr(i), WStr("Categories"), ico, ico)
End If
mIndex = 0
memset(@categoriesDevInfo(i), 0, SizeOf(categoriesDevInfo(i)))
categoriesDevInfo(i).cbSize = SizeOf(categoriesDevInfo(i))
'SetupDiEnumDeviceInfo枚举设备信息集中的设备信息元素
Do While SetupDiEnumDeviceInfo(categoriesHSet(i), mIndex, @categoriesDevInfo(i))
'只显示有设备的设备类别
If pTNode = NULL Then
EnumCCount += 1
ret = SetupDiLoadClassIcon(@categoriesGuid(i), @hicn, NULL)
ico = ImageList_ReplaceIcon(tv->Images->Handle, -1, hicn)
DestroyIcon(hicn)
pTNode = tv->Nodes.Add(*categoriesDescription(i), WStr(i), WStr("Categories"), ico, ico)
End If
j += 1
ReDim Preserve devicesIndexCategories(j)
ReDim Preserve devicesGUID(j)
ReDim Preserve devicesCapabilities(j)
ReDim Preserve devicesStatus(j)
ReDim Preserve devicesProblem(j)
ReDim Preserve devicesPresent(j)
ReDim Preserve devicesEnabled(j)
ReDim Preserve devicesFriendlyName(j)
ReDim Preserve devicesHardwareId(j)
ReDim Preserve devicesDescription(j)
ReDim Preserve devicesInstanceId(j)
ReDim Preserve devicesDriver(j)
devicesIndexCategories(j) = i
'SetupDiGetDeviceInstanceId获取设备实例 ID。这个 ID 是一个唯一的字符串,用于标识系统中的每个设备实例。
cchReq = 0
ret = SetupDiGetDeviceInstanceId(categoriesHSet(i), @categoriesDevInfo(i), NULL, 0, @cchReq)
If cchReq Then
devicesInstanceId(j) = CAllocate(cchReq * 2, SizeOf(Byte))
ret = SetupDiGetDeviceInstanceId(categoriesHSet(i), @categoriesDevInfo(i), devicesInstanceId(j), cchReq, @cchReq)
End If
'SetupDiGetDeviceRegistryProperty从设备的信息集中检索设备的注册表属性
cchReq = 0
regType= 0
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_CLASSGUID, @regType, NULL, 0, @cchReq)
If cchReq Then
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_CLASSGUID, @regType, Cast(UByte Ptr, @devicesGUID(j)), SizeOf(devicesGUID(j)), @cchReq)
End If
cchReq = 0
regType= 0
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_CAPABILITIES, @regType, NULL, 0, @cchReq)
If cchReq Then
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_CAPABILITIES, @regType, Cast(UByte Ptr, @devicesCapabilities(j)), SizeOf(devicesCapabilities(j)), @cchReq)
End If
cchReq = 0
regType= 0
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_HARDWAREID, @regType, NULL, 0, @cchReq)
If cchReq Then
devicesHardwareId(j) = CAllocate(cchReq * 2, SizeOf(Byte))
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_HARDWAREID, @regType, Cast(PBYTE, devicesHardwareId(j)), cchReq, @cchReq)
End If
cchReq = 0
regType= 0
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_FRIENDLYNAME, @regType, NULL, 0, @cchReq)
If cchReq Then
devicesFriendlyName(j) = CAllocate(cchReq * 2, SizeOf(Byte))
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_FRIENDLYNAME, @regType, Cast(PBYTE, devicesFriendlyName(j)), cchReq, @cchReq)
End If
cchReq = 0
regType= 0
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_DEVICEDESC, @regType, NULL, 0, @cchReq)
If cchReq Then
devicesDescription(j) = CAllocate(cchReq * 2, SizeOf(Byte))
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_DEVICEDESC, @regType, Cast(PBYTE, devicesDescription(j)), cchReq, @cchReq)
End If
cchReq = 0
regType= 0
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_DRIVER, @regType, NULL, 0, @cchReq)
If cchReq Then
devicesDriver(j) = CAllocate(cchReq * 2, SizeOf(Byte))
ret = SetupDiGetDeviceRegistryProperty(categoriesHSet(i), @categoriesDevInfo(i), SPDRP_DRIVER, @regType, Cast(PBYTE, devicesDriver(j)), cchReq, @cchReq)
End If
ret = SetupDiLoadDeviceIcon(categoriesHSet(i), @categoriesDevInfo(i), 16, 16, NULL, @hicn)
ico = ImageList_ReplaceIcon(tv->Images->Handle, -1, hicn)
DestroyIcon(hicn)
Dim fPresent As Integer
Dim dwStatus As CfgMgDevNodeStatus = 0
Dim nProbCode As CfgMgrProblems = 0
Dim bProblem As Boolean = False
If CM_Get_DevNode_Status(@dwStatus, @nProbCode, categoriesDevInfo(i).DevInst, 0) = CR_SUCCESS Then
If nProbCode<> 0 Then bProblem = True
If dwStatus And DN_HAS_PROBLEM Then
bProblem = True
'Dim p As WString Ptr
'Dim s As DWORD = 0
's = DeviceProblemText(0, categoriesDevInfo(i).DevInst, nProbCode, NULL, NULL)
'p = CAllocate(s * 4, SizeOf(Byte))
'DeviceProblemText(0, categoriesDevInfo(i).DevInst, nProbCode, p, @s)
'If p Then Deallocate(p)
End If
devicesProblem(j) = nProbCode
devicesStatus(j) = dwStatus
End If
If ShowHide Then
fPresent = False
ret = SetupDiGetDeviceProperty(categoriesHSet(i), @categoriesDevInfo(i), @DEVPKEY_Device_IsPresent, nPropType, @fPresent, SizeOf(fPresent), @cbiReq, 0)
Else
fPresent = True
End If
Dim dwState As DWORD = 0
Dim dwMask As DWORD = 0
devicesEnabled(j) = True
If bProblem Then
Select Case nProbCode
Case CM_PROB_DISABLED
devicesEnabled(j) = False
dwState = INDEXTOOVERLAYMASK(3)
Case CM_PROB_DEVICE_NOT_THERE
dwState = INDEXTOOVERLAYMASK(1)
fPresent = False
Case Else
dwState = INDEXTOOVERLAYMASK(2)
End Select
dwMask = TVIS_OVERLAYMASK
End If
If fPresent = False Then
dwState = dwState Or TVIS_CUT
dwMask = dwMask Or TVIS_CUT
End If
devicesPresent(j) = fPresent
'显示设备
Dim sTNode As TreeNode Ptr
If *devicesFriendlyName(j) = "" Then
sTNode = pTNode->Nodes.Add(*devicesDescription(j), WStr(j), WStr("Devices"), ico, ico)
Else
sTNode = pTNode->Nodes.Add(*devicesFriendlyName(j), WStr(j), WStr("Devices"), ico, ico)
End If
If dwMask Then
TreeView_SetItemState(tv->Handle, sTNode->Handle, dwState, dwMask)
End If
mIndex += 1
memset(@categoriesDevInfo(i), 0, SizeOf(categoriesDevInfo(i)))
categoriesDevInfo(i).cbSize = SizeOf(categoriesDevInfo(i))
Loop
If categoriesHSet(i) Then SetupDiDestroyDeviceInfoList(categoriesHSet(i))
Next
EnumDCount = j
Return EnumCCount + EnumDCount
End Function
Private Function pvEjectDevice(hwndParent As HWND, idx As Integer) As BOOL
Dim lpVeto As PNP_VETO_TYPE
Dim lRet As CONFIGRET
lRet = CM_Request_Device_Eject(categoriesDevInfo(devicesIndexCategories(idx)).DevInst, lpVeto, devicesInstanceId(idx), MAX_PATH, 0)
Return IIf(lRet = CR_SUCCESS, True, False)
End Function
Private Function pvEnableDevice(hwndParent As HWND, idx As Integer, fEnable As BOOL, ShowHide As Boolean) As BOOL
Dim ret As BOOL
Dim hSet As HDEVINFO
Dim cDevInfo As SP_DEVINFO_DATA
ret = pvIndexBydevicesInstanceId(hwndParent, @categoriesGuid(devicesIndexCategories(idx)), devicesInstanceId(idx), ShowHide, hSet, @cDevInfo)
If ret Then
Dim tParams As SP_PROPCHANGE_PARAMS
Dim ret As BOOL
tParams.ClassInstallHeader.cbSize = SizeOf(SP_CLASSINSTALL_HEADER)
tParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE
tParams.StateChange = IIf(fEnable, DICS_ENABLE, DICS_DISABLE)
tParams.Scope = DICS_FLAG_GLOBAL 'DICS_FLAG_CONFIGSPECIFIC
'tParams.Scope = DICS_FLAG_GLOBAL
ret = SetupDiSetClassInstallParams(hSet, @cDevInfo, @tParams.ClassInstallHeader, SizeOf(SP_PROPCHANGE_PARAMS))
If ret Then
ret = SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hSet, @cDevInfo)
End If
End If
If hSet <> NULL Or hSet <> INVALID_HANDLE_VALUE Then SetupDiDestroyDeviceInfoList(hSet)
Return ret
End Function
Private Function pvRemoveDevice(hwndParent As HWND, idx As Integer, ShowHide As Boolean) As BOOL
Dim ret As BOOL
Dim hSet As HDEVINFO
Dim cDevInfo As SP_DEVINFO_DATA
ret = pvIndexBydevicesInstanceId(hwndParent, @categoriesGuid(devicesIndexCategories(idx)), devicesInstanceId(idx), ShowHide, hSet, @cDevInfo)
If ret Then
Dim tParams As SP_REMOVEDEVICE_PARAMS
tParams.ClassInstallHeader.cbSize = SizeOf(SP_CLASSINSTALL_HEADER)
tParams.ClassInstallHeader.InstallFunction = DIF_REMOVE
tParams.Scope = DI_REMOVEDEVICE_GLOBAL
ret = SetupDiSetClassInstallParams(hSet, @cDevInfo, @tParams.ClassInstallHeader, SizeOf(SP_REMOVEDEVICE_PARAMS))
If ret Then
ret = SetupDiCallClassInstaller(DIF_REMOVE, hSet, @cDevInfo)
End If
End If
If hSet <> NULL Or hSet <> INVALID_HANDLE_VALUE Then SetupDiDestroyDeviceInfoList(hSet)
Return ret
End Function
Private Function pvUninstallDevice(hwndParent As HWND, idx As Integer, ShowHide As Boolean) As BOOL
Dim ret As WINBOOL
Dim hSet As HDEVINFO
Dim cDevInfo As SP_DEVINFO_DATA
ret = pvIndexBydevicesInstanceId(hwndParent, @categoriesGuid(devicesIndexCategories(idx)), devicesInstanceId(idx), ShowHide, hSet, @cDevInfo)
If ret Then
ret = SetupDiRemoveDevice(hSet, @cDevInfo)
End If
If hSet <> NULL Or hSet <> INVALID_HANDLE_VALUE Then SetupDiDestroyDeviceInfoList(hSet)
Return ret
End Function
Private Function pvUpdateDevice(hwndParent As HWND, idx As Integer, ShowHide As Boolean) As BOOL
Dim ret As WINBOOL
Dim hSet As HDEVINFO
Dim cDevInfo As SP_DEVINFO_DATA
ret = pvIndexBydevicesInstanceId(hwndParent, @categoriesGuid(devicesIndexCategories(idx)), devicesInstanceId(idx), ShowHide, hSet, @cDevInfo)
If ret Then
ret = SetupDiInstallDevice(hSet, @cDevInfo)
End If
If hSet <> NULL Or hSet <> INVALID_HANDLE_VALUE Then SetupDiDestroyDeviceInfoList(hSet)
Return ret
End Function
Private Function pvShowPropPage(hwndParent As HWND, idx As Integer) As BOOL
Return DevicePropertiesEx(hwndParent, NULL, devicesInstanceId(idx), DEVPROP_SHOW_RESOURCE_TAB, False)
End Function