VB6:所有用户会话中的单实例应用程序 [英] VB6: Single-instance application across all user sessions

查看:100
本文介绍了VB6:所有用户会话中的单实例应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序必须是Windows PC上所有用户会话中的单实例应用程序.到目前为止,我的研究集中在使用互斥锁来完成此操作,但是我有一个不确定的问题,我认为这确实是最佳实践的问题.

I have an application that needs to be a single-instance app across all user sessions on a Windows PC. My research thus far has centered around using a mutex to accomplish this, but I am having an issue that I am not sure is really an issue, this is really a best-practice question I believe.

首先是代码:

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'

现在,基于文章,我相信我已经了解到,通过像上面那样将NULL指针传递给CreateMutex函数,我基本上是在分配与当前登录用户相关联的任何安全描述符.

Now, based on this article I believe I understand that by passing the NULL pointer to the CreateMutex function as I am above I'm basically assigning whatever security descriptor is associated with the currently logged in user.

如果这意味着我认为这样做(我可能在这里需要更多指导)告诉我其他登录的用户将无法查看"原始用户会话下创建的互斥体,也无法创建具有相同名称的互斥锁.

If that means what I think it does (I may need more guidance here) that tells me that other users who log in will not be able to "see" the mutex created under the original user's session, nor will they be able to create a mutex with the same name.

现在,经验证据似乎证明了这一点.我使用一个消息框弹出我收到的"LastDLLError",当另一个用户尝试启动该应用程序时(该应用程序已经在另一个用户帐户下运行),我将收到ERROR_ACCESS_DENIED代码.我可以将其与ERROR_ALREADY_EXISTS代码一起对此进行测试,并且可以在"/"或"/"上退出.但是,这感觉有点骇人听闻,我想知道是否有人可以建议其他方法. 正确"的做法似乎是将正确的指针传递给CreateMutex函数,以便任何用户都具有查看任何现有互斥对象(互斥对象)的适当权限,但是我不确定如果没有当前的互斥对象,是否有可能登录用户是管理员(这是不可接受的).任何帮助/指导,我们将不胜感激.预先感谢!

Now, emperical evidence seems to back this up. I used a message box to pop the "LastDLLError" I was receiving, and when another user attempted to launch the application (while it was already running under another user account) I would receive an ERROR_ACCESS_DENIED code. I am OK with testing against this along with the ERROR_ALREADY_EXISTS code and just exiting on either/or. However, this feels sort of hackish and I'm wondering if someone can suggest an alternative. The "right" thing to do seems to be to pass the proper pointer to the CreateMutex function such that any user has the proper permissions to view any existing mutexes (mutices?), but I'm not so sure this is possible without the currently logged in user being an admin (which is unacceptible). Any assistance/guidance is greatly appreciated. Thanks in advance!

推荐答案

去年年底,我一直在VB6中寻找类似的解决方案.当时我找不到跨用户边界进行通信的VB6应用程序的任何示例,因此我不得不编写自己的示例.

I was looking for a similar solution in VB6 late last year. At the time I was unable to find any examples of VB6 apps communicating across the user boundary, so I had to write my own.

请参阅:通过信号量进行进程间通信

您可以使用该类来创建并检查全局信号量,该信号量将告诉您您的应用程序是否已经在任何用户下运行.我没有看过Mutex API,但是它们的用法非常相似. GetSecurityDescriptor 函数是您已经编写了一些Mutex代码后想要转置的函数.

You can use the class to create and check for a global semaphore which will tell you if your app is already running under any user. I didn't look at the Mutex APIs but their usage is very similar. The GetSecurityDescriptor function is what you'll want to transpose if you've already got some Mutex code written.

这篇关于VB6:所有用户会话中的单实例应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆