- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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
링크가 복사됨
14 응답
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
Look at the DLL\DLL_Shared_Data sample included with the product. It does this.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
[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]
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
The 2nd argument for CreateMutex is the Win32 constant FALSE, which is not the same as the Fortran boolean .FALSE.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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.
- 신규로 표시
- 북마크
- 구독
- 소거
- RSS 피드 구독
- 강조
- 인쇄
- 부적절한 컨텐트 신고
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