-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEnumDevicesDlg.cpp
More file actions
2043 lines (1915 loc) · 61.2 KB
/
EnumDevicesDlg.cpp
File metadata and controls
2043 lines (1915 loc) · 61.2 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2012-2026 Stefan-Mihai MOGA
This file is part of IntelliTask application developed by Stefan-Mihai MOGA.
IntelliTask is an alternative Windows version to the famous Task Manager!
IntelliTask is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Open
Source Initiative, either version 3 of the License, or any later version.
IntelliTask is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
IntelliTask. If not, see <http://www.opensource.org/licenses/gpl-3.0.html>*/
// EnumDevicesDlg.cpp : implementation file
//
#include "stdafx.h"
#include "IntelliTask.h"
#include "afxdialogex.h"
#include "EnumDevicesDlg.h"
// Link required system libraries for device enumeration
#pragma comment(lib, "Cfgmgr32.lib") // Configuration Manager API
#pragma comment(lib, "Setupapi.lib") // Setup API for device installation
#pragma comment(lib, "Rpcrt4.lib") // RPC runtime for GUID operations
/**
* @brief Implementation of the Enumerate Devices dialog
*/
IMPLEMENT_DYNAMIC(CEnumDevicesDlg, CDialogEx)
/**
* @brief Constructor for the Enumerate Devices dialog
* @param pParent Pointer to the parent window (default: nullptr)
*/
CEnumDevicesDlg::CEnumDevicesDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_ENUMDEVICES_DIALOG, pParent)
{
}
/**
* @brief Destructor for the Enumerate Devices dialog
*/
CEnumDevicesDlg::~CEnumDevicesDlg()
{
}
/**
* @brief Exchange data between dialog controls and member variables
* @param pDX Pointer to the CDataExchange object
*/
void CEnumDevicesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
// Map tree view and list view controls to member variables
DDX_Control(pDX, IDC_DEVICES, m_ctrlDevices);
DDX_Control(pDX, IDC_DETAILS, m_ctrlDetails);
}
BEGIN_MESSAGE_MAP(CEnumDevicesDlg, CDialogEx)
ON_WM_DESTROY()
ON_NOTIFY(TVN_SELCHANGED, IDC_DEVICES, &CEnumDevicesDlg::OnSelchangedDevices)
END_MESSAGE_MAP()
/**
* @brief Forward declaration for ListView column insertion helper function
*/
void ListViewInsertColumnText(HWND hListView, const DWORD wIdx,
int wFmt, const TCHAR* pszText, const BOOL bFinal, HWND hDlg);
/**
* @brief Initialize the dialog and enumerate all devices
* @return TRUE unless focus is set to a control
*/
BOOL CEnumDevicesDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set up the details ListView with two columns
ListViewInsertColumnText(GetDlgItem(IDC_DETAILS)->GetSafeHwnd(), 0, 0, _T("Field"), 0, GetSafeHwnd());
ListViewInsertColumnText(GetDlgItem(IDC_DETAILS)->GetSafeHwnd(), 1, 0, _T("Descritption"), 1, GetSafeHwnd());
// Enumerate all devices and populate the tree view
EnumDevices();
// Set up window resizing behavior for controls
VERIFY(m_pWindowResizer.Hook(this));
// Devices tree stays on left, resizes vertically
VERIFY(m_pWindowResizer.SetAnchor(IDC_DEVICES, ANCHOR_LEFT | ANCHOR_TOP | ANCHOR_BOTTOM));
// Details list resizes both horizontally and vertically
VERIFY(m_pWindowResizer.SetAnchor(IDC_DETAILS, ANCHOR_LEFT | ANCHOR_RIGHT | ANCHOR_TOP | ANCHOR_BOTTOM));
// Cancel button stays at bottom right
VERIFY(m_pWindowResizer.SetAnchor(IDCANCEL, ANCHOR_RIGHT | ANCHOR_BOTTOM));
// Restore previous window size if available
const int nWidth = theApp.GetInt(_T("Width"), -1);
const int nHeight = theApp.GetInt(_T("Height"), -1);
if ((-1 != nWidth) && (-1 != nHeight))
{
CRect pWndRect(0, 0, nWidth, nHeight);
MoveWindow(pWndRect, FALSE);
CenterWindow();
UpdateWindow();
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
/**
* @brief Clean up resources and save window size on dialog destruction
*/
void CEnumDevicesDlg::OnDestroy()
{
// Free all allocated device list nodes
FreeAllocDeviceNode();
// Free all device order nodes
FreeAllDeviceOrderNode();
CDialogEx::OnDestroy();
// Save current window dimensions for next time
RECT pWndRect;
GetWindowRect(&pWndRect);
const int nWidth = pWndRect.right - pWndRect.left;
const int nHeight = pWndRect.bottom - pWndRect.top;
theApp.WriteInt(_T("Width"), nWidth);
theApp.WriteInt(_T("Height"), nHeight);
}
/**
* @brief Convert a GUID structure to its string representation
* @param guid The GUID to convert
* @param pData Output buffer to receive the GUID string
*/
void ConvertGUIDToString(const GUID guid, TCHAR* pData)
{
TCHAR szData[30] = { 0 };
TCHAR szTmp[3] = { 0 };
DWORD wLoop;
// Format the first three parts of the GUID (Data1-Data3)
_stprintf(pData, _T("%04X-%02X-%02X-"), guid.Data1, guid.Data2, guid.Data3);
// Format the Data4 array (8 bytes) as hex pairs
for (wLoop = 0; wLoop < 8; wLoop++)
{
// Add separator after the first 2 bytes of Data4
if (wLoop == 2)
_tcscat(szData, _T("-"));
_stprintf(szTmp, _T("%02X"), guid.Data4[wLoop]);
_tcscat(szData, szTmp);
};
// Append the formatted Data4 string to the output
_tcscpy(pData + _tcslen(pData), szData);
};
/**
* @brief Display a system error message in a message box
* @param hWnd Parent window handle for the message box
* @param dwErrorCode System error code to format and display
* @param szFunctionName Name of the function where the error occurred
*/
void ShowErrorMsg(HWND hWnd, const DWORD dwErrorCode, const TCHAR* szFunctionName)
{
void* lpMsgBuf;
// Format the system error message from the error code
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
0L,
dwErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(TCHAR*)&lpMsgBuf,
0,
0L))
return;
// Display the error message in a message box
MessageBox(hWnd, (const TCHAR*)lpMsgBuf, szFunctionName, MB_ICONSTOP | MB_OK);
// Free the buffer allocated by FormatMessage
LocalFree(lpMsgBuf);
};
/**
* @brief Delete an item or all items from a ListView control
* @param hList Handle to the ListView control
* @param wItem Index of the item to delete, or -1 to delete all items
*/
void ListViewDeleteItem(HWND hList, const DWORD wItem)
{
if (wItem == -1)
SendMessage(hList, LVM_DELETEALLITEMS, 0, 0);
else
SendMessage(hList, LVM_DELETEITEM, (WPARAM)wItem, 0);
};
/**
* @brief Set extended styles for a ListView control
* @param hListView Handle to the ListView control
* @param dwStyle Style flags to set, or 0 for default styles
*/
void ListViewSetExtStyle(HWND hListView, const DWORD dwStyle)
{
DWORD style;
//
style = (dwStyle) ? dwStyle :
LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_FLATSB;
SendMessage(hListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)style);
};
/**
* @brief Select a specific item in a ListView control
* @param hListView Handle to the ListView control
* @param iItem Index of the item to select
*/
void ListViewSetSelectItem(HWND hListView, const DWORD iItem)
{
LVITEM lvItem;
lvItem.iItem = iItem;
lvItem.mask = LVIF_STATE;
lvItem.state = LVIS_SELECTED;
SendMessage(hListView, LVM_SETITEMSTATE, iItem, (LPARAM)&lvItem);
};
/**
* @brief Insert a column with text into a ListView control
* @param hListView Handle to the ListView control
* @param wIdx Column index where to insert
* @param wFmt Column format (alignment), or 0 for default left alignment
* @param pszText Column header text
* @param bFinal TRUE if this is the last column (uses wider width)
* @param hDlg Handle to the parent dialog for text measurement
*/
void ListViewInsertColumnText(HWND hListView, const DWORD wIdx,
int wFmt, const TCHAR* pszText, const BOOL bFinal, HWND hDlg)
{
LVCOLUMN column;
TEXTMETRIC text;
int nWidth;
HDC hDC = GetDC(hDlg);
// Calculate column width based on average character width
GetTextMetrics(hDC, &text);
nWidth = text.tmAveCharWidth * 20;
ReleaseDC(hDlg, hDC);
// Default to left alignment if not specified
if (!wFmt)
wFmt = LVCFMT_LEFT;
// Set up column structure
column.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;
column.pszText = (LPWSTR)pszText;
column.fmt = wFmt;
// Final column gets extra width to fill remaining space
if (!bFinal)
column.cx = nWidth;
else
column.cx = nWidth * 20;
// Insert the column and handle errors
if (SendMessage(hListView, LVM_INSERTCOLUMN, wIdx, (LPARAM)(LPLVCOLUMN)&column) == -1)
ShowErrorMsg(hDlg, GetLastError(), _T("InitialListView"));
};
/**
* @brief Add a column with image and text to a ListView control
* @param hWnd Handle to the parent window
* @param nID Control ID of the ListView
* @param wIdx Column index where to insert
* @param wFmt Column format (alignment), or 0 for default left alignment
* @param pszText Column header text
* @param bFinal TRUE if this is the last column (uses wider width)
*/
void ListViewAddColumnImageText(HWND hWnd, const UINT nID, const DWORD wIdx,
int wFmt, TCHAR* pszText, const BOOL bFinal)
{
LVCOLUMN column;
TEXTMETRIC text;
int nWidth;
HWND hListView = GetDlgItem(hWnd, nID);
HDC hDC = GetDC(hWnd);
// int fmt;
GetTextMetrics(hDC, &text);
nWidth = text.tmAveCharWidth * 20;
ReleaseDC(hWnd, hDC);
//
if (!wFmt)
wFmt = LVCFMT_LEFT;
column.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH | LVCF_IMAGE;
column.pszText = pszText;
column.fmt = wFmt;
if (!bFinal)
column.cx = nWidth;
else
column.cx = 700;
if (SendMessage(hListView, LVM_INSERTCOLUMN, wIdx, (LPARAM)(LPLVCOLUMN)&column) == -1)
ShowErrorMsg(hWnd, GetLastError(), _T("InitialListView"));
};
/**
* @brief Insert or set text for a ListView item or subitem
* @param hListView Handle to the ListView control
* @param iItem Item index
* @param iSubItem Subitem (column) index
* @param pszText Text to insert or set
*/
void ListViewInsertItemText(HWND hListView, const int iItem,
const int iSubItem, const TCHAR* pszText)
{
LVITEM item;
//
item.mask = LVIF_TEXT;
item.iItem = iItem;
item.iSubItem = iSubItem;
item.state = 0;
item.stateMask = 0;
item.iImage = 0;
item.lParam = 0;
item.pszText = (LPWSTR)pszText;
item.cchTextMax = (int)_tcslen(pszText);
if (!iSubItem)
SendMessage(hListView, LVM_INSERTITEM, 0, (LPARAM)&item);
else
SendMessage(hListView, LVM_SETITEMTEXT, iItem, (LPARAM)&item);
};
/**
* @brief Get the index of the currently selected item in a ListView
* @param hListView Handle to the ListView control
* @return Index of the selected item, or -1 if no item is selected
*/
int ListViewGetItemSelect(HWND hListView)
{
return (int)SendMessage(hListView, LVM_GETNEXTITEM, (WPARAM) - 1, MAKELPARAM((UINT)LVNI_SELECTED, 0));
};
/**
* @brief Get the total number of items in a ListView
* @param hListView Handle to the ListView control
* @return Number of items in the ListView
*/
int ListViewGetItemCount(HWND hListView)
{
return (int)SendMessage(hListView, LVM_GETITEMCOUNT, 0, 0);
};
/**
* @brief Get the text of a specific subitem from the selected item
* @param hListView Handle to the ListView control
* @param iSubItem Subitem (column) index
* @param pszText Output buffer to receive the text
*/
void ListViewGetItemText(HWND hListView, const int iSubItem, TCHAR* pszText)
{
LVITEM item;
int idx = ListViewGetItemSelect(hListView);
//
if (idx == -1)
return;
//
item.iSubItem = iSubItem;
item.pszText = pszText;
item.cchTextMax = 255;
SendMessage(hListView, LVM_GETITEMTEXT, idx, (LPARAM)&item);
};
/**
* @brief Get the text of a specific item and subitem
* @param hListView Handle to the ListView control
* @param iItem Item index
* @param iSubItem Subitem (column) index
* @param pszText Output buffer to receive the text
*/
void ListViewGetSpecItem(HWND hListView, const int iItem, const int iSubItem, TCHAR* pszText)
{
LVITEM item;
//
if (iItem == -1)// || iItem == 0)
return;
//
item.iSubItem = iSubItem;
item.pszText = pszText;
item.cchTextMax = 255;
SendMessage(hListView, LVM_GETITEMTEXT, iItem, (LPARAM)&item);
};
/**
* @brief Check if a notification is a ListView double-click event
* @param nID Control ID to check
* @param pnmh Pointer to the notification message header
* @return The control ID if it's a double-click event, 0 otherwise
*/
UINT IsListViewDBClkEvent(const UINT nID, NMHDR* pnmh)
{
if (pnmh->idFrom == nID)
{
if (pnmh->code == NM_DBLCLK)
return nID;
};
return 0;
};
/**
* @brief Check if a notification is a ListView click event (left or right)
* @param nID Control ID to check
* @param pnmh Pointer to the notification message header
* @return NM_CLICK for left click, NM_RCLICK for right click, 0 otherwise
*/
UINT IsListViewClkEvent(const UINT nID, NMHDR* pnmh)
{
if (pnmh->idFrom == nID)
{
if (pnmh->code == NM_CLICK)
return NM_CLICK;
else if (pnmh->code == NM_RCLICK)
return NM_RCLICK;
};
return 0;
};
/**
* @brief Remove all items from a ListView control
* @param hListView Handle to the ListView control
*/
void ListViewRemoveAllItems(HWND hListView)
{
LRESULT wCnt = SendMessage(hListView, LVM_GETITEMCOUNT, 0, 0);
DWORD wLoop;
//
for (wLoop = 0; wLoop < wCnt; wLoop++)
SendMessage(hListView, LVM_DELETEITEM, 0, 0);
SendMessage(hListView, LVM_DELETEALLITEMS, 0, 0);
};
/**
* @brief Remove a specific item from a ListView control
* @param hDlg Handle to the parent dialog
* @param nID Control ID of the ListView
* @param iItem Index of the item to remove
*/
void ListViewRemoveItem(HWND hDlg, const UINT nID, const int iItem)
{
SendMessage(GetDlgItem(hDlg, nID), LVM_DELETEITEM, (WPARAM)iItem, 0);
};
/**
* @brief Remove a specific column from a ListView control
* @param hDlg Handle to the parent dialog
* @param nID Control ID of the ListView
* @param iCol Index of the column to remove
*/
void ListViewRemoveColumn(HWND hDlg, const UINT nID, const int iCol)
{
SendMessage(GetDlgItem(hDlg, nID), LVM_DELETECOLUMN, (WPARAM)iCol, 0);
};
/**
* @brief Remove all nodes from a TreeView control
* @param hDlg Handle to the parent dialog
* @param nIdTree Control ID of the TreeView
*/
void TreeViewRemoveAllNodes(HWND hDlg, const UINT nIdTree)
{
HWND hTree = GetDlgItem(hDlg, nIdTree);
LRESULT wCnt = SendMessage(hDlg, TVM_GETCOUNT, 0, 0);
DWORD wLoop;
for (wLoop = 0; wLoop < wCnt; wLoop)
SendMessage(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
SendMessage(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
};
/**
* @brief Global device class image list data
*/
SP_CLASSIMAGELIST_DATA _spImageData = { 0 };
/**
* @brief Initialize the device class image list
*
* Destroys any existing class image list and retrieves a new one from the system.
*/
void InitialImageData()
{
// HIMAGELIST hImageListView = 0L;
SetupDiDestroyClassImageList(&_spImageData);
RtlZeroMemory(&_spImageData, sizeof(SP_CLASSIMAGELIST_DATA));
_spImageData.cbSize = sizeof(SP_CLASSIMAGELIST_DATA);
SetupDiGetClassImageList(&_spImageData);
};
/**
* @brief Create the root tree item for the device tree
* @param spImageData Device class image list data
* @param nIdTree Control ID of the TreeView
* @param nIdBmp Bitmap resource ID for the root icon
* @param hDlg Handle to the parent dialog
* @return Handle to the root tree item, or NULL on failure
*/
HTREEITEM MakeDeviceRootTree(SP_CLASSIMAGELIST_DATA spImageData, const UINT nIdTree, const UINT nIdBmp, HWND hDlg)
{
TVINSERTSTRUCT tvStruct = { 0 };
// HIMAGELIST hImageListTree = 0L;
HTREEITEM hTreeItem = 0L;
HBITMAP hBitmap = 0L;
DWORD nIndex = MAX_PATH - 1;
TCHAR szName[MAX_PATH] = { 0 };
//
GetComputerName(szName, &nIndex);
hBitmap = LoadBitmap(nullptr, MAKEINTRESOURCE(nIdBmp));
nIndex = ImageList_Add(spImageData.ImageList, hBitmap, 0L);
DeleteObject(hBitmap);
SendMessage(GetDlgItem(hDlg, nIdTree), TVM_SETIMAGELIST,
TVSIL_NORMAL, (LPARAM)spImageData.ImageList);
if (ImageList_GetImageCount(spImageData.ImageList) != -1)
{
tvStruct.hParent = 0L;
tvStruct.hInsertAfter = TVI_ROOT;
tvStruct.item.mask = TVIF_IMAGE | TVIF_TEXT | TVIF_SELECTEDIMAGE;
tvStruct.item.pszText = szName;
tvStruct.item.iImage = nIndex;
tvStruct.item.iSelectedImage = nIndex;
hTreeItem = (HTREEITEM)SendMessage(GetDlgItem(hDlg, nIdTree),
TVM_INSERTITEM, 0, (LPARAM)&tvStruct);
return hTreeItem;
};
return 0;
};
/**
* @brief Global pointer to the head of the device list
*/
DEVICE_LIST* _pHead = 0L;
/**
* @brief Global pointer to the head of the device order list
*/
DEVICE_ORDER* _pOrderHead = 0L;
DEVICE_ORDER* AllocNewDeviceOrderNode(HWND hDlg);
/**
* @brief Initialize the device order list
* @param hDlg Handle to the parent dialog
* @return 1 on success, 0 on failure
*/
char InitialDeviceOrder(HWND hDlg)
{
_pOrderHead = AllocNewDeviceOrderNode(hDlg);
return (_pOrderHead) ? 1 : 0;
};
/**
* @brief Allocate a new device order node
* @param hDlg Handle to the parent dialog for error messages
* @return Pointer to the new node, or NULL on failure
*/
DEVICE_ORDER* AllocNewDeviceOrderNode(HWND hDlg)
{
DEVICE_ORDER* pNew = (DEVICE_ORDER*)LocalAlloc(LPTR, sizeof(DEVICE_ORDER));
//
if (!pNew)
{
ShowErrorMsg(hDlg, GetLastError(), _T("LocalAlloc"));
return 0;
};
RtlZeroMemory(pNew->szDevName, sizeof(TCHAR) * LINE_LEN);
pNew->pNext = 0L;
return pNew;
};
/**
* @brief Add a new device order node to the list
* @param szDevName Device name to store in the node
* @param hDlg Handle to the parent dialog for error messages
* @return 1 on success, 0 on failure
*/
char AddNewDeviceOrderNode(const TCHAR* szDevName, HWND hDlg)
{
// Allocate a new device order node
DEVICE_ORDER* pAdd = AllocNewDeviceOrderNode(hDlg);
if (!pAdd)
return 0;
// Copy device name into the node
_tcscpy(pAdd->szDevName, szDevName);
// Insert at the head of the list (after the dummy head node)
pAdd->pNext = _pOrderHead->pNext;
_pOrderHead->pNext = pAdd;
return 1;
};
/**
* @brief Find the order/count of a device by name
* @param szName Device name to search for
* @return Number of times the device name appears in the order list
*/
DWORD FindDeviceOrder(const TCHAR* szName)
{
DEVICE_ORDER* pList = _pOrderHead->pNext;
DWORD wOrder = 0;
// Traverse the list and count how many times this device name appears
// This determines the order index for devices with the same class
while (pList)
{
if (!_tcscmp(pList->szDevName, szName))
wOrder++;
pList = pList->pNext;
};
return wOrder;
};
/**
* @brief Free all device order nodes and cleanup the list
*/
void FreeAllDeviceOrderNode()
{
DEVICE_ORDER* pDel = _pOrderHead->pNext;
DEVICE_ORDER* pTmp = 0L;
// Traverse the list and free each node
while (pDel->pNext)
{
pTmp = pDel;
pDel = pDel->pNext;
LocalFree(pTmp);
};
// Free the dummy head node
LocalFree(_pOrderHead);
_pOrderHead = 0L;
};
/**
* @brief Initialize the device list
* @param hDlg Handle to the parent dialog for error messages
* @return 1 on success, 0 on failure
*/
char InitialDeviceList(HWND hDlg)
{
_pHead = AllocNewDeviceNode(hDlg);
return (_pHead) ? 1 : 0;
};
/**
* @brief Allocate a new device list node
* @param hDlg Handle to the parent dialog for error messages
* @return Pointer to the new node, or NULL on failure
*/
DEVICE_LIST* AllocNewDeviceNode(HWND hDlg)
{
DEVICE_LIST* pNew = (DEVICE_LIST*)LocalAlloc(LPTR, sizeof(DEVICE_LIST));
//
if (!pNew)
{
ShowErrorMsg(hDlg, GetLastError(), _T("LocalAlloc"));
return 0;
};
RtlZeroMemory(&pNew->guid, sizeof(GUID));
RtlZeroMemory(pNew->szInstallID, sizeof(char) * LINE_LEN);
RtlZeroMemory(pNew->szName, sizeof(char) * MAX_PATH);
RtlZeroMemory(pNew->szPath, sizeof(char) * MAX_PATH);
pNew->wOrder = (DWORD) - 1;
pNew->wIndex = (DWORD) - 1;
pNew->pNext = 0L;
return pNew;
};
/**
* @brief Add a new device node to the device list
* @param guid Device class GUID
* @param szName Device name
* @param szInstallID Device installation ID
* @param szPath Device path
* @param wIndex Device index
* @param wOrder Device order
* @param hDlg Handle to the parent dialog for error messages
* @return 1 on success, 0 on failure
*/
char AddNewDeviceNode(const GUID guid,
const TCHAR* szName,
const TCHAR* szInstallID,
const TCHAR* szPath,
const DWORD wIndex,
const DWORD wOrder,
HWND hDlg)
{
DEVICE_LIST* pAdd = AllocNewDeviceNode(hDlg);
//
if (!pAdd)
return 0;
memcpy(&pAdd->guid, &guid, sizeof(GUID));
_tcscpy(pAdd->szInstallID, szInstallID);
_tcscpy(pAdd->szName, szName);
_tcscpy(pAdd->szPath, szPath);
pAdd->wIndex = wIndex;
pAdd->wOrder = wOrder;
pAdd->pNext = _pHead->pNext;
_pHead->pNext = pAdd;
return 1;
};
/**
* @brief Free all device nodes and cleanup the device list
*/
void FreeAllocDeviceNode()
{
DEVICE_LIST* pDel = _pHead->pNext;
DEVICE_LIST* pTmp = 0L;
//
while (pDel->pNext)
{
pTmp = pDel;
pDel = pDel->pNext;
LocalFree(pTmp);
};
LocalFree(_pHead);
_pHead = 0L;
};
/**
* @brief Get and display detailed information about a device
* @param pList Pointer to the device list node
* @param hDlg Handle to the parent dialog
*/
void GetDeviceDetailInfo(DEVICE_LIST* pList, HWND hDlg)
{
TCHAR szBuf[MAX_PATH] = { 0 };
HWND hList = GetDlgItem(hDlg, IDC_DETAILS);
ConvertGUIDToString(pList->guid, szBuf);
ListViewInsertItemText(hList, 0, 1, szBuf);
ListViewInsertItemText(hList, 2, 1, pList->szInstallID);
_stprintf(szBuf, _T("%d"), pList->wIndex);
ListViewInsertItemText(hList, 10, 1, szBuf);
ListViewInsertItemText(hList, 11, 1, pList->szPath);
};
/**
* @brief Display driver detail information in the tree view
* @param hTreeChild Handle to the parent tree item
* @param nID Control ID of the TreeView
* @param szBuf Text to display
* @param iImageIdx Image index for the item
* @param iSelectImage Selected image index (unused)
* @param hDlg Handle to the parent dialog
*/
void DisplayDriverDetailInfo(HTREEITEM hTreeChild, const UINT nID, const TCHAR* szBuf, const int iImageIdx, const int /*iSelectImage*/, HWND hDlg)
{
TVINSERTSTRUCT tvStruct = { 0 };
HWND hTree = GetDlgItem(hDlg, nID);
tvStruct.hParent = hTreeChild;
tvStruct.hInsertAfter = TVI_LAST;
tvStruct.item.mask = TVIF_IMAGE | TVIF_TEXT | TVIF_SELECTEDIMAGE | TVIF_HANDLE;
tvStruct.item.mask |= TVIF_PARAM;
tvStruct.item.lParam = 1;
tvStruct.item.pszText = (LPWSTR)szBuf;
tvStruct.item.iImage = iImageIdx;
tvStruct.item.iSelectedImage = iImageIdx;
SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)&tvStruct);
};
/**
* @brief Get the device instance ID
* @param hDevInfo Device information set handle
* @param pspDevInfoData Pointer to device information data
* @param szInstanceID Output buffer for the instance ID
* @param hDlg Handle to the parent dialog for error messages
*/
void GetDeviceInstanceID(HDEVINFO hDevInfo,
SP_DEVINFO_DATA* pspDevInfoData,
TCHAR* szInstanceID, HWND hDlg)
{
if (!SetupDiGetDeviceInstanceId(hDevInfo,
pspDevInfoData,
szInstanceID,
LINE_LEN,
0))
ShowErrorMsg(hDlg, GetLastError(), _T("SetupDiBuildDriverInfoList"));
};
/**
* @brief Insert text into a TreeView as a root or child item
* @param hWnd Handle to the parent window
* @param nID Control ID of the TreeView
* @param hParent Handle to the parent tree item, or NULL for root
* @param hAfter Handle to the item after which to insert, or NULL
* @param pszText Text to display in the tree item
* @return Handle to the newly inserted tree item
*/
HTREEITEM TreeViewInsertRootText(HWND hWnd, UINT nID, HTREEITEM hParent, HTREEITEM hAfter, TCHAR* pszText)
{
TVINSERTSTRUCT tvInsert;
HWND hTree = GetDlgItem(hWnd, nID);
//
if (!hParent && !hAfter)
{
tvInsert.hParent = 0L;
tvInsert.hInsertAfter = 0L;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = (LPWSTR)pszText;
return (HTREEITEM)SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)&tvInsert);
};
tvInsert.hParent = hParent;
tvInsert.hInsertAfter = hAfter;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = pszText;
return (HTREEITEM)SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)&tvInsert);
};
/**
* @brief Find a child item in a TreeView by text
* @param hTreeView Handle to the TreeView control
* @param hRoot Handle to the root item to search from
* @param szBuf Text to search for
* @return Handle to the found tree item, or NULL if not found
*/
HTREEITEM TreeViewFindChild(HWND hTreeView, HTREEITEM hRoot,
const TCHAR* szBuf)
{
HTREEITEM hChildItem = 0L;
HTREEITEM hParentItem = hRoot;
TCHAR szText[128] = { 0 };
TVITEM tvItem = { 0 };
int iLoop;
int nIndex;
tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
tvItem.pszText = szText;
tvItem.cchTextMax = sizeof(char) * 127;
nIndex = (int)SendMessage(hTreeView, TVM_GETCOUNT, 0, 0);
RtlZeroMemory(szText, sizeof(char) * 128);
for (iLoop = 0; iLoop < nIndex; iLoop++)
{
hChildItem = (HTREEITEM)SendMessage(hTreeView,
TVM_GETNEXTITEM,
(iLoop) ? TVGN_NEXT : TVGN_CHILD,
(iLoop) ? (LPARAM)hParentItem : (LPARAM)hRoot);
tvItem.hItem = hChildItem;
SendMessage(hTreeView, TVM_GETITEM, 0, (LPARAM)&tvItem);
if (*szText)
{
if (!_tcscmp(szBuf, szText))
return hChildItem;
};
hParentItem = hChildItem;
};
return 0;
};
/**
* @brief Check if a notification is a TreeView click event
* @param nID Control ID to check
* @param pnmh Pointer to the notification message header
* @return NM_CLICK for left click, NM_RCLICK for right click, 0 otherwise
*/
UINT IsTreeViewClkEvent(const UINT nID, NMHDR* pnmh)
{
if (pnmh->idFrom == nID)
{
if (pnmh->code == NM_CLICK)
return NM_CLICK;
else if (pnmh->code == NM_RCLICK)
return NM_RCLICK;
};
return 0;
};
/**
* @brief Check if a notification is a TreeView selection changed event
* @param nID Control ID to check
* @param pnmh Pointer to the notification message header
* @return TVN_SELCHANGED if selection changed, 0 otherwise
*/
UINT IsTreeViewSelectChanged(const UINT nID, NMHDR* pnmh)
{
if (pnmh->code == TVN_SELCHANGED && pnmh->idFrom == nID)
return TVN_SELCHANGED;
return 0;
};
/**
* @brief Expand or collapse a TreeView item
* @param hTree Handle to the TreeView control
* @param hTreeItem Handle to the tree item
* @param bExpand TRUE to expand, FALSE to collapse
*/
void TreeViewExpand(HWND hTree, HTREEITEM hTreeItem, const BOOL bExpand)
{
UINT flags = (bExpand) ? TVE_EXPAND : TVE_COLLAPSE;
//
SendMessage(hTree, TVM_EXPAND, (WPARAM)flags, (LPARAM)hTreeItem);
};
/**
* @brief Get the text of the selected TreeView item
* @param hTree Handle to the TreeView control
* @param pnmh Pointer to the notification message header
* @param pItem Pointer to TVITEM structure to receive the item information
*/
void TreeViewGetSelectText(HWND hTree, NMHDR* pnmh, TVITEM* pItem)
{
NMTREEVIEW* pnmView = (NMTREEVIEW*)pnmh;
TCHAR pszText[MAX_PATH] = { 0 };
//
pItem->mask = TVIF_TEXT;
pItem->hItem = pnmView->itemNew.hItem;
do
{
pItem->pszText = pszText;
pItem->cchTextMax = MAX_PATH - 1;
// SendMessage(hTree, TVM_GETNEXTITEM, TVGN_CARET, (LPARAM)pItem);
SendMessage(hTree, TVM_GETITEM, 0, (LPARAM)pItem);
if (_tcslen(pItem->pszText))
break;
} while (1);
};
/**
* @brief Enumerate all WDM (Windows Driver Model) drivers and populate the device tree
* @param nIdTree Control ID of the TreeView
* @param nIdBmp Bitmap resource ID for the root icon
* @param hDlg Handle to the parent dialog
* @return 1 on success, 0 on failure
*/
char EnumWDMDriver(const UINT nIdTree, const UINT nIdBmp, HWND hDlg)
{
HDEVINFO hDevInfo = 0L;
SP_DEVINFO_DATA spDevInfoData = { 0 };
DWORD wIndex = 0;
HTREEITEM hTreeChild = 0L;
//
hTreeChild = MakeDeviceRootTree(_spImageData, nIdTree, nIdBmp, hDlg);
if (!hTreeChild)
return 0;
//
hDevInfo = SetupDiGetClassDevs(0L, 0L, hDlg, DIGCF_PRESENT |
DIGCF_ALLCLASSES | DIGCF_PROFILE);
if (hDevInfo == (void*)-1)
{
ShowErrorMsg(hDlg, GetLastError(), _T("SetupDiGetClassDevs"));
return 0;
};
//
wIndex = 0;
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
SendMessage(GetDlgItem(hDlg, nIdTree), TVM_SETIMAGELIST,
TVSIL_NORMAL, (LPARAM)_spImageData.ImageList);
//
while (1)
{
if (SetupDiEnumDeviceInfo(hDevInfo,
wIndex,
&spDevInfoData))
{
TCHAR szBuf[MAX_PATH] = { 0 };
int wImageIdx = 0;
// DWORD wItem = 0;
if (!SetupDiGetDeviceRegistryProperty(hDevInfo,
&spDevInfoData,
SPDRP_CLASS, //SPDRP_DEVICEDESC,
0L,
(PBYTE)szBuf,
2048,
0))
{
wIndex++;
continue;
};
//
if (SetupDiGetClassImageIndex(&_spImageData,
&spDevInfoData.ClassGuid,
&wImageIdx))
{
TVINSERTSTRUCT tvStruct = { 0 };
HWND hTree = GetDlgItem(hDlg, nIdTree);
TCHAR szName[64] = { 0 };
TCHAR szID[LINE_LEN] = { 0 };
TCHAR szPath[MAX_PATH] = { 0 };
HTREEITEM hItem;
DWORD dwRequireSize;
DWORD wOrder;
//
if (!SetupDiGetClassDescription(&spDevInfoData.ClassGuid,
szBuf,
MAX_PATH,
&dwRequireSize))
{
wIndex++;
continue;
};