Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Check if process is already started

onkelhotte
New Contributor II
1,951 Views
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
0 Kudos
14 Replies
Steven_L_Intel1
Employee
1,951 Views
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.
0 Kudos
rase
New Contributor I
1,951 Views
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.
0 Kudos
Steven_L_Intel1
Employee
1,951 Views
Look at the DLL\DLL_Shared_Data sample included with the product. It does this.
0 Kudos
rase
New Contributor I
1,951 Views
Thanks for the hint, Steve. It's indeed simpler than I expected.
0 Kudos
Paul_Curtis
Valued Contributor I
1,951 Views
[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]
0 Kudos
Steven_L_Intel1
Employee
1,951 Views
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.
0 Kudos
Paul_Curtis
Valued Contributor I
1,951 Views
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.
0 Kudos
Steven_L_Intel1
Employee
1,951 Views
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
0 Kudos
IanH
Honored Contributor III
1,951 Views
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.
0 Kudos
GVautier
New Contributor III
1,951 Views
Hello

That tips is very helpful. Is it sure that the mutex is destroyed even if the process crashes?
0 Kudos
onkelhotte
New Contributor II
1,951 Views
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
0 Kudos
Paul_Curtis
Valued Contributor I
1,951 Views
The 2nd argument for CreateMutex is the Win32 constant FALSE, which is not the same as the Fortran boolean .FALSE.
0 Kudos
Steven_L_Intel1
Employee
1,951 Views
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.
0 Kudos
SergeyKostrov
Valued Contributor II
1,951 Views
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?
...

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
0 Kudos
Reply