locked
Using a mutex to check if an application is already running in windows mobile RRS feed

  • Question

  • Hi, i´m new in this forum and to windows mobile and could use some help.

    I am doing an application using visual basic and compact framework 3.5 and i have a function to check if the application is already ruining, i am using a named mutex and i call the method GetLast error to check if the mutex exist or not. this is what i've done so far.

    Public Class Class1
    
        Public Declare Function CreateMutex Lib "Coredll.dll" (ByRef lpMutexAttributes As IntPtr, _
                                ByVal bInitiaOwner As Boolean, ByVal lpName As String) As IntPtr
        Public Declare Function GetLastError Lib "CoreDll.dll" () As Integer
    
        Public Declare Sub CloseHandle Lib "coredll.dll" (ByVal hMutex As IntPtr)
    
        Const ERROR_ALREADY_EXISTS = 183
    
    
        Public Function NamedMutex()
            Dim res As Integer
            Dim mut As IntPtr
            Dim app As String = "visium@logistics"
            mut = CreateMutex(IntPtr.Zero, True, app)
            'res = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
            res = GetLastError()
    
            If res = ERROR_ALREADY_EXISTS Then
                CloseHandle(mut)
                Return False
            End If
    
            'Dim mut As New Mutex(True)
    
            'If Not mut.WaitOne(0, False) Then
            '    '. already running
            '    mut.ReleaseMutex()
    
            'Return False
            'End If
    
            Return True
        End Function
    
    
    End Class

    when the mutex does not exist i GetLastError() returns 6 ERROR_INVALID_HANDLE, else returns 87 ERROR_INVALID_PARAMETER when it should return 183 ERROR_ALREADY_EXISTS. i also try using System.Runtime.InteropServices.Marshal.GetLastWin32Error() instead of GetLastError but i always get the error 87.


    Tuesday, October 16, 2012 8:51 AM

All replies

  • Why do you need to do that? Windows Mobile would automatically enforce single instance behavior for Forms application on Windows Mobile.

    Is that on Windows CE or you're using console app?


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Tuesday, October 16, 2012 3:53 PM
  • I am using Windows CE 6.0 and it allows me to start two instances of the same application. 
    Tuesday, October 16, 2012 8:19 PM