OpenMutex fails with ERROR_ACCESS_DENIED (using VB.NET)
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Paul Naylor via .NET 247 (VIP)
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("Kernel32", SetLastError:=True, _
CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
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("Kernel32", SetLastError:=True, _
CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
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.GetLastWin32Error() = 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

--------------------------------
From: Paul Naylor

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>usOmTrpmsE2OPoo7+ZDIGA==</Id>
Reply to this message...
 
    
Nick Hall
>"Paul Naylor via .NET 247" <Click here to reveal e-mail address> wrote in message
news:%>Click here to reveal e-mail address...
[Original message clipped]

I *think* that the lpEventsAttribute needs to be passed ByVal instead (since
IntPtr already represents a pointer).

[Original message clipped]

From the docs, dwDesiredAccess should be ByVal as it is a DWORD value.

[Original message clipped]

Given the change above I think this line becomes: -

myWin32Mutex = CreateMutex(IntPtr.Zero, True, "test2_MUTEX")

> ....rest of the code

Hope this helps,

Nick Hall

Reply to this message...
 
    
trayguy (VIP)
Thanks Nick! That was it! I really appreciate it as I can now cease bang my
head against the wall :)
Incidentally - which 'docs' did you refer to to find the VB interop
definitions for those win32 functions? I was trying to guess them from their
C definitions.

Reply to this message...
 
    
Nick Hall
"trayguy" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
> Thanks Nick! That was it! I really appreciate it as I can now cease bang
my
> head against the wall :)

:-)

[Original message clipped]

I didn't exactly - I translated them from the C definitions in the Platform
SDK. If you want a backgrounder on Win32 types and how to translate them,
AllAPI is a good place to start (http://www.mentalis.org/apilist).

Nick Hall

Reply to this message...
 
 
System.EventArgs
System.IntPtr
System.Object
System.Runtime.InteropServices.CharSet
System.Runtime.InteropServices.Marshal
System.Threading.Mutex




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com