- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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
- Balises:
- Intel® Fortran Compiler
Lien copié
14 Réponses
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
Look at the DLL\DLL_Shared_Data sample included with the product. It does this.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
Thanks for the hint, Steve. It's indeed simpler than I expected.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
[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]
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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?
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
The 2nd argument for CreateMutex is the Win32 constant FALSE, which is not the same as the Fortran boolean .FALSE.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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.
- Marquer comme nouveau
- Marquer
- S'abonner
- Sourdine
- S'abonner au fil RSS
- Surligner
- Imprimer
- Signaler un contenu inapproprié
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
Répondre
Options du sujet
- S'abonner au fil RSS
- Marquer le sujet comme nouveau
- Marquer le sujet comme lu
- Placer ce Sujet en tête de liste pour l'utilisateur actuel
- Marquer
- S'abonner
- Page imprimable