我有一個應用程序需要在Windows PC上的所有用戶會話中成爲單實例應用程序。到目前爲止,我的研究主要集中在使用互斥體來完成這個任務,但是我有一個問題,我不確定是否真的是一個問題,這是我相信的一個最佳實踐問題。VB6:跨所有用戶會話的單實例應用程序
這裏第一是所有的代碼的:
Private Const AppVer = "Global\UNIQUENAME" ' This is not what i am using but the name is unique
Public Sub Main()
Dim mutexValue As Long
mutexValue = CreateMutex(ByVal 0&, 1, AppVer)
If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
SaveTitle$ = App.Title
App.Title = "... duplicate instance."
MsgBox "A duplicate instance of this program exists."
CloseHandle mutexValue
Exit Sub
End If
' Else keep on truckin'
現在,基於this文章中,我相信我的理解是由NULL指針傳遞給CreateMutex功能,因爲我上面我基本上指派任何安全描述符與當前登錄的用戶相關聯。
如果這意味着我認爲它的確如此(我可能需要更多的指導),這告訴我其他登錄用戶將無法「看到」在原用戶會話下創建的互斥體,也不會能夠創建一個具有相同名稱的互斥體。
現在,經驗證據似乎支持了這一點。我使用了一個消息框來彈出我正在接收的「LastDLLError」,並且當另一個用戶試圖啓動應用程序時(它已經在另一個用戶帳戶下運行),我將收到一個ERROR_ACCESS_DENIED代碼。我可以與ERROR_ALREADY_EXISTS代碼一起對此進行測試,然後在/或上退出。然而,這感覺有點駭人聽聞,我想知道如果有人可以提出一個替代方案。要做的「正確」事情似乎是將適當的指針傳遞給CreateMutex函數,以便任何用戶都有適當的權限來查看任何現有的互斥體(mutices?),但是我不確定如果沒有當前的互斥體登錄的用戶是管理員(這是不可感知的)。任何援助/指導非常感謝。提前致謝!
感謝大家的反饋。我將修改我的代碼,使其像Joe的代碼中的代碼一樣,隨附的測試應用程序證實我應該能夠使其工作。結束這一個。 – 2011-01-26 22:19:33