Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # py -3.14 -m pip install psutil pywin32
- # pyw -3.14 NeeViewClient.py path_to_neeview_exe path_to_image_file (...)
- import sys, json, subprocess, psutil
- import win32pipe, win32file, pywintypes
- import ctypes
- from ctypes import windll
- def find_process_by_name(process_name):
- for proc in psutil.process_iter(['pid', 'name', 'exe']):
- if proc.info['name'] == process_name:
- return proc.info
- return None
- Win32 = windll.user32
- EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
- def activate_window_by_pid(pid):
- def activate_window(hwnd):
- target_pid = ctypes.c_ulong()
- Win32.GetWindowThreadProcessId(hwnd, ctypes.byref(target_pid))
- if pid == target_pid.value:
- if Win32.IsIconic(hwnd):
- Win32.ShowWindow(hwnd, 9) # SW_RESTORE == 9
- Win32.SetForegroundWindow(hwnd)
- Win32.BringWindowToTop(hwnd)
- return True
- else:
- return False
- def each_window(hwnd, _):
- if activate_window(hwnd):
- return 0
- return 1
- proc = EnumWindowsProc(each_window)
- Win32.EnumWindows(proc, 0)
- def launch_neeview(neeview_exe, files):
- argv = [neeview_exe]
- argv.extend(files)
- process = subprocess.Popen(argv, creationflags= subprocess.DETACHED_PROCESS)
- # process = subprocess.Popen(argv, creationflags= subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP)
- def main():
- if len(sys.argv)<=2:
- return
- neeview_exe = sys.argv[1]
- files = sys.argv[2:]
- found_process = find_process_by_name("NeeView.exe")
- if found_process is None:
- launch_neeview(neeview_exe, files)
- return
- pid = found_process['pid']
- try:
- pipeName = f"\\\\.\\pipe\\NeeView.p{pid}"
- handle = win32file.CreateFile(pipeName, win32file.GENERIC_WRITE, 0, None, win32file.OPEN_EXISTING, 0, None)
- if handle == win32file.INVALID_HANDLE_VALUE:
- launch_neeview(neeview_exe, files)
- return
- filesjson = json.dumps(files)
- message = f'{{"Id":"LoadAs","Args":{filesjson}}}'
- win32file.WriteFile(handle, message.encode('utf-8'))
- win32file.CloseHandle(handle)
- activate_window_by_pid(pid)
- except pywintypes.error as e:
- Win32.MessageBoxW(None, f'Error: {e}', 'NeeViewClient.py', 0x00000030) # MB_ICONWARNING == 0x00000030L
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment