Created
November 21, 2012 04:03
-
-
Save nobonobo/4122938 to your computer and use it in GitHub Desktop.
多重起動禁止処理 for Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import win32api | |
import win32security | |
import win32event | |
import win32gui | |
sa = win32security.SECURITY_ATTRIBUTES() | |
sa.SECURITY_DESCRIPTOR.SetSecurityDescriptorDacl(True, None, False) | |
# saを指定しておかないと別のユーザで起動したときにMutexが衝突しなくなる | |
mutex = win32event.CreateMutex(sa, False, 'unique-string...') | |
err = win32api.GetLastError() | |
# 既存のアプリが存在するとエラーになる。 | |
if err != 0: | |
# print win32api.FormatMessageW(err) | |
id = win32gui.FindWindow('QWidget', 'Title') # for QMainWindow | |
if id: | |
# 既存のウインドウをフォアグラウンドに持ってくる。 | |
win32gui.SetForegroundWindow(id) | |
win32gui.BringWindowToTop(id) | |
sys.exit(-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment