Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2004

Tip: Looking for answers? Try searching our database.

OpenMutex fails with ERROR_ACCESS_DENIED (using VB.NET)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul Naylor - 08 Sep 2004 04:46 GMT
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
Nick Hall - 08 Sep 2004 12:31 GMT
>"Paul Naylor via .NET 247" <anonymous@dotnet247.com> wrote in message
news:%>23Nx8jZVlEHA.536@TK2MSFTNGP11.phx.gbl...

>    'CreateMutex
>    <System.Runtime.InteropServices.DllImport("Kernel32", SetLastError:=True, _
[quoted text clipped - 3 lines]
>                ByVal bInitialOwner As Boolean, ByVal lpName As String) As IntPtr
>    End Function

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

>    'OpenMutex
>    <System.Runtime.InteropServices.DllImport("Kernel32", SetLastError:=True, _
[quoted text clipped - 3 lines]
>                ByVal lpName As String) As IntPtr
>    End Function

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

>    Public Const SYNCHRONIZE = &H100000&
>    Public Const ERROR_ACCESS_DENIED = 5
[quoted text clipped - 4 lines]
>        Dim myWin32Mutex As IntPtr
>        myWin32Mutex = CreateMutex(Nothing, True, "test2_MUTEX")

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.