通过注册表获取当前所有串口号

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons,Registry;


type
TIntArray=Array of Integer;

 

function TForm1.GetComPorts:TIntArray;
var
reg:TRegistry; // 注: 要引用Registry单元
Namelst:TStrings;
i:integer;
str:AnsiString;
ComPorts:TIntArray;
begin
Namelst:=TStringList.Create;
reg:=TRegistry.Create;
reg.RootKey :=HKEY_LOCAL_MACHINE;
reg.OpenKey('HARDWARE/DEVICEMAP/SERIALCOMM',true);
reg.GetValueNames(Namelst);
SetLength(ComPorts,Namelst.Count);

for i := 0 to Namelst.Count -1 do
begin
str:=reg.ReadString(Namelst[i]);
ComPorts[i]:=StrToInt(Copy(str,4,Length(str)));
end;
reg.CloseKey;
reg.Free;
Namelst.Free;
Result:=ComPorts;


end;

你可能感兴趣的:(注册表)