Jump to Real's How-to Main page

Detect if an application is already running

[external function declaration]
FUNCTION ulong CreateMutexA &
   (ulong lpMutexAttributes, boolean bInitialOwner, REF string lpszName) &
   LIBRARY "kernel32.dll"
FUNCTION long GetLastError() LIBRARY "kernel32.dll"

[powerscript]
// boolean of_IsRunning()
//
//        IF of_isRunning THEN
//           MessageBox("Oups", "already running!")
//        END IF
//

constant ulong ERROR_ALREADY_EXISTS = 183
constant ulong SUCCESSFUL_EXECUTION = 0

ulong lul_mutex
ulong lpsa
ulong lul_last_error
boolean lb_ret = FALSE

IF NOT (Handle(GetApplication()) = 0) THEN
    lul_mutex = CreateMutexA(lpsa, FALSE, as_appname)
    lul_last_error = GetLastError()
    lb_ret = NOT (lul_last_error = SUCCESSFUL_EXECUTION)
END IF

RETURN LB_RET

If you find this article useful, consider making a small donation
to show your supportfor this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]