Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Created November 21, 2012 04:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobonobo/4122938 to your computer and use it in GitHub Desktop.
Save nobonobo/4122938 to your computer and use it in GitHub Desktop.
多重起動禁止処理 for Windows
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