While fixing a bug in
LazSerial I got to
synaser. Here is the problematic code:
function GetSerialPortNames: string;
var
reg: TRegistry;
l, v: TStringList;
n: integer;
debvalnames: string;
begin
l := TStringList.Create;
v := TStringList.Create;
reg := TRegistry.Create;
try
{$IFNDEF VER100}
{$IFNDEF VER120}
reg.Access := KEY_READ;
{$ENDIF}
{$ENDIF}
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.OpenKey('\HARDWARE\DEVICEMAP\SERIALCOMM', false);
debvalnames:=reg.GetValueNames(l);
reg.GetValueNames(l);
for n := 0 to l.Count - 1 do
begin
v.Add(reg.ReadString(l[n]));
end;
Result := v.CommaText;
finally
reg.Free;
l.Free;
v.Free;
end;
end;
The purpose of the function is to return a string, containing the list of the COM devices, attached to the computer. The bug occurs when the number of the COM port is higher than 9 or exactly- last digits of the COM number are trimmed. For example, my bluetooth device is attached at COM32, but in the result of the function I get COM3.
I have narrowed the search to the following code:
v.Add(reg.ReadString(l[n]));
I changed the code the following way:
ComName:= reg.ReadString(l[n]);
ShowMessage(length(ComName ) + ' ' + ComName);
v.Add(ComName);
If I put a breakpoint on the line with
ShowMessage(...); when the application stops on it, when I put the mouse pointer over
ComName it shows „
COM32“, or exactly what it should.
But, ShowMessage(length(ComName ) + ' ' + ComName); shows
„COM3 4“. It occurs that the last char is lost. If I have a longer port number, probably last two chars will be lost.
So I did:
ComName:= reg.ReadString(l[n]);
SetLength (ComName, 5);
ShowMessage(length(ComName ) + ' ' + ComName);
v.Add(ComName);
Now the last character is not lost, but I have a - (minus) sign in the end of the shorter COM port names, which is not a surprise.
To recapitulate: The string is correctly retrieved from the registry, but its length value is wrong. Is there a bug in windows itself, in FPC (or Lazarus) registry module or in the posted code itself? And what should I do to make it work. Definitely, I can trim the minus signs, but that is the last chance solution.
Edit: I added anoher entry in the registry: COM252.
So, actually when using
SetLength (ComName, 5); I get the following strings „
COM1“; „
COM32-“; „
COM33-“ and
„COM252“. There are no minus signs after
COM1 !!!
EDIT (again): Trough the registry editor I open the COM32 and COM33 values and then I pressed enter, just like editing them. Now the minus signs disappeared! I removed the SetLength statement and still everythings works fine.
Seems like the bluetooth device gets added improperly?!?
Edit (once again): Now I remember, that some quotation marks " were shown in the registry entry names, which have now disappeared. I will move the bluetooth dongle to another usb port.