Whenever I had to use API callbacks, like with EnumChildWindows
, I typically wrote them like this:
procedure TMyClass.FindChildWindowAndDoSomething(
AParent: HWND;
ASomeCriterion: Integer;
const AData: Pointer);
type
TEnumWndParam = record
Criterion: Integer;
Data: Pointer;
end;
PEnumWndParam = ^TEnumWndParam;
function EnumProc(AHandle: HWND; AParam: PEnumWndParam): Boolean; stdcall;
begin
Result := not {AHandle satisfies AParam^.Criterion};
if not Result then
{Do something to AHandle, possibly using AParam^.Data}
else
Result := True;
end;
var
lParam: TEnumWndParam;
begin
lParam.Criterion := ASomeCriterion;
lParam.Data := AData;
EnumChildWindows(AParent, @EnumProc, Winapi.Windows.LPARAM(@lParam));
end;
This still works fine with 32bit Delphi XE3, but with the 64bit compiler things are a little different: It still compiles without problems, but when EnumProc
gets called, the arguments it gets fed contain garbage. The only way I found to make this work was to move the local declarations (the param record and the enum function) into the global space.
I really dislike this as it unnecessarily broadens the scope of these declarations and clutters the namespace. Is there some other way to get this to work with the 64bit compiler? Has this maybe been fixed in XE4 already?