-
Notifications
You must be signed in to change notification settings - Fork 0
/
hwutil.pas
102 lines (86 loc) · 2.7 KB
/
hwutil.pas
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
unit hwutil;
interface
uses
windows,
setupapilite,
sysutils;
function EnumSerialCommWithFriendlyName(const S: TStrings): Integer;
implementation
function EnumSerialCommWithFriendlyName(const S: TStrings): Integer;
var
Guid: TGUID;
Size: DWORD;
hDevInf: HDEVINFO;
Index: DWORD;
DevInfoData: SP_DEVINFO_DATA;
S1: String;
hRegKey: HKEY;
S2: String;
RegType: DWORD;
PortNo: Integer;
begin
Result := 0;
Size := 0;
if SetupDiClassGuidsFromName('Ports',@Guid,1,Size) = False then
begin
RaiseLastOSError;
end;
hDevInf := SetupDiGetClassDevs(@Guid,nil,0,DIGCF_PRESENT);
if hDevInf = INVALID_HANDLE_VALUE then
begin
RaiseLastOSError;
end;
try
Index := 0;
while True do
begin
FillChar(DevInfoData,SizeOf(DevInfoData),0);
DevInfoData.cbSize := SizeOf(DevInfoData);
if SetupDiEnumDeviceInfo(hDevInf,Index,DevInfoData) = False then
begin
Break;
end;
SetupDiGetDeviceRegistryProperty(hDevInf,DevInfoData,SPDRP_FRIENDLYNAME,
nil,nil,0,Size);
SetLength(S1,(Size + SizeOf(Char)) div SizeOf(Char));
if SetupDiGetDeviceRegistryProperty(hDevInf,DevInfoData,SPDRP_FRIENDLYNAME,
nil,PChar(S1),Size,Size) = True then
begin
SetLength(S1,StrLen(PChar(S1)));
hRegKey := SetupDiOpenDevRegKey(hDevInf,DevInfoData,DICS_FLAG_GLOBAL,0,DIREG_DEV,KEY_READ);
if hRegKey <> INVALID_HANDLE_VALUE then
begin
try
if RegQueryInfoKey(hRegKey,nil,nil,nil,nil,nil,nil,nil,nil,
@Size,nil,nil) = ERROR_SUCCESS then
begin
SetLength(S2,(Size + SizeOf(Char)) div SizeOf(Char));
if (RegQueryValueEx(hRegKey,'PortName',nil,@RegType,Pointer(PChar(S2)),
@Size) = ERROR_SUCCESS) and
(RegType = REG_SZ) then
begin
SetLength(S2,StrLen(PChar(S2)));
if CompareText(Copy(S2,1,3),'COM') = 0 then
begin
if TryStrToInt(Copy(S2,4,Length(S2)),PortNo) = True then
begin
S.AddObject(S1,Pointer(PortNo));
if Result < PortNo then
begin
Result := PortNo;
end;
end;
end;
end;
end;
finally
RegCloseKey(hRegKey);
end;
end;
end;
Index := Index + 1;
end;
finally
SetupDiDestroyDeviceInfoList(hDevInf);
end;
end;