From a Windows API call (GetUserPreferredUILanguages()
), I get a list of strings as one null-delimited PWideChar
. I need to convert this to a list of Delphi strings. I began writing code to manually loop through the list, looking for #0
chars.
Is there a smarter way to do this?
Example of the PWideChar
returned by GetUserPreferredUILanguages
:
('e','n','-','U','S',#0,'f','r','-','F','R',#0,#0,...)
(based on what I read in the documentation, because when I call the function on my computer, it only returns one language, i.e. 'en-US'#0#0)
Here is my code so far:
procedure GetWinLanguages(aList: TStringList);
var lCount, lSize: ULong;
lChars: array of WideChar;
lIndex, lLastIndex: integer;
begin
lSize := 1000;
SetLength(lChars, lSize);
GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, @lCount, @lChars[0], @lSize);
// untested quick solution to convert from lChars to aList
lIndex := 0;
lLastIndex := -1;
while (lIndex<=lSize) do
begin
while (lIndex<lSize) and (lChars[lIndex]<>#0) do
inc(lIndex);
if (lIndex-lLastIndex)>1 then
begin
// here: copy range lLastIndex to lIndex, convert to string and add to aList
lLastIndex := lIndex;
inc(lIndex);
end else
Break;
end;
end;
PS. I am on Windows 10 using Delphi Berlin for a FMX project.