469,648 Members | 1,417 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

OpenMutex fails with ERROR_ACCESS_DENIED

Accepting the fact that .NET 1.x is lacking support for named events, and
windows security manipulation, we end up using interop and the platform SDK,
and functions like CreateMutex and OpenMutex. And it's with OpenMutex that
I'm having a problem.

In summary: I'm creating a mutex and then trying to 'open' it. I always get
ERROR_ACCESS_DENIED which seems odd as the same process just created it
moments before.

This example is actually a much simplified example of my production
suituation where I'm using named events as well (which .NET doesn't support).

Incidentally if I use the managed .NET new Mutex() to create the mutex then
open it with OpenMutex then I have the same problem. But if I use the Win32
API CreateMutex, and then managed .NET's new Mutex() to open the existing
named mutex then I don't have a problem... so the problem surely lies with
OpenMutex, ...I would think.

I've also looked at creating Security Descriptors, which scare the living
daylights out of me, but if I understand it correctly, by passing 'Nothing'
into The CreateMutex() Security Decsriptor parameter I'm telling it to use
the default descriptor which should be fine for the purposes of multiple
Windows VB App's being run on the same machine by the same user, and in the
case of our test case below should certainly be fine given that it's even the
same process thats asking for SYNCHRONIZE access.

I have a complete chunk of sample code you should be able to paste into a
newly created blank form VB.NET project, to test. The code fire's on the form
load so just run it to reproduce the problem. I anxiously await your reply.

'CreateMutex
<System.Runtime.InteropServices.DllImport("Kernel3 2",
SetLastError:=True, _
CharSet:=System.Runtime.InteropServices.CharSet.Au to)> _
Public Shared Function CreateMutex( _
ByRef lpEventAttributes As IntPtr, _
ByVal bInitialOwner As Boolean, ByVal lpName As String) As
IntPtr
End Function

'OpenMutex
<System.Runtime.InteropServices.DllImport("Kernel3 2",
SetLastError:=True, _
CharSet:=System.Runtime.InteropServices.CharSet.Au to)> _
Public Shared Function OpenMutex( _
ByRef dwDesiredAccess As Integer, ByVal bInheritHandle As
Boolean, _
ByVal lpName As String) As IntPtr
End Function

Public Const SYNCHRONIZE = &H100000&
Public Const ERROR_ACCESS_DENIED = 5

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' create a named mutex using Win32 API
Dim myWin32Mutex As IntPtr
myWin32Mutex = CreateMutex(Nothing, True, "test2_MUTEX")
If myWin32Mutex.Equals(IntPtr.Zero) Then
Throw New Exception("3")
End If

' get a reference to an existing named mutex using Win32 API
Dim tempWin32Mutex As IntPtr = OpenMutex(SYNCHRONIZE, True,
"test2_MUTEX")
If tempWin32Mutex.Equals(IntPtr.Zero) Then
If System.Runtime.InteropServices.Marshal.GetLastWin3 2Error() =
ERROR_ACCESS_DENIED Then
Throw New Exception("4") ' <--- IT DIES HERE
End If
Throw New Exception("5")
End If
Throw New Exception("6")
End Sub
Jul 21 '05 #1
0 1817

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by trayguy | last post: by
6 posts views Thread by kenneth fleckenstein nielsen | last post: by
2 posts views Thread by Richard Hsu | last post: by
2 posts views Thread by su haiqing | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.