This is the code we use to manage our two executables. To get the class name for the application, you can use the Code-insight tool that comes with delphi. If the application you want to manage is written in delphi, the class name will be TApplication. The window name (ASTR_MainFormWindowName), is typically the caption on your main form at the time delphi creates it. You do not need both parameters, but if you have more than one delphi application running, TApplication may not find the one you want without the second parameter.
I hope this helps.
function OpenPreviousInstance (ASTR_ApplicationClassName, ASTR_MainFormWindowName:string):boolean;
//Either parameter can be nil. You only need one in most cases.
var
AppHandle : THandle;
begin
result := false;
AppHandle := FindWindow (PChar (ASTR_ApplicationClassName) , Pchar(ASTR_MainFormWindowName));
if AppHandle <> 0 then
begin
if IsIconic(AppHandle) then
ShowWindow(AppHandle,SW_Restore)
else
BringWindowToTop(AppHandle);
SetForegroundWindow(AppHandle);
CloseHandle(AppHandle);
result := true;
exit;
end;
CloseHandle(AppHandle);
end;
If I type "openpreviousinstance('tapplication','Form1')" in a procedure, I obtain every time for the APPHANDLE the value 0.The rest of the function is not thus done.
If the other application is not running yet, you need to execute the other program first. We include the the fmxutils.pas (in the delphi demos) in our uses clause and call like this:
if OpenPreviousInstance('Tapplication','CARS') = false then
ExecuteFile('CarsFB.exe','','C:\Apps\CarsFb\Bin\',sw_ShowNormal);
If the application you are trying to contact is already running, then perhaps you do not have the correct name for the application. Try running it with the second parameter as nil : OpenPreviousInstance('TApplication',nil)
This should open the first delphi application the system finds running. If this works, I would try the WinSight and see if the application you are trying to open has a different name than you expect for some reason.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.