unit SerialPorts;
interface
type
SerialPort_Rec = record
PortNr : Word;
Linked : Boolean;
PortName,
Description,
FriendlyName,
Decive,
KeyDevice,
KeyEnum : String;
end;
SerialPort_Ar = Array of SerialPort_Rec;
function GetComPorts:SerialPort_Ar;
implementation
uses Windows, SysUtils, Classes, Registry;
const Key_Devices = '\SYSTEM\CurrentControlSet\Control\DeviceClasses\{86e0d1e0-8089-11d0-9ce4-08003e301f73}\';
Key_Enum = '\SYSTEM\CurrentControlSet\Enum\';
procedure SortComPorts(VAR Daten:SerialPort_Ar);
var Sort_Max,
Sort_From,
Sort_To,
Sort_Size : LongInt;
TempData : SerialPort_Rec;
begin
if Daten = NIL then
Exit;
Sort_Max := High(Daten);
Sort_Size := Sort_Max shr 1; { div 2 }
while Sort_Size > 0 do
begin
for Sort_From := 0 to Sort_Max - Sort_Size do
begin
Sort_To := Sort_From;
while (Sort_To >= 0) AND (Daten[Sort_To].PortNr > Daten[Sort_To + Sort_Size].PortNr) do
begin // Tauschen
TempData := Daten[Sort_To];
Daten[Sort_To] := Daten[Sort_To + Sort_Size];
Daten[Sort_To + Sort_Size] := TempData;
Dec(Sort_To,Sort_Size);
end;
end;
Sort_Size := Sort_Size shr 1; { div 2 }
end;
end;
function GetComPorts:SerialPort_Ar;
var Reg : TRegistry;
Keys : TStrings;
Count,
Index,
Linked : Integer;
Key1,
Key2,
Device,
Description,
FriendlyName,
PortName : String;
begin
Result := NIL;
Reg := TRegistry.Create;
Keys := TStringList.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly(Key_Devices) then
begin
Reg.GetKeyNames(Keys);
if Keys.Count > 0 then
begin
Index := 0;
SetLength(Result,Keys.Count);
for Count := 0 to Keys.Count-1 do
begin
Key1 := Key_Devices+Keys[Count] + '\';
if Reg.OpenKeyReadOnly(Key1) then
begin
Device := Reg.ReadString('DeviceInstance');
Key2 := Key_Enum + Device + '\';
if Reg.OpenKeyReadOnly(Key1 + '#\Control\') then
begin
Linked := Reg.ReadInteger('Linked');
if Reg.OpenKeyReadOnly(Key2) then
begin
if (Reg.ReadString('Class') = 'Ports') AND Reg.KeyExists('Device Parameters') then
begin
FriendlyName := Reg.ReadString('FriendlyName');
Description := Reg.ReadString('DeviceDesc');
if Reg.OpenKeyReadOnly(Key2+'\Device Parameters\') AND Reg.ValueExists('PortName') then
begin
PortName := Reg.ReadString('PortName');
if Pos('
COM',PortName) = 1 then
begin
Delete(Description,1,Pos(';',Description));
Result[Index].PortNr := StrToIntDef(Copy(PortName,4),0);
Result[Index].Linked := Linked > 0;
Result[Index].PortName := PortName;
Result[Index].Description := Description;
Result[Index].FriendlyName := FriendlyName;
Result[Index].Decive := Device;
Result[Index].KeyDevice := Key1;
Result[Index].KeyEnum := Key2;
Inc(Index);
end;
end;
end;
end;
end;
end;
end;
SetLength(Result,Index);
end;
end;
finally
Keys.Free;
Reg.CloseKey;
Reg.Free;
SortComPorts(Result);
end;// finally
end;
end.