[英]How to limit macOS process to a single instance in plain C++?
我正在使用以下 Swift 代码将 macOS 上的 GUI 应用程序限制为单个实例:
func IsAnotherInstanceRunning() -> Bool
{
let thisApp = NSRunningApplication.current
let thisBundleID = thisApp.bundleIdentifier
let thisPID = thisApp.processIdentifier
let workspace = NSWorkspace.shared
let apps = workspace.runningApplications.filter { (app) -> Bool in
if(app.bundleIdentifier == thisBundleID &&
app.processIdentifier != thisPID)
{
return true;
}
return false;
};
//Found any?
if(apps.count > 0)
{
return true
}
return false
}
我还有一个用 C++ 编写的控制台应用程序。 我怎样才能在纯 C++ 中做同样的事情?
主要是如何调用与NSRunningApplication
和NSWorkspace
相关的任何内容?
我相信您可以使用命名互斥锁来实现您想要的。 您创建一个具有特定名称的互斥体。 如果 mutex 已经存在,则退出。
https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutexattr_getpshared.html
对于跨平台实现,查看https://theboostcpplibraries.com/boost.interprocess-synchronization named_mutex 是有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.