Recent

Author Topic: Odd registry behaviour in WinXP  (Read 7589 times)

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Odd registry behaviour in WinXP
« on: October 24, 2014, 01:43:27 pm »
While fixing a bug in LazSerial I got to synaser. Here is the problematic code:
Code: [Select]
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:
 
Code: [Select]
v.Add(reg.ReadString(l[n]));


I changed the code the following way:
 
Code: [Select]
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:
Code: [Select]
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.
« Last Edit: October 24, 2014, 02:15:41 pm by paskal »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

derek.john.evans

  • Guest
Re: Odd registry behaviour in WinXP
« Reply #1 on: October 24, 2014, 02:42:15 pm »
Have you checked the binary encoding of the strings? (via regedit).  Maybe its a widechar issue?

What version of Lazarus are you running?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Odd registry behaviour in WinXP
« Reply #2 on: October 24, 2014, 02:54:37 pm »
His signature mentions the Laz version...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Odd registry behaviour in WinXP
« Reply #3 on: October 24, 2014, 03:02:38 pm »
Have you checked the binary encoding of the strings? (via regedit).  Maybe its a widechar issue?
I just did. So here is what I found out. It was 43 00 4F 00 4d 00 33 00 32 00. When I edited it (actually by only clicking OK in the registry editor), it got changed to  43 00 4F 00 4d 00 33 00 32 00 00 00. Maybe there is a trick to use this info.
« Last Edit: October 24, 2014, 03:09:15 pm by paskal »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

derek.john.evans

  • Guest
Re: Odd registry behaviour in WinXP
« Reply #4 on: October 24, 2014, 03:39:41 pm »
Thats interesting. Looks like some registry writers arn't writing correctly.

There is a StringSizeIncludesNull property, which is hardcoded to True in winreg.inc

Which causes the string to be truncated in registry.pas
Code: Pascal  [Select][+][-]
  1.      If StringSizeIncludesNull then
  2.        SetLength(Result, Info.DataSize-1)
  3.      else
  4.        SetLength(Result, Info.DataSize);    
  5.  

It would seem, the code "should" check to see if the end char is actually a #0, before truncating.

EDIT: I guess you will have to use your own version of ReadString() to cater for the possible non nulled strings

« Last Edit: October 02, 2015, 03:40:58 am by Geepster »

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Odd registry behaviour in WinXP
« Reply #5 on: October 24, 2014, 04:28:55 pm »
I see at least tree possible reasons for that:
1. COM port adding to bluetooth in WinXP is buggy.
2. There is a problem in the driver of the  bluetooth dongle.
3. There is a problem in the device I am connecting to. It is a custom one, so I might discuss it with the designer. But I cannot think why it would have a connection to COM port assignment, so this does not seem much possible to me.
Thanks for the ideas, I am done with Pascal for this week.
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Odd registry behaviour in WinXP
« Reply #6 on: October 27, 2014, 07:45:57 am »
So I can definitely say, that the reason for that behaviour is item 1: COM port adding to bluetooth in WinXP is buggy.
I have added my cell phone and the same problem occurred, which rejected item 3 as a reason for the problem. Then replaced the bluetooth dongle by a one by another manufacturer (or another model, at least, definitely it is smaller than any of the ICs in the other one). The problem reoccured, rejecting item 2 as a possible cause.


EDIT: I do wonder if there is not a buig in the FPC/Lazarus registry functions? In a few days I could try this in another programming language, to check how things are working there.
« Last Edit: October 27, 2014, 10:22:27 am by paskal »
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

 

TinyPortal © 2005-2018