// add these at the top of your project as global variables:
var
AppInstanceLaunch: boolean;
AppInstanceFound: boolean;
AppInstanceName: string;
AppInstanceHandle: hWnd;
// add this function at the top of your program -- no need to register it
// I found it on the internet and it did not have an author disclosed
// I also modified it so it would suite my needs
// it searches for a string within the caption of a first instance app:
function TMainFrm.FindWindowExtd(partialTitle: string): HWND; // get with wildcard
var // by Dorin Duminica, September 10, 2009
hWndTemp: hWnd;
iLenText: Integer;
cTitletemp: array [0..254] of Char;
sTitleTemp: string;
begin
hWndTemp:= FindWindow(nil, nil);
if AppInstanceLaunch then
begin
AppInstanceFound:= false;
AppInstanceName:= '';
AppInstanceHandle:= 0;
end;
while hWndTemp <> 0 do
begin
iLenText:= GetWindowText(hWndTemp, cTitletemp, 255);
//sTitleTemp:= cTitletemp; // I omitted this step and modified the next line
sTitleTemp:= UpperCase(copy(cTitleTemp, 1, iLenText)); // it was copy(sTitleTemp,
partialTitle:= UpperCase(partialTitle);
if pos(partialTitle, sTitleTemp) > 0 then // was <> // this section is what I added
begin
if (not AppInstanceFound) and (AppInstanceLaunch) then // these two are safeguards
begin
windows.beep(200,600); // tone,duration -- first of two tone alarm
AppInstanceFound:= true; // _vvv_globals_vvv_ // these are globals if needed for checking
AppInstanceName:= sTitleTemp; // AppInstanceFound is the only critical variable
AppInstanceHandle:= hWndTemp;
end;
Break;
end;
hWndTemp:= GetWindow(hWndTemp, GW_HWNDNEXT);
end;
result:= hWndTemp;
end;
// this makes a single-instance app
// add this code at the top of your FormCreate procedure
// it is what closes down your app if it is a second instance:
MainFrm.Caption:= 'CmdData Extant-Search'; // rename caption so SwitchApp will skip this app
AppInstanceLaunch:= true; // this variable isn't necessary -- only a safeguard
AppInstanceFound:= false; // this variable is critical and its setting is also a safeguard
if AppInstanceLaunch then
begin
SwitchApp('CmdData... '); // 'CmdData... ' is my search string -- my app appends this with my database name
AppInstanceLaunch:= false; // the search string doesn't have to be a prefix -- it can be anywhere within a caption
end;
if AppInstanceFound then
begin
windows.beep(500,600); // tone,duration -- second of two tone alarm
halt; // alternate methods: FormClose(self); application.terminate; Halt;
end else
begin
MainFrm.Caption:= 'CmdData... no project assigned';
end;
// the two tone alarm tells me that each function is working,
// and it informs the user that my app is already launched and active
// after the tones it switches focus to the first instance app.
// the reason for a tone alert is that your app is not visible during OnCreate.
// all of this works best in Win10 if your app is not minimized
// Win10 resists raising a minimized app, but will allow you to switch focus to an active one
// WinXP will allow you to raise a minimized app, but you need to add that code
// the FindWindowExtd function can additionally be used for switching to other active apps, such as NotePad and others.
// the safeguards permit you to do so without interfering with the single instance check, and vice-versa