27

I have imported the kernel32 library. So, I have the createMutex function available but I am not quite sure of the various parameters and return values.

This is classic Visual Basic, not Visual Basic.NET but I can probably work with either language in the form of an answer.

eglease
  • 2,445
  • 11
  • 18
  • 28
Goyuix
  • 23,614
  • 14
  • 84
  • 128

3 Answers3

10

Here's the VB6 declarations for CreateMutex - I just copied them from the API viewer, which you should have as part of your VB6 installation. VB6 marshalls strings to null-terminated ANSI using the current code page.

Public Type SECURITY_ATTRIBUTES
   nLength As Long
   lpSecurityDescriptor As Long
   bInheritHandle As Long 
End Type

Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" _
   (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, _
    ByVal lpName As String) As Long

Bear in mind that if you create a mutex from the VB6 IDE, the mutex belongs to the IDE and won't be destroyed when you stop running your program - only when you close the IDE.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
8

The VB code looks something like this:

hMutex = CreateMutex(ByVal 0&, 1, ByVal 0&)

The first parameter is a pointer to an SECURITY_ATTRIBUTES structure. If you don't know what it is, you don't need it. Pass NULL (0).

The second parameter is TRUE (non-zero, or 1) if the calling thread should take ownership of the mutex. FALSE otherwise.

The third parameter is the mutex name and may be NULL (0), as shown. If you need a named mutex, pass the name (anything unique) in. Not sure whether the VB wrapper marshals the length-prefixed VB string type (BSTR) over to a null-terminated Ascii/Unicode string if not, you'll need to do that and numerous examples are out there.

Good luck!

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
1

Well, based on the documentation it looks like:

  1. Security attributes (can pass null)
  2. Whether it's initially owned (can pass false)
  3. The name of it

HTH

whoan
  • 8,143
  • 4
  • 39
  • 48
Darren Kopp
  • 76,581
  • 9
  • 79
  • 93