Author: Jonas Bilinkevicius How to activate the previous instance of an application Answer: Place the following at the start of your project file: 1 2 begin 3 if HPrevInst < > 0 then 4 begin 5 ActivatePreviousInstance; 6 Exit; 7 end; 8 { ... } 9 10 and include the following unit: 11 12 unit PrevInst; 13 14 interface 15 16 uses 17 WinProcs, WinTypes, SysUtils; 18 19 type 20 PHWnd = ^HWnd; 21 22 {function EnumFunc(Wnd: HWnd; TargetWindow: PHWnd): Bool; export; 23 procedure ActivatePreviousInstance;} 24 25 implementation 26 27 function EnumFunc(Wnd: HWnd; TargetWindow: PHWnd): Bool; 28 var 29 ClassName: array[0..30] of char; 30 begin 31 Result := True; 32 if GetWindowWord(Wnd, GWW_HINSTANCE) = HPrevInst then 33 begin 34 GetClassName(Wnd, ClassName, 30); 35 if StrIComp(ClassName, 'TApplication') = 0 then 36 begin 37 TargetWindow^ := Wnd; 38 Result := False; 39 end; 40 end; 41 end; 42 43 procedure ActivatePreviousInstance; 44 var 45 PrevInstWnd: HWnd; 46 begin 47 PrevInstWnd := 0; 48 EnumWindows(@EnumFunc, Longint(@PrevInstWnd)); 49 if PrevInstWnd <> 0 then 50 if IsIconic(PrevInstWnd) then 51 ShowWindow(PrevInstWnd, SW_RESTORE) 52 else 53 BringWindowToTop(PrevInstWnd); 54 end; 55 56 end.