독도 광고 모금 캠페인

process_find_and_close.py
0.00MB

모든 출처의 글이 사용된것은 아니나 구현 하려고 했던 것의 영감을 줌
process name 과 window title 모두 찾은 이유는 사실 window title로 찾아지지 않는 뭔가를 편하게 끄려고 한 것임

더보기

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.")

 

 

출처 : 

https://stackoverflow.com/questions/31278590/get-the-title-of-a-window-of-another-program-using-the-process-name

 

Get the title of a window of another program using the process name

This question is probably quite basic but I'm having difficulty cracking it. I assume that I will have to use something in ctypes.windll.user32. Bear in mind that I have little to no experience using

stackoverflow.com

http://timgolden.me.uk/python/win32_how_do_i/find-the-window-for-my-subprocess.html

 

Tim Golden's Python Stuff: Find the window for my subprocess

Find the window for my subprocess Tim Golden > Python Stuff > Win32 How Do I...? > Find the window for my subprocess Introduction The challenge: to find the windows belonging to the process you've just kicked off. The idea is that if, for example, you run

timgolden.me.uk

https://stackoverflow.com/questions/51418928/find-all-window-handles-from-a-subprocess-popen-pid

 

Find all window handles from a subprocess.Popen pid

I'm currently trying to get all window handles from a process started by subprocess.Popen looking like this: def openMP(): mp_p = os.path.abspath("C:\Program Files (x86)\Mobile Partner\Mobile

stackoverflow.com

https://stackoverflow.com/questions/17272572/get-window-handler-from-started-process

 

get window handler from started process

I see there's win32process.GetWindowThreadProcess() that gets a window handler and returns it's process id. Is there a way to do the opposite: get the window handler of a running process by it's pr...

stackoverflow.com

 

Posted by 이현호
,