2013-02-28 66 views
0

我正在使用互斥鎖來同步兩個進程。 互斥體正在通過服務進程創建。 但是,當客戶端嘗試訪問該互斥鎖時,他將獲得Unauthorizedaccessexception。 用戶帳戶已創建全局對象權限 這發生在少數運行Windows 7的機器上,但在其他Windows 7機器上無法重現。 可能是什麼原因。 感謝您的幫助UnauthorizedAccessException打開全局互斥鎖

以下是創建全局互斥鎖的代碼 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);

+0

問題機器上,你可以把SysInternals processexplorer和當用戶有問題,檢查你是否真的可以看到互斥? – 2013-02-28 07:50:14

回答

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而不是試圖創建一個新的。

我能想到的唯一的其他事情就是您看到的全局對象,因爲該名稱相當通用,因爲它不是您創建的對象?

我剛做了一些測試,當我創建互斥和一個具有相同名稱的事件。

System.Threading.WaitHandleCannotBeOpenedException = { 「A WaitHandle的全系統的名稱的 '全球\ AE66918ED73040bbB59F2BE9405FDCA2-5501' 不能被創建。不同類型的WaitHandle的可能具有相同的名稱。」}

我想這不是問題,也許你的服務靜靜地得到這個異常?