检测实例是否正在使用 kernel32::CreateMutexA 运行
我正在开发 NSIS 安装程序,并尝试在卸载之前检查某个应用程序是否正在运行。因此,我使用 kernel32::CreateMutexA
调用。这是块:
System::Call 'kernel32::CreateMutexA(i 0, i 0, t "cmd.exe") i .r1 ?e'
Pop $R0
StrCmp $R0 0 +3
MessageBox MB_USERICON "The application is already running."
Abort
我将其放入 un.onInit
中。问题是,该进程(此处为cmd.exe
)从未被检测到。
我错过了什么吗?
德克萨斯州。
I'm working on an NSIS installer, and trying to check if a certain application is running before uninstalling. So, I use kernel32::CreateMutexA
call. Here is the chunk:
System::Call 'kernel32::CreateMutexA(i 0, i 0, t "cmd.exe") i .r1 ?e'
Pop $R0
StrCmp $R0 0 +3
MessageBox MB_USERICON "The application is already running."
Abort
I put it into un.onInit
. Trouble is, the process (cmd.exe
here) is never detected.
Did I miss something?
Tx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了一个简单的解决方案;使用 FindProcDLL 插件。
因此:
PS
FindProcDLL.dll
必须复制到/Plugins
中。I found a simple solution; using FindProcDLL plugin.
So:
P.S.
FindProcDLL.dll
must be copied into/Plugins
.您所做的就是创建一个全局名称为“cmd.exe”的互斥锁。来自 MSDN 文章
创建互斥体
:因此,除非
cmd.exe
创建一个名为"cmd.exe"
的对象类型之一的句柄,否则此调用只会创建一个具有该名称的新互斥体,并且返回给您(无错误的)句柄。All you are doing is creating a mutex with the global name
"cmd.exe"
. From the MSDN article forCreateMutex
:So unless
cmd.exe
creates a handle to one of those types of objects with the name"cmd.exe"
, this call will simply create a new mutex with that name and return you the (non-erronous) handle.您可能使用了错误的 Win32 API 函数。您的 CreateMutex 尝试创建一个名为“something.exe”的互斥体。如果没有具有该名称的互斥体,它将成功,因此除非您尝试检查的进程正在创建具有该名称的互斥体,否则您将不会得到您想要的结果。
您想要的可能是枚举所有正在运行的进程,并查看您要查找的进程是否存在。您可以使用 Win32 API 中的 ToolHelp32 来执行此操作 - 请参阅此处的示例。我不知道将其转换为“纯”NSIS 有多容易,因此您可能需要编写一个 DLL 插件,或者检查 NSIS 社区是否有现有的解决方案。
You're probably using the wrong Win32 API function. Your CreateMutex tries to create a named mutex "something.exe". If there isn't one with that name it will succeed, so unless the process you're trying to check is creating a mutex with that name, you won't get the result you're after.
What you want is probably to enumerate all running processes and see if the one you're after is there. You can do this with ToolHelp32 from Win32 API - see sample here. I don't know how easy it will be to convert it to 'pure' NSIS so you might want to write a DLL plugin, or check if there's an existing solution floating around the NSIS community.
对于这种事情,我使用了 NSIS 的 KillProcess 或 Find-Close-Terminate 插件 - 请参阅 这里
文档非常简单,希望这能满足您的需求 - 并且开销相当小。
For this kind of thing, I've used either the KillProcess or Find-Close-Terminate plugins for NSIS - see here
Documentation is pretty straightforward, hopefully this does what you need - with a fairly minimal overhead.