Enumerate COM ports in Windows with Lazarus lazarus / 34-Windows ---------------------------------------------------------------------- padiazg · 2012-04-13 14:26 Full history here: http://patotech.blogspot.com/2012/04/enumerate-com- ports-in-windows-with.html As I have only USB ports on my development machine, is quite common at connecting a serial device that I have to go to the device manager to see in which COM port it was installed.  I found this page: http://www.lazarus.freepascal.org/index.php?topic=14313.0 which publishes three interesting features, but none of them fullfills my needs. So I came up with this: function GetSerialPortNamesExt: string; var   reg  : TRegistry;   l,v  : TStringList;   n    : integer;   pn,fn: string;     function findFriendlyName(key: string; port: string): string;   var     r : TRegistry;     k : TStringList;     i : Integer;     ck: string;     rs: string;   begin     r := TRegistry.Create;     k := TStringList.Create;       r.RootKey := HKEY_LOCAL_MACHINE;     r.OpenKeyReadOnly(key);     r.GetKeyNames(k);     r.CloseKey;       try       for i := 0 to k.Count - 1 do       begin         ck := key + k + '\'; // current key         // looking for "PortName" stringvalue in "Device Parameters" subkey         if r.OpenKeyReadOnly(ck + 'Device Parameters') then         begin           if r.ReadString('PortName') = port then           begin             //Memo1.Lines.Add('--> ' + ck);             r.CloseKey;             r.OpenKeyReadOnly(ck);             rs := r.ReadString('FriendlyName');             Break;           end // if r.ReadString('PortName') = port ...         end  // if r.OpenKeyReadOnly(ck + 'Device Parameters') ...         // keep looking on subkeys for "PortName"         else // if not r.OpenKeyReadOnly(ck + 'Device Parameters') ...         begin           if r.OpenKeyReadOnly(ck) and r.HasSubKeys then           begin             rs := findFriendlyName(ck, port);             if rs <> '' then Break;           end; // if not (r.OpenKeyReadOnly(ck) and r.HasSubKeys) ...         end; // if not r.OpenKeyReadOnly(ck + 'Device Parameters') ...       end; // for i := 0 to k.Count - 1 ...       result := rs;     finally       r.Free;       k.Free;     end; // try ...   end; // function findFriendlyName ...   begin   v      := TStringList.Create;   l      := TStringList.Create;   reg    := TRegistry.Create;   Result := '';     try     reg.RootKey := HKEY_LOCAL_MACHINE;     if reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM') then     begin       reg.GetValueNames(l);         for n := 0 to l.Count - 1 do       begin         pn := reg.ReadString(l[n]);         fn := findFriendlyName('\System\CurrentControlSet\Enum\', pn);         v.Add(pn + ' = '+ fn);       end; // for n := 0 to l.Count - 1 ...         Result := v.CommaText;     end; // if reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM') ...   finally     reg.Free;     v.Free;   end; // try ... end; ---------------------------------------------------------------------- seaton · 2012-08-06 05:48 many thanks just what I was after for my current project ---------------------------------------------------------------------- exdatis · 2012-08-06 06:10 Great! Thanks! ---------------------------------------------------------------------- picstart · 2012-08-06 07:35 I had to change  ck := key + k + '\'; // current key to  Code: [Select] ck := key + k[i] + '\'; // current key to get it to work win7 pro 32bit laz 1ORC1 FPC 2.6.