Article View: comp.os.ms-windows.programmer.misc
Article #2940Re: How to avoid multiple instances of a program in NT and 95?
From: Horst Schmid
Date: Sun, 09 Feb 1997 00:00
Date: Sun, 09 Feb 1997 00:00
98 lines
4427 bytes
4427 bytes
Slawomir Marczynski wrote:
>
> Richard Baldwin (richard@ormesci.demon.co.uk) wrote:
> : >I've been striving for so long now to find a SAVE way to start only one
> : >instance of my C++ program under Windows NT and 95. As you might recall, this
>
> : In CMainFrame
> : =============
> : BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
>
> There is also an example (I've forgotten this name) on BC5.01 CDROM
> with OWL instead MFC.
>
> --
> Slawomir Marczynski (Mr)
> Institute of Physics, Technical University of Szczecin
> Al. Piastow 48/49, 70-310 Szczecin, Poland
> slawek@arcadia.inter.tuniv.szczecin.pl, tel:+(048-91)-494056
For Delphi programmers I have a solution that ask user in case of starting the 2nd time to switch
to the first or to cancel:
Horst Schmid, http://ourworld.compuserve.com/homepages/Horst_Schmid
unit once; {Ask User to switch to the first Instance instead of starting a second}
{Simply add this Unit to your uses-Statement of your Delphi2.0-Project.dpr
to prevent starting Your App. more than once!}
interface
const Titel:pchar='Warning'; {You can change this to whatever you want}
Msg:String='App. %s is already running, switch to it?';
implementation
uses windows,sysutils,tlhelp32;
procedure SwitchToThisWindow(h1:hWnd; x:bool);stdCall; external user32 name 'SwitchToThisWindow';
{x=false: Size unchanged, x=true: normal size}
function EnumThreadWndProc(h1:hWnd;LPARAM:lParam):bool;stdCall; {Callback}
var hp:word;
begin
if IsWindowVisible(h1) then begin
hp:=GetLastActivePopup(h1);
if IsWindow(hp) then begin
SwitchToThisWindow(hp,true);
end end;
EnumThreadWndProc:=true;
end;
var mHandle: THandle; // Mutexhandle
s:string;
i:integer;
b:bool;
h,h2:tHandle;
lppe: TProcessEntry32;
lpte: TThreadEntry32;
pid0,tid:dWord;
x:array[0..255]of char;
Initialization {called when App is started}
s:=ParamStr(0); {Progname as MutexName}
i:=length(s);
while (i>0) and (s[i]<>'\') do dec(i); {without Path}
mHandle := CreateMutex(nil,True,pchar(copy(s,i+1,255)));
if GetLastError = ERROR_ALREADY_EXISTS then begin // App is running
i:=MessageBox(0,pchar(Format(Msg,[copy(s,i+1,255)]))
,Titel,MB_ICONQUESTION+MB_OKCANCEL);
if i=IDOK then begin {find the other Task and bring to top}
tid:=0;
h:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
lppe.dwSize:=SizeOf(lppe);
b:=Process32First(h,lppe);
pid0:=GetCurrentProcess;
s:=s+#0; {make String to ASCIIZ}
while b do begin {Proccess-Loop}
if (StrComp(@s[1],lppe.szExeFile)=0)and(lppe.th32ProcessID<>pid0) then begin
h2:=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
lpte.dwSize:=SizeOf(lpte);
b:=Thread32First(h2,lpte);
tid:=0;
while b do begin {Thread-Loop}
if lpte.th32OwnerProcessID=lppe.th32ProcessID then begin
tid:=lpte.th32ThreadID;
EnumThreadWindows(tid,@EnumThreadWndProc,0) end;
b:=Thread32Next(h2,lpte) end;
end
else b:=Process32Next(h,lppe) end;
CloseHandle(h2);
CloseHandle(h);
end;
halt;{Cancel second start. Delete this line if the the second Choice
should be to start a second App.}
end;
finalization {End of App:Delete Mutex}
if mHandle <> 0 then CloseHandle(mHandle)
end.
Technical Details
Message-ID:
<32FD964F.621A@compuserve.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<5d4eeb$n6u$1@euler.space.net> <aFwHpCAU3e9yEwa8@ormesci.demon.co.uk> <32fb49bb.0@news.man.szczecin.pl>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1124cf,3403ef0501dd273 X-Google-Attributes: gid1124cf,public X-Google-Thread: f6ef4,3403ef0501dd273 X-Google-Attributes: gidf6ef4,public X-Google-Thread: 113dfb,3403ef0501dd273 X-Google-Attributes: gid113dfb,public X-Google-Thread: 105df1,3403ef0501dd273 X-Google-Attributes: gid105df1,public From: Horst Schmid <Horst_Schmid@compuserve.com> Subject: Re: How to avoid multiple instances of a program in NT and 95? Date: 1997/02/09 Message-ID: <32FD964F.621A@compuserve.com> X-Deja-AN: 217500349 references: <5d4eeb$n6u$1@euler.space.net> <aFwHpCAU3e9yEwa8@ormesci.demon.co.uk> <32fb49bb.0@news.man.szczecin.pl> content-type: text/plain; charset=us-ascii organization: (private) mime-version: 1.0 newsgroups: comp.os.ms-windows.programmer.misc,comp.os.ms-windows.programmer.win32,de.comp.os.ms-windows.programmer,fido.ger.windows.prog x-mailer: Mozilla 2.02 (Win95; I; 16bit)