1

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

以下是创建全局互斥体 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);

4

1 回答 1

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 和同名的 Event 时,我只是做了一些测试。

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

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

于 2013-02-28T07:16:42.967 回答