- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi there,
Im programming a client in a client server solution.
My problem is, when I start a second instance of my client, the server crashes.
Is there a possibility to check if an instance of the client process is already running so I could prevent a second instance?
Im using Windows7 and XE Update 9.
Thanks in advance,
Markus
Im programming a client in a client server solution.
My problem is, when I start a second instance of my client, the server crashes.
Is there a possibility to check if an instance of the client process is already running so I could prevent a second instance?
Im using Windows7 and XE Update 9.
Thanks in advance,
Markus
Link Copied
14 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
One simple way is to create a named Mutex and try to "acquire" the Mutex at the beginning of the program. If it fails, then another copy of the client is running and you should exit. The Windows API routines for mutexes are straightfoward.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve, you made me curious. Could you give us an example of Fortran code for creating and accessing a named Mutex? I was not able to locate something usable at Microsoft's databases.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Look at the DLL\DLL_Shared_Data sample included with the product. It does this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the hint, Steve. It's indeed simpler than I expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[bash]! create a mutex to block multiple invocations
CALL SetLastError(0) !Just in case
ghMutex = CreateMutex(NULL_SECURITY_ATTRIBUTES, FALSE, "YourMutex"C)
IF (GetLastError() == ERROR_ALREADY_EXISTS) THEN
rval = MessageBox (ghwndMain, &
"Program already running"C, &
"Error"C, MB_OK )
WinMain = 0
RETURN
END IF
[/bash]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, that should work. When the creator exits, the mutex gets deleted automatically. MSDN offers this caution, though:
If you are using a named mutex to limit your application to a single instance, a malicious user can create this mutex before you do and prevent your application from starting. To prevent this situation, create a randomly named mutex and store the name so that it can only be obtained by an authorized user. Alternatively, you can use a file for this purpose. To limit your application to one instance per user, create a locked file in the user's profile directory.
If you are using a named mutex to limit your application to a single instance, a malicious user can create this mutex before you do and prevent your application from starting. To prevent this situation, create a randomly named mutex and store the name so that it can only be obtained by an authorized user. Alternatively, you can use a file for this purpose. To limit your application to one instance per user, create a locked file in the user's profile directory.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It does work, perfectly. And, as long as that mutex is in existence, it can be used for other coding tasks as well.
Your malicious user scenario seems a bit far-fetched; such a malefactor would have to be quite clever and industrious, and a programmer as well, and for what purpose? And the (pre)existence of the duplicate mutex would be easily found with ProcessExplorer or similar.
Your malicious user scenario seems a bit far-fetched; such a malefactor would have to be quite clever and industrious, and a programmer as well, and for what purpose? And the (pre)existence of the duplicate mutex would be easily found with ProcessExplorer or similar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's not my scenario, I just quoted what MSDN said. It is a useful point to think about - while it may not apply in this case, it might in others. At the very least one should be aware of the limitations in using a mutex for this purpose
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Depending on the details and requirements for the "client/server" arrangement (particularly if the "server" will ever run as a Windows service) the name of the Mutex may also need to explicitly be in the global namespace.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello
That tips is very helpful. Is it sure that the mutex is destroyed even if the process crashes?
That tips is very helpful. Is it sure that the mutex is destroyed even if the process crashes?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Paul for the code, I altered it a little bit so it runs in my QuickWin program:
[bash] subroutine checkSingleInstance(MutexName)
use ifwin use ifqwin implicit none integer(kind=4) hMutex character*(*) MutexName logical(kind=4) l ! create a mutex to block multiple invocations call SetLastError(0) !Just in case hMutex = CreateMutex(NULL, .false., MutexName) if (GetLastError() == ERROR_ALREADY_EXISTS) then l = MessageBoxQQ ( & 'Process '//MutexName//' is already running.'//char(10)//char(10)// & 'The program will not start.'//char(0), & 'Error'//char(0), MB$OK.or.MB$ICONSTOP) stop end if end subroutine checkSingleInstance[/bash]It works as it should (for my purpose). When I kill the first process via Task Manager, I can start another "first" instance.
Btw, I get an run-time error "fort: (7): Attempt to use pointer NULL_SECURITY_ATTRIBUTES when it is not associated with a target", so I changed it to NULL.
Markus
[bash] subroutine checkSingleInstance(MutexName)
use ifwin use ifqwin implicit none integer(kind=4) hMutex character*(*) MutexName logical(kind=4) l ! create a mutex to block multiple invocations call SetLastError(0) !Just in case hMutex = CreateMutex(NULL, .false., MutexName) if (GetLastError() == ERROR_ALREADY_EXISTS) then l = MessageBoxQQ ( & 'Process '//MutexName//' is already running.'//char(10)//char(10)// & 'The program will not start.'//char(0), & 'Error'//char(0), MB$OK.or.MB$ICONSTOP) stop end if end subroutine checkSingleInstance[/bash]It works as it should (for my purpose). When I kill the first process via Task Manager, I can start another "first" instance.
Btw, I get an run-time error "fort: (7): Attempt to use pointer NULL_SECURITY_ATTRIBUTES when it is not associated with a target", so I changed it to NULL.
Markus
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The 2nd argument for CreateMutex is the Win32 constant FALSE, which is not the same as the Fortran boolean .FALSE.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well, it will be the same value, but it would be more correct to use FALSE here.
Microsoft says that the mutex will be destroyed when the last handle to it is closed, and exiting a process closes all handles.
Microsoft says that the mutex will be destroyed when the last handle to it is closed, and exiting a process closes all handles.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting onkelhotte
...
Is there a possibility to check if an instance of the client process is already running so I could prevent a second instance?
...
Is there a possibility to check if an instance of the client process is already running so I could prevent a second instance?
...
It can be done in a very simple way:
If your Client application has some title than a call to a Win32 API function FindWindow(..."title"...) or FindWindowEx(..."title"...)
will return some HWND value if the Client is already started.
I used that method many times because it is simple.
Best regards,
Sergey
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page