Guest User

NeeViewClient.py

a guest
Nov 5th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | Source Code | 0 0
  1. # py -3.14 -m pip install psutil pywin32
  2. # pyw -3.14 NeeViewClient.py path_to_neeview_exe path_to_image_file (...)
  3.  
  4. import sys, json, subprocess, psutil
  5. import win32pipe, win32file, pywintypes
  6. import ctypes
  7. from ctypes import windll
  8.  
  9. def find_process_by_name(process_name):
  10.   for proc in psutil.process_iter(['pid', 'name', 'exe']):
  11.     if proc.info['name'] == process_name:
  12.       return proc.info
  13.   return None
  14.  
  15. Win32 = windll.user32
  16. EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
  17.  
  18. def activate_window_by_pid(pid):
  19.   def activate_window(hwnd):
  20.     target_pid = ctypes.c_ulong()
  21.     Win32.GetWindowThreadProcessId(hwnd, ctypes.byref(target_pid))
  22.     if pid == target_pid.value:
  23.       if Win32.IsIconic(hwnd):
  24.         Win32.ShowWindow(hwnd, 9) # SW_RESTORE == 9
  25.       Win32.SetForegroundWindow(hwnd)
  26.       Win32.BringWindowToTop(hwnd)
  27.       return True
  28.     else:
  29.       return False
  30.   def each_window(hwnd, _):
  31.     if activate_window(hwnd):
  32.       return 0
  33.     return 1
  34.   proc = EnumWindowsProc(each_window)
  35.   Win32.EnumWindows(proc, 0)
  36.  
  37. def launch_neeview(neeview_exe, files):
  38.     argv = [neeview_exe]
  39.     argv.extend(files)
  40.     process = subprocess.Popen(argv, creationflags= subprocess.DETACHED_PROCESS)
  41.     # process = subprocess.Popen(argv, creationflags= subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP)
  42.  
  43. def main():
  44.   if len(sys.argv)<=2:
  45.     return
  46.   neeview_exe = sys.argv[1]
  47.   files = sys.argv[2:]
  48.   found_process = find_process_by_name("NeeView.exe")
  49.   if found_process is None:
  50.     launch_neeview(neeview_exe, files)
  51.     return
  52.   pid = found_process['pid']
  53.   try:
  54.     pipeName = f"\\\\.\\pipe\\NeeView.p{pid}"
  55.     handle = win32file.CreateFile(pipeName, win32file.GENERIC_WRITE, 0, None, win32file.OPEN_EXISTING, 0, None)
  56.     if handle == win32file.INVALID_HANDLE_VALUE:
  57.       launch_neeview(neeview_exe, files)
  58.       return
  59.  
  60.     filesjson = json.dumps(files)
  61.     message = f'{{"Id":"LoadAs","Args":{filesjson}}}'
  62.     win32file.WriteFile(handle, message.encode('utf-8'))
  63.     win32file.CloseHandle(handle)
  64.  
  65.     activate_window_by_pid(pid)
  66.   except pywintypes.error as e:
  67.     Win32.MessageBoxW(None, f'Error: {e}', 'NeeViewClient.py', 0x00000030) # MB_ICONWARNING == 0x00000030L
  68.  
  69. if __name__ == '__main__':
  70.   main()
  71.  
Advertisement
Add Comment
Please, Sign In to add comment