var
TotalComNum : Integer = 0;
HaveOpenCom : string = '0';
USES Registry;
procedure GetComListFromReg();
var
reg:TRegistry; // 注: 要引用Registry单元
Namelst:TStrings;
i,j:integer;
str:string;
b:Boolean;
begin
Namelst:=TStringList.Create;
reg:=TRegistry.Create;
reg.RootKey :=HKEY_LOCAL_MACHINE;
b := reg.OpenKey('HARDWARE\DEVICEMAP\SERIALCOMM',true);
//ShowMessage('open ='+ BoolToStr(b));
reg.GetValueNames(Namelst);
//Form1.memo1.Clear;
//for i := 0 to Namelst.Count-1 do
// Form1.memo1.Lines.Add(reg.ReadString(Namelst[i]));
//ShowMessage('namelst = ' + IntToStr(Namelst.Count) + 'totalcomnum = ' + IntToStr(TotalComNum));
if Namelst.Count <> TotalComNum then
begin
TotalComNum := Namelst.Count;
if HaveOpenCom = '0' then
begin
//ShowMessage('123');
Form8.ComboBox1.Items.Clear;
for i := 0 to Namelst.Count-1 do
begin
//ShowMessage(reg.ReadString(Namelst[i]));
Form8.ComboBox1.Items.Add(reg.ReadString(Namelst[i]));
end;
// 排序
for i := 0 to Namelst.Count-2 do
for j := i+1 to Namelst.Count-1 do
if Form8.ComboBox1.Items[i] > Form8.ComboBox1.Items[j] then
begin
str := Form8.ComboBox1.Items[i];
Form8.ComboBox1.Items[i] := Form8.ComboBox1.Items[j];
Form8.ComboBox1.Items[j] := str;
end;
Form8.ComboBox1.itemindex := 0;
end
else
begin
//ShowMessage('456');
//ShowMessage('*******'+ HaveOpenCom);
for i := 0 to Namelst.Count-1 do
begin
//ShowMessage('-------' + reg.ReadString(Namelst[i]));
if HaveOpenCom = reg.ReadString(Namelst[i]) then
begin
reg.CloseKey;
reg.Free;
Namelst.Free;
Exit;
//break;
end;
end;
//ShowMessage('i=' + IntToStr(i) + 'count=' + IntToStr(Namelst.Count));
if i = Namelst.Count then
begin
//ShowMessage('close');
// Form8.Button1.Click;
end;
//ShowMessage('***i=' + IntToStr(i) + 'count=' + IntToStr(Namelst.Count));
end;
end;
reg.CloseKey;
reg.Free;
Namelst.Free;
end;
procedure TForm8.FormCreate(Sender: TObject);
begin
GetComListFromReg();
end;