|
-
May 7th, 2004, 01:06 AM
#1
Rerstricting EXE to single instance
Hi,
From my application I am launchin another exe by calling thrugh WinExec("EXE path",SW_SHOW).But the problem here is EXE is getting opened every time when I click the toolbar butto.I want to restrict this for single instance.
can anybody help me regarding this?
-
May 7th, 2004, 01:36 AM
#2
Hi,
If your application is MFC based, you can try the following things,
The safest way is to use a named Mutex. You can do it like this:
BOOL CMyApp::IsAppRunning ( )
{
HANDLE hMutex = NULL;
hMutex = CreateMutex ( NULL, TRUE, _T("MySpecialMutexNameHERE") );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
CloseHandle ( hMutex );
return TRUE;
}
return FALSE;
}
Make sure you change the Mutex name to something unique for your app. Now, all you have to do from your InitInstance() is :
if ( IsAppRunning () ) return FALSE;
-
May 7th, 2004, 04:39 AM
#3
Originally posted by vrishchik007
If your application is MFC based, you can try the following things,
If your application is not MFC based too you can use IsAppRunning() by creating an object of CMyApp class and calling it from the main function and return if there is already an instance of the application.
regards
collin
Last edited by joscollin; May 7th, 2004 at 04:46 AM.
-
May 7th, 2004, 10:31 AM
#4
Originally posted by joscollin
If your application is not MFC based too you can use IsAppRunning() by creating an object of CMyApp class and calling it from the main function and return if there is already an instance of the application.
regards
collin
The IsAppRunning() function that vrishchik007 wrote contains no MFC code, so you could just incorporate it directly; just define it as a regular function rather than a member of a class, or move it into a different class.
There's no need to create a CMyApp() class and pull in all of MFC just for this function.
-
May 7th, 2004, 12:17 PM
#5
1. Take a look at this Joseph M. Newcomer' article: Avoiding Multiple Instances of an Application
2. Don't use WinExec in Win32 applications: this function is obsolete. You should use CreateProcess or ShellExecuteEx instead.
-
May 8th, 2004, 12:21 AM
#6
Originally posted by wayside
There's no need to create a CMyApp() class and pull in all of MFC just for this function.
The need for creating a class depends on ThomasBoseKenn's coding. He might be coding in MFC or in Win32 C++.
I just want to say this code will work in a win32 project too.
regards
collin.
Last edited by joscollin; May 8th, 2004 at 12:25 AM.
-
May 10th, 2004, 01:13 AM
#7
Hi,
Thanks to u both for the response...but the thing is I don't have the code of EXE which I am launching from my apllication !!
.....I think that I need to get the process ID and then restrict it...but I don't know how to get the process ID of a perticular application!..can u ppl help me regarding this...!!??
thanx in advance...
-
May 10th, 2004, 01:48 AM
#8
PID's will be different for two instances of the same application.
If you are spawning the process from your application then do it only once. Also the PID can be retrieved from PROCESS_INFORMATION structure after calling CreateProcess().
regards
collin.
-
May 10th, 2004, 01:54 AM
#9
Originally posted by ThomasBoseKenn
Hi,
Thanks to u both for the response...but the thing is I don't have the code of EXE which I am launching from my apllication !!
.....I think that I need to get the process ID and then restrict it...but I don't know how to get the process ID of a perticular application!..can u ppl help me regarding this...!!??
thanx in advance...
why don't you do it simpler?
launch the process the first time and after FindWindow(...) to see if there is already an instance of that process running. if there is do nothing.
-
May 10th, 2004, 03:42 AM
#10
This FAQ should answer your questions.
"A problem well stated is a problem half solved.” - Charles F. Kettering
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|