File: n_mutex.sru
Size: 1276
Date: Mon, 31 Dec 2018 21:14:38 +0100
$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 ulong CreateMutex ( &
   ulong lpMutexAttributes, &
   int bInitialOwner, &
   Ref string lpName &
   ) Library "kernel32.dll" Alias For "CreateMutexA"

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

Ulong lul_mutex, lul_error
Boolean lb_prev
String ls_appname
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
lul_mutex = CreateMutex(0, 0, 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