I have two windows in separate applications. The first application has a button that starts the second application with its window handle and process id:
procedure TForm1.Button1Click(Sender: TObject);
begin
WinExec(PChar('Second.exe ' + IntToStr(Handle) + ' ' + IntToStr(GetCurrentProcessId)), SW_SHOWDEFAULT);
end;
The second application also has a button which should set the foreground window to the first application:
function AllowSetForegroundWindow(AHandle: HWND): Boolean; external 'user32.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin
if not AllowSetForegroundWindow(StrToInt(ParamStr(2))) then begin
ShowMessage('ERROR');
Exit;
end;
SendMessage(StrToInt(ParamStr(1)), WM_APP + 1, 0, 0);
end;
The first application has a message handler that handles WM_APP + 1 like this:
procedure TForm1.WWAppPlusOne(var Msg: TMsg);
begin
Application.BringToFront;
end;
When I start the first application and press on the button, the second application starts. When I press the button on the second application it shows ERROR.
What am I doing wrong here?
Your declaration of
AllowSetForegroundWindowis incorrect. You have omitted the calling convention. The data types you have used are wrong too, although that’s probably benign for you at the moment.It should look like this: