-
Notifications
You must be signed in to change notification settings - Fork 792
/
SerialPortsCtrl.pas
63 lines (52 loc) · 1.66 KB
/
SerialPortsCtrl.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
// -----------------------------------------------------------------------------
// 本代码来源于互联网,感谢原作者.
// http://bbs.csdn.net/topics/310211601
// -----------------------------------------------------------------------------
// 名称: SerialPortsCtrl
// 备注: 串行接口枚举
// -----------------------------------------------------------------------------
unit SerialPortsCtrl;
interface
uses
Windows, Classes, ExtCtrls;
procedure EnumComPorts(Ports: TStrings);
implementation
procedure EnumComPorts(Ports: TStrings);
var
KeyHandle: HKEY;
ErrCode, Index: Integer;
ValueName, Data: string;
ValueLen, DataLen, ValueType: DWORD;
TmpPorts: TStringList;
begin
ErrCode := RegOpenKeyEx(HKEY_LOCAL_MACHINE, 'HARDWARE\DEVICEMAP\SERIALCOMM',
0, KEY_READ, KeyHandle);
if ErrCode <> ERROR_SUCCESS then
Exit; // raise EComPort.Create(CError_RegError, ErrCode);
TmpPorts := TStringList.Create;
try
Index := 0;
repeat
ValueLen := 256;
DataLen := 256;
SetLength(ValueName, ValueLen);
SetLength(Data, DataLen);
ErrCode := RegEnumValue(KeyHandle, Index, PChar(ValueName),
Cardinal(ValueLen), nil, @ValueType, PByte(PChar(Data)), @DataLen);
if ErrCode = ERROR_SUCCESS then
begin
SetLength(Data, DataLen);
TmpPorts.Add(Data);
Inc(Index);
end
else if ErrCode <> ERROR_NO_MORE_ITEMS then
Exit; // raise EComPort.Create(CError_RegError, ErrCode);
until (ErrCode <> ERROR_SUCCESS);
TmpPorts.Sort;
Ports.Assign(TmpPorts);
finally
RegCloseKey(KeyHandle);
TmpPorts.Free;
end;
end;
end.