gpt4 book ai didi

delphi - 每台计算机一个应用程序实例,怎么样?

转载 作者:行者123 更新时间:2023-12-03 14:57:30 27 4
gpt4 key购买 nike

我试图让我的应用程序在计算机上仅运行一次,我的应用程序需要与网络服务通信,因此让它运行多次是不好的,目前我使用互斥体:

MyMsg := RegisterWindowMessage('My_Unique_App_Message_Name');
Mutex := CreateMutex(nil, True, 'My_Unique_Application_Mutex_Name');
if (Mutex = 0) OR (GetLastError = ERROR_ALREADY_EXISTS) then
exit;

目前,这可以限制每个用户 1 个应用程序实例,但我的应用程序正在 Windows Server 环境中使用,其中一次有 20 多个用户登录,因此我需要将其严格限制为每个服务器仅运行一次,我想做的是将互斥体声明为全局互斥体,但当我执行下一个代码时我没有这样做,它根本不起作用。

 MyMsg := RegisterWindowMessage('My_Unique_App_Message_Name');
Mutex := CreateMutex(nil, True, 'Global\My_Unique_Application_Mutex_Name');
if (Mutex = 0) OR (GetLastError = ERROR_ALREADY_EXISTS) then
begin
exit

所以我做错了什么吗?有没有其他可靠的方法来不让我的应用程序的第二个实例运行?

最佳答案

默认情况下,lpMutexAttributes = nil 创建的互斥体只能由运行该进程的用户访问。互斥体需要可供所有用户访问。

空 DACL 安全描述符

通过使用具有空 DACL 的安全描述符来执行此操作。请在下面找到我用于单实例应用程序/服务的代码

//Creates a mutex to see if the program is already running.
function IsSingleInstance(MutexName : string; KeepMutex : boolean = true):boolean;
const MUTEX_GLOBAL = 'Global\'; //Prefix to explicitly create the object in the global or session namespace. I.e. both client app (local user) and service (system account)

var MutexHandel : THandle;
SecurityDesc: TSecurityDescriptor;
SecurityAttr: TSecurityAttributes;
ErrCode : integer;
begin
// By default (lpMutexAttributes =nil) created mutexes are accessible only by
// the user running the process. We need our mutexes to be accessible to all
// users, so that the mutex detection can work across user sessions.
// I.e. both the current user account and the System (Service) account.
// To do this we use a security descriptor with a null DACL.
InitializeSecurityDescriptor(@SecurityDesc, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@SecurityDesc, True, nil, False);
SecurityAttr.nLength:=SizeOf(SecurityAttr);
SecurityAttr.lpSecurityDescriptor:=@SecurityDesc;
SecurityAttr.bInheritHandle:=False;

// The mutex is created in the global name space which makes it possible to
// access across user sessions.
MutexHandel := CreateMutex(@SecurityAttr, True, PChar(MUTEX_GLOBAL + MutexName));
ErrCode := GetLastError;

// If the function fails, the return value is 0
// If the mutex is a named mutex and the object existed before this function
// call, the return value is a handle to the existing object, GetLastError
// returns ERROR_ALREADY_EXISTS.
if {(MutexHandel = 0) or }(ErrCode = ERROR_ALREADY_EXISTS) then
begin
result := false;
closeHandle(MutexHandel);
end
else
begin
// Mutex object has not yet been created, meaning that no previous
// instance has been created.
result := true;

if not KeepMutex then
CloseHandle(MutexHandel);
end;

// The Mutexhandle is not closed because we want it to exist during the
// lifetime of the application. The system closes the handle automatically
//when the process terminates.
end;

关于delphi - 每台计算机一个应用程序实例,怎么样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20669917/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com