1

I am using mutex to synchronize two process. Mutex is getting created by service process. But when client tries to access that mutex he gets Unauthorizedaccessexception. The user account has create global object rights This happens in few machines running windows 7 but not reproducible on other windows 7 machine. What could be the reason?

Following is the code for creation of global mutex

bool gCreated;
Mutex syncMutex = new Mutex(true, "Global\\SWDBOject", out gCreated);
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();  
securitySettings.AddAccessRule(allowEveryoneRule);
syncMutex.SetAccessControl(securitySettings);
Paul Farry
  • 4,730
  • 2
  • 35
  • 61
user853390
  • 21
  • 3
  • on the problem machine, can you put SysInternals processexplorer on and when the user has the problem, check if you really can see the Mutex? – Paul Farry Feb 28 '13 at 07:50

1 Answers1

2

You will need to add Rights to your event otherwise the other application cannot access it, even though it's global.

Private Shared Function CreateEvent(name As String) As _ 
                                              System.Threading.EventWaitHandle
    Dim created As Boolean
    Dim users As New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing)
    Dim rule As New EventWaitHandleAccessRule(users, _ 
                                              EventWaitHandleRights.FullControl, _ 
                                              AccessControlType.Allow)

    Dim security As New EventWaitHandleSecurity()
    security.AddAccessRule(rule)

    Try
        Dim theHandle As New System.Threading.EventWaitHandle(False, _ 
                            Threading.EventResetMode.AutoReset, name, _ 
                            created, security)

        If created Then
            Return theHandle
        Else

        End If
    Catch ex As UnauthorizedAccessException
        'there was another instance created in this very small window. 
        'therefore just attach to the existing.
    Catch ex As Exception
        'do something with this.
    End Try
    Return Nothing
End Function

In your user code are you using System.Threading.Mutex.OpenExisting rather than attempting to Create a new one.

The only other thing I can think of is that the Global Object you are seeing because that name is fairly generic is that it's not the object that you created?

I just did some tests when I create Mutex and an Event with same name.

System.Threading.WaitHandleCannotBeOpenedException = {"A WaitHandle with system-wide name 'Global\AE66918ED73040bbB59F2BE9405FDCA2-5501' cannot be created. A WaitHandle of a different type might have the same name."}

I guess that's not the issue, maybe your service is getting this exception quietly??

Paul Farry
  • 4,730
  • 2
  • 35
  • 61