【问题标题】:UnauthorizedAccessException on Openexisting global mutexOpenexisting 全局互斥体上的 UnauthorizedAccessException
【发布时间】:2013-02-28 16:24:39
【问题描述】:

我正在使用互斥锁来同步两个进程。 互斥锁是由服务进程创建的。 但是当客户端尝试访问该互斥体时,他会得到 Unauthorizedaccessexception。 用户帐户具有创建全局对象权限 这发生在少数运行 Windows 7 的机器上,但在其他 Windows 7 机器上无法重现。 可能是什么原因。 感谢您的帮助

以下是创建全局互斥锁的代码 布尔 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);

【问题讨论】:

  • 在问题机器上,你能不能把SysInternals processexplorer打开,当用户有问题时,检查你是否真的能看到Mutex?

标签: .net windows-7


【解决方案1】:

您需要为您的事件添加权限,否则即使它是全局的,其他应用程序也无法访问它。

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

在您的用户代码中,您使用的是 System.Threading.Mutex.OpenExisting 而不是尝试创建一个新的。

我唯一能想到的另一件事是,您看到的全局对象是因为该名称相当通用,所以它不是您创建的对象?

我刚刚在创建 Mutex 和同名事件时做了一些测试。

System.Threading.WaitHandleCannotBeOpenedException = {"无法创建系统范围名称为 'Global\AE66918ED73040bbB59F2BE9405FDCA2-5501' 的 WaitHandle。不同类型的 WaitHandle 可能具有相同的名称。"}

我想这不是问题,也许您的服务正在悄悄地得到这个异常??

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多