import psutil
import sys
import os
import traceback
import win32con as wcon
import win32api as wapi
import win32gui as wgui
import win32process as wproc
process_info_list = None
# Callback
def enum_windows_proc(hwnd, param):
pid = param.get("pid", None)
data = param.get("data", None)
if pid is None or wproc.GetWindowThreadProcessId(hwnd)[1] == pid:
text = wgui.GetWindowText(hwnd)
if text:
## 여기에 어떻게 조건을 주느냐에 따라서 active된 process 또는 visable 속성인 window등만 골라낼 수 있음
# style = wapi.GetWindowLong(wnd, wcon.GWL_STYLE)
# if style & wcon.WS_VISIBLE:
if data is not None:
data.append((hwnd, text))
#else:
#print("%08X - %s" % (wnd, text))
def enum_process_windows(pid=None):
data = []
param = {
"pid": pid,
"data": data
}
wgui.EnumWindows(enum_windows_proc, param)
return data
def get_list():
global process_info_list
temp_obj = None
pslist = list(psutil.process_iter())
process_info_list = []
for ps in pslist:
temp_obj = dict()
temp_obj['pid'] = ps.pid
temp_obj['exec_name'] = ps.name()
data = enum_process_windows(ps.pid)
if data:
temp_obj['data'] = data
# proc_text = "PId {0:d}{1:s}windows:".format(ps.pid, " (File: [{0:s}]) ".format(ps.name()) if ps.name() else " ")
# print(proc_text)
# for handle, text in data:
# print(" {0:d}: [{1:s}]".format(handle, text))
# print(temp_obj)
process_info_list.append(temp_obj)
# print(process_info_list)
# for pr in process_info_list:
# print(pr)
def main(*args):
global process_info_list
filtered_list = list()
# proc_name = args[0] if args else None
# proc_name = 'excel' # 이건 process name 테스트용
proc_name = '통합 문서' # 이건 window title 테스트용
get_list()
if len(process_info_list) > 0:
for pr in process_info_list:
is_correct = False
data = None
if 'data' in pr:
data = pr['data']
else:
data = list()
##############################
# 지금은 close 버튼 클릭한 것과 동일한 내용만 표시함
# 차후에 popup 창 찾아서 &Yes 또는 &Ok등 버튼 찾아 클릭하는 내용까지 추가하면 완전히 종료가능
##############################
if proc_name in pr['exec_name'].lower():
print('exec find - {0}'.format(proc_name))
for hwnd , text in data:
wgui.SendMessage(hwnd,wcon.WM_CLOSE,0,0)
else:
for hwnd , text in data:
if proc_name in text:
print('window title find - {0}'.format(proc_name))
wgui.SendMessage(hwnd,wcon.WM_CLOSE,0,0)
if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main(*sys.argv[1:])
print("\nDone.")