UnauthorizedAccessException on Openexisting global mutex

烈酒焚心 提交于 2019-12-11 18:20:08

问题


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. Thanks for helping

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);


回答1:


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??



来源:https://stackoverflow.com/questions/15129643/unauthorizedaccessexception-on-openexisting-global-mutex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!