How to call EnumSystemLocales in Delphi?

2019-04-05 18:32发布

i am trying to call EnumSystemLocales in Delphi. For example:

{ Called for each supported locale. }
function LocalesCallback(Name: PChar): BOOL; stdcall;
begin
   OutputDebugString(Name);
   Result := Bool(1); //True
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   EnumSystemLocales(@LocalesCallback, LCID_SUPPORTED);
end;

The problem is that the callback is only being called once.

Note: EnumSystemLocales is returning true, indicating success.

The remarks of EnumSystemLocales says that my callback must return true to continue enumerating (or more correctly, must not return false to continue enumerating):

The function enumerates locales by passing locale identifiers, one at a time, to the specified application-defined callback function. This continues until all of the installed or supported locale identifiers have been passed to the callback function or the callback function returns FALSE.

On the documentation of the callback function:

BOOL CALLBACK EnumLocalesProc(
  __in  LPTSTR lpLocaleString
);

a commenter has come across a problem with the definition of "not false":

This function must return 1, not (DWORD) -1 to continue processing

This makes me think that delphi's definition of

True: BOOL;

is different than Window's. (That's why i tried a return value of BOOL(1) - which still fails).

Next i wonder if it's not even supposed to be stdcall.

Either way, can someone suggest how, in Delpi, to call EnumSystemLocales?


Edit: Also tried:

  • Result := BOOL(-1);
  • Result := BOOL($FFFFFFFF);
  • Result := BOOL(1);
  • Result := True;

3条回答
一纸荒年 Trace。
2楼-- · 2019-04-05 18:40

If you insist on using BOOL type for callback function result, use this:

function LocalesCallback(Name: PChar): BOOL; stdcall;
begin
   OutputDebugString(Name);
   LongWord(Result) := 1;
end;

because Bool(1) = $FFFFFFFF.

查看更多
冷血范
3楼-- · 2019-04-05 18:49

This problem happens due WinAPI bug, observed in Windows version 5.1 WinNls EnumXXX function family (and, according to the comments, probably several others) is only recognizing exactly (BOOL)1 as (BOOL)TRUE and will stop enumeration if callback returns any other returnValue != (BOOL)FALSE.

Here is a most semantic workaround i figured out:

  LongWord(Result) := LongWord(True);     // WINBUG: WinNls functions will continue
                                          // enumeration only if exactly 1 was returned
                                          // from the callback
查看更多
The star\"
4楼-- · 2019-04-05 18:58

try declarating the LocalesCallback function like this

function LocalesCallback(Name: PChar): Integer; stdcall;

check this sample

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows,
  SysUtils;

function LocalesCallback(Name: PChar): Integer; stdcall;
begin
   Writeln(Name);
   Result := 1;
end;

begin
  try
    EnumSystemLocales(@LocalesCallback, LCID_SUPPORTED);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
查看更多
登录 后发表回答