oceans Posted February 24, 2009 Share Posted February 24, 2009 Any one has working code for mutex. I am trying, but could not get it right. Do you think if I post my small code, you can find my mistake. Quote Link to comment https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/ Share on other sites More sharing options...
corbin Posted February 24, 2009 Share Posted February 24, 2009 Not much of a VB person, but I have a little experience in C/C++. I could try to help if you post code/explain your problem. Quote Link to comment https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769732 Share on other sites More sharing options...
oceans Posted February 24, 2009 Author Share Posted February 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769739 Share on other sites More sharing options...
corbin Posted February 24, 2009 Share Posted February 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769750 Share on other sites More sharing options...
oceans Posted February 24, 2009 Author Share Posted February 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769754 Share on other sites More sharing options...
corbin Posted February 24, 2009 Share Posted February 24, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769762 Share on other sites More sharing options...
oceans Posted February 24, 2009 Author Share Posted February 24, 2009 I think we are locking on, let me mutate your C to my B. Quote Link to comment https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769791 Share on other sites More sharing options...
oceans Posted February 24, 2009 Author Share Posted February 24, 2009 I thank you. Not only because you solved it, you took effort to handle a "forgin language" for me. Quote Link to comment https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769796 Share on other sites More sharing options...
corbin Posted February 24, 2009 Share Posted February 24, 2009 Eh, gave me something to do ;p. Quote Link to comment https://forums.phpfreaks.com/topic/146619-solved-mutex-vb60/#findComment-769800 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.