Corrupt non-Latin environment-variables in Windows lazarus / 12-Debugger ---------------------------------------------------------------------- Igor · 2020-07-14 06:32 Lazarus 2.0.10 + FPC 3.2.0 (x86-64) returns corrupt non-Latin environment-variables when I use ExpandEnvironmentStringsW() under debugger on Windows 10. When I run directly the EXE file everything is OK. This problem didn't appear in Lazarus 2.0.6 + FPC 3.0.4 (x86-64). My app reads a path to App Data and Documents folders. And if my Windows profile name contain Russian symbols ("Игорь") returned path is wrong. Should I report a bug for Lazarus or FPC? A test application: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE CONSOLE}program EnvBug; function ExpandEnvironmentStringsW(lpSrc: PWideChar; lpDst: PWideChar;  nSize: Cardinal): Cardinal; stdcall; external 'kernel32.dll' name 'ExpandEnvironmentStringsW'; function GetEnv(const Name: WideString): WideString;var  len  : Cardinal;begin  len:=ExpandEnvironmentStringsW( PWideChar(Name), nil, 0);  if len>0 then begin    SetLength( Result, len-1 );    ExpandEnvironmentStringsW( PWideChar(Name), PWideChar(Result), len );  end else Result:='';end; procedure Test(const Name : WideString);begin  WriteLn( 'EvnVar '+Name,' = ', GetEnv(Name) );end; begin  Test('%LOCALAPPDATA%'); WriteLn;  Write('Press ENTER to Exit'); ReadLn;end. ---------------------------------------------------------------------- Martin_fr · 2020-07-14 13:00 Yes please report a bug: Lazarus EDIT: lets look at my next post first Also, what happens if you run outside the debugger? If it is only in the debugger, please go to our sourceforge page https://sourceforge.net/projects/lazarus/files/Lazarus%20Windows%2064% 20bits/Alternative%20GDB/ And try different versions of gdb. Or install package LazDebuggerFp, and see if fpdebug does a better job. ---------------------------------------------------------------------- Martin_fr · 2020-07-14 14:03 Are you sure, it is the environment, and not the console to which you write. Below code prints the ord values for each char, and they seem to be correct. As I do not have none-latin chars in my path, I went to menu: Run > Run parameters and set an env variable test1=Игорь test2=äöü Note the app below also prints the utf8 as ordinals. Windows of course does not return utf8. It either returns utf16, or the local codepage  (My local codepage has no support for Игорь , hence the äöü) All instances of Игорь print 1048, 1075, 1086, 1088, 1100, That is the same under an IDE build with fpc 3.0.4 and 3.2.0 That seems correct according to 1048 = 0x418 https://www.compart.com/en/unicode/U+0418 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1;{$codepage utf8}{$mode objfpc}{$H+} uses  windows  ; {$R *.res} function ExpandEnvironmentStringsW(lpSrc: PWideChar; lpDst: PWideChar;  nSize: Cardinal): Cardinal; stdcall; external 'kernel32.dll' name 'ExpandEnvironmentStringsW'; function GetEnv(const Name: WideString): WideString;var  len  : Cardinal;begin  len:=ExpandEnvironmentStringsW( PWideChar(Name), nil, 0);  if len>0 then begin    SetLength( Result, len-1 );    ExpandEnvironmentStringsW( PWideChar(Name), PWideChar(Result), len );  end else Result:='';end; procedure Test(const Name : WideString);var  w: WideChar;begin  WriteLn( 'EvnVar '+Name,' = ', GetEnv(Name) );  for w in GetEnv(Name) do    write(ord(w), ', ');  writeln;end;   const  t1: widestring = widestring('Игорь');  t2: UTF8String = 'Игорь';   d1: widestring = widestring('äöü');  d2: UTF8String = 'äöü';var  x: array [0..101] of word;  y: array [0..101] of byte;  i: Integer;  l: DWORD;  w: WideChar;  c: Char;begin  for w in t1 do    write(ord(w), ', ');  writeln;  for c in t2 do    write(ord(c), ', ');  writeln;  writeln;   l := GetEnvironmentVariableW(pwidechar('test1'), @x[0], 100);  writeln(l);  for i := 0 to l-1 do    write(x[i],', ');  writeln;   l := GetEnvironmentVariableA(pchar('test1'), @y[0], 100);  writeln(l);  for i := 0 to l-1 do    write(y[i],', ');  writeln;  writeln;   writeln;  writeln;    for w in d1 do    write(ord(w), ', ');  writeln;  for c in d2 do    write(ord(c), ', ');  writeln;  writeln;   l := GetEnvironmentVariableW(pwidechar('test2'), @x[0], 100);  writeln(l);  for i := 0 to l-1 do    write(x[i],', ');  writeln;   l := GetEnvironmentVariableA(pchar('test2'), @y[0], 100);  writeln(l);  for i := 0 to l-1 do    write(y[i],', ');  writeln;   Test('%test1%'); WriteLn;  Test('%test2%'); WriteLn;    readln;end. ---------------------------------------------------------------------- blind · 2020-07-14 14:31 Quote from: Martin_fr on July 14, 2020, 04:03:50 pmAre you sure, it is the environment, and not the console to which you write. Below code prints the ord values for each char, and they seem to be correct. I created a GUI example on Lazarus that does not use the console. The program reads all environment variables using the GetEnvironmentStringsW function. When starting the program without a debugger, the values of the environment variables are correct. When launched under the debugger on Lazarus 2.0.10, the national symbols are corrupted. https://drive.google.com/file/d/1ah4lZEY-Nh5qKLD- WN7xL4DmQGlatpyq/view?usp=sharing ---------------------------------------------------------------------- Martin_fr · 2020-07-14 14:53 Ok yes, please report. I can reproduce. In my first test, I was accidentally using the new fpdebug, and that works. It does happen under the default gdb based debugger. ---------------------------------------------------------------------- Martin_fr · 2020-07-14 16:16 It appears to be a problem with the updated gdb. You can download the old one here https://sourceforge.net/projects/laz arus/files/Lazarus%20Windows%2064%20bits/Alternative%20GDB/GDB%207.3.5 0/ ---------------------------------------------------------------------- Igor · 2020-07-14 16:48 Martin, Thanks for your reply and alternate solution. I've submited a report for Lazarus: https://bugs.freepascal.org/view.php?id=37349