uses Winapi.Windows;
procedure Tfrmtest.EnumComPorts(Ports: TStrings);
const
sProlific = 'prolific';
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
raise Exception.Create('打开串口列表的注册表项出错');
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);
if Pos(LowerCase(sProlific), LowerCase(ValueName)) > 0 then
Data := sProlific + ' ' + Trim(Data);
TmpPorts.Add(Data);
inc(Index);
end
else if ErrCode <> ERROR_NO_MORE_ITEMS then
raise Exception.Create('打开串口列表的注册表项出错');
until (ErrCode <> ERROR_SUCCESS);
TmpPorts.Sort;
Ports.Assign(TmpPorts);
finally
RegCloseKey(KeyHandle);
TmpPorts.Free;
end;
end;