我试图在Delphi中调用
EnumSystemLocales
.
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;
问题是回调只被调用一次.
注意:EnumSystemLocales返回true,表示成功.
EnumSystemLocales的评论说我的回调必须返回true才能继续枚举(或者更准确地说,不能返回false以继续枚举):
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.
在documentation of the callback function:
BOOL CALLBACK EnumLocalesProc( __in LPTSTR lpLocaleString );
评论者遇到了“非虚假”定义的问题:
This function must return 1,not (DWORD) -1 to continue processing
这让我觉得delphi的定义
True: BOOL;
与Window不同. (这就是为什么我尝试了BOOL(1)的返回值 – 它仍然失败了).
接下来我想知道它是不是应该是stdcall.
无论哪种方式,有人可以建议如何在Delpi中调用EnumSystemLocales吗?
编辑:还试过:
>结果:= BOOL(-1);
>结果:= BOOL($FFFFFFFF);
>结果:= BOOL(1);
>结果:=真;
解决方法
尝试像这样声明LocalesCallback函数
function LocalesCallback(Name: PChar): Integer; stdcall;
检查这个样本
{$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.