WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Create and Release Mutex


;Define Constants
mutexname = "SPSuserMode5"
SECURITY_DESCRIPTOR_REVISION = 1

#DefineFunction udfDefineSecurityAttributes( revisionlevel )
   ;Define SECURITY_ATTRIBUTES structure for CreateMutexA API call so that handles are inheritable by the child

   bbSEC_ATTR = BinaryAlloc( 12 ) ; allocate buffer to hold structure
   BinaryPoke4( bbSEC_ATTR, 0, 12 ) ; size of SECURITY_ATTRIBUTES structure (DWORD nLength;)
   If WinVersion( 4 ) == 4 ;If running on WinNT/2000/XP initialize security descriptor
      bbSEC_DESC = BinaryAlloc( 20 )
      If !DllCall( StrCat( DirWindows(1), "advapi32.dll" ), long:"InitializeSecurityDescriptor", lpbinary:bbSEC_DESC, long:revisionlevel )
         Message( "Error", StrCat( "InitializeSecurityDescriptor failed. Errorcode = ", DllLastError() ) )
         Exit
      EndIf
      If !DllCall( StrCat( DirWindows(1), "advapi32.dll" ), long:"SetSecurityDescriptorDacl", lpbinary:bbSEC_DESC, long:1, lpnull, long:0 )
         Message( "Error", StrCat( "SetSecurityDescriptorDacl failed. Errorcode = ", DllLastError()) )
         Exit
      EndIf
      BinaryPoke4( bbSEC_ATTR, 4, IntControl (42, bbSEC_DESC, 0, 0, 0) ) ; pointer to security descriptor
   Else
      BinaryPoke4( bbSEC_ATTR, 4, 0 ) ; null pointer for security descriptor  (LPVOID lpSecurityDescriptor;)
   EndIf
   BinaryPoke4( bbSEC_ATTR, 8, @TRUE ) ;inheritable attribute of SECURITY_ATTRIBUTES structure (BOOL bInheritHandle;)
   Return  bbSEC_ATTR
#EndFunction

#DefineFunction udfCreateMutex( bbSEC_ATTR, mutexname )
   ;Call CreateMutexA
   ERROR_ALREADY_EXISTS = 183
   kernel32 = StrCat( DirWindows( 1 ), "kernel32.dll" )
   hmutex = DllCall( kernel32, long:"CreateMutexA", lpbinary: bbSEC_ATTR, long:@TRUE, lpstr:mutexname )
   error = DllLastError()
   If hmutex == 0 Then Message( "CreateMutexA Error", error )
   If error == ERROR_ALREADY_EXISTS Then Return (-1) ;mutex exist an hinstance is running
   Return hmutex
#EndFunction

#DefineFunction udfOpenMutex( mutexname )
   ;CallOpenMutexA
   MUTEX_MODIFY_STATE = 1
   MUTEX_ALL_ACCESS = 2031617
   kernel32 = StrCat( DirWindows( 1 ), "kernel32.dll" )
   hmutex = DllCall( kernel32, long:"OpenMutexA", long:MUTEX_ALL_ACCESS, long:@TRUE, lpstr:mutexname )
   error = DllLastError()
   If hmutex == 0
      Message( "OpenMutexA Error", error )
       Exit
   EndIf
   Return hmutex
#EndFunction


#DefineFunction udfReleaseMutex( hmutex )
   ;Call CreateMutexA
   kernel32 = StrCat( DirWindows( 1 ), "kernel32.dll" )
   res = DllCall( kernel32, long:"ReleaseMutex", long:hmutex )
   error = DllLastError()
   If res == 0 Then Message( "ReleaseMutex Error", error )
   Return res
#EndFunction

#DefineFunction udfCloseHandle( hmutex )
    ;Call CloseHandle
    kernel32 = StrCat( DirWindows( 1 ), "kernel32.dll" )
    res = DllCall( kernel32, long:"CloseHandle", long:hmutex )
   Return res
#EndFunction


;Call UDF that defines SECURITY_ATTRIBUTES structure
bbSEC_ATTR = udfDefineSecurityAttributes( SECURITY_DESCRIPTOR_REVISION )

;Call CreateMutexA
hmutex = udfCreateMutex( bbSEC_ATTR, mutexname )
If hmutex
   Message("Mutex","Has been Created")
EndIf

If hmutex == -1
   Message("Mutex","Already Exists")
EndIf

;Get Handle to Mutex using OpenMutex
hmutex = udfOpenMutex( mutexname )

;Call ReleaseMutex
ret = udfReleaseMutex ( hmutex )
If ret
   udfCloseHandle( hmutex )
   Message("Mutex","Has been Released")
EndIf

Pause( "Wait for another process","")

Article ID:   W17404
File Created: 2008:04:10:15:09:18
Last Updated: 2008:04:10:15:09:18