Check if an application is already running in widows mobile using a mutex

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
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 ive
done so far.
<pre class="prettyprint 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[/code]
<br/>
when the mutex does not exist i GetLastError() returns 6 <span class="x_Apple-style-span" style="color:#273d49; font-family:arial; font-size:13px; line-height:normal ERROR_INVALID_HANDLE, else returns 87 <span class="x_Apple-style-span" style="font-family:Segoe UI,Verdana,Arial; font-size:13px ERROR_INVALID_PARAMETER
when it should return 183 <span class="x_Apple-style-span" style="font-family:Segoe UI,Verdana,Arial; font-size:13px ERROR_ALREADY_EXISTS. i also try using
System.Runtime.InteropServices.Marshal.GetLastWin32Error() <span style="font-size:small <span class="x_Apple-style-span" style="line-height:19px instead of GetLastError but i always get the error 87.
<span class="x_Apple-style-span" style="font-family:Segoe UI,Verdana,Arial; font-size:13px
<span class="x_Apple-style-span" style="font-family:Segoe UI,Verdana,Arial; font-size:13px


View the full article
 
Back
Top