File: n_mutex.sru
Size: 1295
Date: Thu, 14 Jul 2022 19:28:12 +0200
$PBExportHeader$n_mutex.sru
forward
global type n_mutex from nonvisualobject
end type
end forward

global type n_mutex from nonvisualobject autoinstantiate
end type

type prototypes
Function longptr CreateMutex ( &
   longptr lpMutexAttributes, &
   boolean bInitialOwner, &
   Ref string lpName &
   ) Library "kernel32.dll" Alias For "CreateMutexW"

Function ulong GetLastError ( &
   ) Library "kernel32.dll"

end prototypes

forward prototypes
public function boolean of_apprunning ()
end prototypes

public function boolean of_apprunning ();// this function determines if previous instance of application is running

Boolean lb_prev
Longptr ll_mutex
String ls_appname
ULong lul_error
Application la_app

la_app = GetApplication()

// if running from PB, allow duplicates
If Handle(la_app) = 0 Then Return False

// create Mutex using AppName as key
ls_appname = la_app.AppName
ll_mutex = CreateMutex(0, True, ls_appname)

// error 183 indicates the Mutex already exists
lul_error = GetLastError()
If lul_error = 183 Then
   lb_prev = True
End If

Return lb_prev

end function

on n_mutex.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_mutex.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on