Jump to content

Recommended Posts

I am trying to use the following code for MUTEX operation, but it is not working, I am failing to find where I am going wrong.

 

"I want only one instance of my application to run at any one time, the new instance should END with no harm to the old one"

 

Can you point out where I am wrong. (My code does not prevent the second instance)

 


'code in my sub main()
Option Explicit
Public Declare Function OpenMutex Lib "KERNEL32.DLL" Alias "OpenMutexA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Boolean, ByVal lpName As String) As Long
Public Declare Function ReleaseMutex Lib "KERNEL32.DLL" (ByVal hMutex As Long) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Declare Function CreateMutex Lib "KERNEL32.DLL" Alias "CreateMutexA" (ByVal lpMutexAttributes As Any, ByVal bInitialOwner As Boolean, ByVal lpName As String) As Long

Public LongMutex As Long
    
Public Sub Main()
    If ProgramRun = True Then End
    Form1.Show
End Sub

Private Function ProgramRun() As Boolean
    Dim ReturnReleaseLong As Long
    Dim ReturnCloseLong As Long
    If OpenMutex(0, True, "Project1") Then
        'ReturnReleaseLong = ReleaseMutex(LongMutex)
        'CloseHandle LongMutex
        ProgramRun = True
    Else
        LongMutex = CreateMutex(ByVal 0&, False, "Project1")
        ProgramRun = False
    End If
End Function

 


'code in my form 
Option Explicit
Private Sub Form_Unload(Cancel As Integer)
Dim ReturnReleaseLong As Long
Dim ReturnCloseLong As Long

    If OpenMutex(0, True, "Project1") Then
        ReturnReleaseLong = ReleaseMutex(LongMutex)
        CloseHandle LongMutex
    End If
    
    
End Sub

Link to comment
https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769739
Share on other sites

Hrmmm....  would help if I knew more about VB....

 

Have you narrowed down the problem?  Does the mutex create the first time sucessfully?  Does the app grab the handle to it correctly the second time?

 

 

What's the full flow of the code?  Is Form_Unload called at the end of Form (or Form::Show or what ever)?

 

 

Hrmmmm.....  I think I'm going to mess with CreateMutex and OpenMutex in C++ for a second.

Link to comment
https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769750
Share on other sites

Corbin I Thank you,

Though you mention you are not good in VB, you are lending your hand. Thanks.

 

Flow --> 'code in my sub main() --> create one if not present (if present End the program) -->Form1.Show --> "now we are in form1"--> on exit of the program -->Form_Unload.

 

My situation I keep on opening same applicaition. (if present End the program) does not happen

 

Link to comment
https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769754
Share on other sites

Hrmmm....  After some expirimenting in C++, I have figured this out (and after some Googling, hehe):

 

Apparently CreateMutex returns a handle no matter well (well essentially no matter what).  It's the ownership of it that needs to be checked.  I'm pretty sure you know this.  But there seems to be an easier way.

 

 

If you call GetLastError, you can check it against ERROR_ALREADY_EXISTS.

 

 

So, in VB, the code would go something like this (I think):

 

Public Declare Function GetLastError Lib "KERNEL32.DLL" Alias "GetLastError" () As unsigned long

 

 

It's a function declared in Kernel32 that has no parameters and returns a DWORD value.  (No idea if it's thread safe.  Seems like it could potentially defeat the purpose in some situations if not....)

 

 

Anyway:

 

Hrmmm.....  Wish I knew that VB version of external.......  Or I wish you could just include headers in VB lol.  (Or can you???)

 

 

Anyway, in C++ it would look something like this:

 

 

HANDLE h = CreateMutex(NULL, true, "SomeName");

if(h == NULL) {

    //unable to get handle for some weird reason

}

if(GetLastError() == ERROR_ALREADY_EXISTS) {

    //a process is already running (well, a mutex already exists)

    exit 0;

}

Link to comment
https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769762
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.