Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for the existence of other instances a project

Status
Not open for further replies.

Donald

Programmer
Mar 13, 2000
15
0
0
CA
How can I make sure that a c++ program is the only instance (of itself) running on the machine?<br>
<br>
thanks<br>
<br>
D
 
check to see if the handle hPrevInstance is NULL in WinMain. If it is, then the application is the first instance.<br>
If not, then there is another copy of the program already running.<br>
<br>
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,<br>
LPSTR lpszCmdLine, int nCmdShow)<br>
<p> <br><a href=mailto:Kim_Christensen@telus.net>Kim_Christensen@telus.net</a><br><a href= Page</a><br>
 
zBuilder,<br>
<br>
Can you expand on this a bit. I've seen the use of mutex to do this but some of the machines I work on do not have mutex running.<br>
<br>
Donald,<br>
I've also seen the use of creating a file in a certain directory when a program starts. If the program is started again, the program sees the file and knows that it has already been started. The downside to this is if the program crashes, the program is left open and someone has to manually delete the file before the program can start again.<br>
<br>
<p>James P. Cottingham<br><a href=mailto:main@ivcusa.com>main@ivcusa.com</a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
2ffat,<br>
<br>
I'm thinking of machines with Win3.1, Win95/98 running as the GUI / OS<br>
Under these environments, the OS supplies to the newly executed application/window a handle to any previous instance of itself. WinMain is the entry point for all MsWindows applications whether they use OWL, MFC, etc... It may be hidden in an object somewhere, if the programmer is using a OOPs interface to the windows API.<br>
Under another OS, this would be done differently though...<br>
<br>
Donald,<br>
<br>
Let us know if you ARE compiling for the Windows environment or not....<br>
<p> <br><a href=mailto:Kim_Christensen@telus.net>Kim_Christensen@telus.net</a><br><a href= Page</a><br>
 
I am compiling for win 95 but I'm having trouble using WinMain. Where do I put it? How is it implemented? So far, it's been quietly ignored by the compiler.<br>
<br>
D
 
Ahhh... Ok. Win95. My example was for 16bit windows... I'm no expert on Win32 though. Below is an exerpt from a win32 help file (Hope it helps)<br>
<br>
The WinMain function is called by the system as the initial entry point for a Win32-based application. <br>
<br>
int WINAPI WinMain(<br>
<br>
HINSTANCE hInstance, // handle to current instance<br>
HINSTANCE hPrevInstance, // handle to previous instance<br>
LPSTR lpCmdLine, // pointer to command line<br>
int nCmdShow // show state of window<br>
); <br>
Parameters<br>
<br>
hInstance<br>
<br>
Identifies the current instance of the application. <br>
<br>
hPrevInstance<br>
<br>
Identifies the previous instance of the application. For a Win32-based application, this parameter is always NULL. If you need to detect whether another instance already exists, create a named mutex using the CreateMutex function. If the GetLastError function returns ERROR_ALREADY_EXISTS, another instance of your application exists (it created the mutex).<br>
<br>
lpCmdLine<br>
<br>
Points to a null-terminated string specifying the command line for the application. <br>
<br>
nCmdShow<br>
<br>
Specifies how the window is to be shown. This parameter can be one of the following values: <br>
<br>
Value Meaning<br>
SW_HIDE Hides the window and activates another window.<br>
SW_MINIMIZE Minimizes the specified window and activates the top-level window in the system’s list.<br>
SW_RESTORE Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position (same as SW_SHOWNORMAL).<br>
SW_SHOW Activates a window and displays it in its current size and position.<br>
SW_SHOWMAXIMIZED Activates a window and displays it as a maximized window.<br>
SW_SHOWMINIMIZED Activates a window and displays it as an icon.<br>
SW_SHOWMINNOACTIVE Displays a window as an icon. The active window remains active.<br>
SW_SHOWNA Displays a window in its current state. The active window remains active.<br>
SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.<br>
SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position (same as SW_RESTORE).<br>
Return Values<br>
<br>
If the function succeeds, terminating when it receives a WM_QUIT message, it should return the exit value contained in that message’s wParam parameter. If the function terminates before entering the message loop, it should return 0. <br>
<br>
Remarks<br>
<br>
WinMain initializes an application, displays its main window, and then enters a message retrieval-and-dispatch loop that is the top-level control structure for the remainder of the application’s execution. The message loop terminates when a WM_QUIT message is received. At that point, WinMain exits the application, returning the value passed in the WM_QUIT message’s wParam parameter. If WM_QUIT was received as a result of calling PostQuitMessage, the value of wParam is the value of the PostQuitMessage function’s nExitCode parameter. For more information, see Creating a Message Loop.<br>
<br>
<p> <br><a href=mailto:Kim_Christensen@telus.net>Kim_Christensen@telus.net</a><br><a href= Page</a><br>
 
zBuilder,<br>
<br>
We are using WinNT 4.0. The snippet that I saw was from BCBDEV.COM site. It uses a mutex. I will try your example and see how it works. Thanks<br>
<br>
<p>James P. Cottingham<br><a href=mailto:main@ivcusa.com>main@ivcusa.com</a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
Here is a Solution using Mutex:<br><br>WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)<br>{<br>int Mutex;<br>Mutex=(int)CreateMutex(NULL,true,GetPrevInstMutexName());<br>if((Mutex!=NULL)&& (GetLastError()==0))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application-&gt;Initialize();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application-&gt;CreateForm(__classid(TForm1), &Form1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application-&gt;Run();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;catch (Exception &exception)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application-&gt;ShowException(&exception);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top