冰楓論壇

標題: [C#] 透過行程名稱/行程ID操作程式程式 [打印本頁]

作者: whitefox    時間: 2023-6-2 21:00
標題: [C#] 透過行程名稱/行程ID操作程式程式
判斷程式是否執行
  1. public bool IsWindowExist(IntPtr handle)
  2. {
  3.     return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
  4. }

  5. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  6. public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);

  7. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  8. public static extern bool IsWindowVisible(HandleRef hWnd);
複製代碼
根據處理緒Handle查找程式
  1. public IntPtr GetWindowHandle(int processId)
  2. {
  3.     var windowHandle = IntPtr.Zero;
  4.     EnumThreadWindowsCallback windowsCallback = new EnumThreadWindowsCallback(FindMainWindow);
  5.     EnumWindows(windowsCallback, IntPtr.Zero);

  6.     GC.KeepAlive(windowsCallback);

  7.     bool FindMainWindow(IntPtr handle, IntPtr extraParameter)
  8.     {
  9.         int num;
  10.         GetWindowThreadProcessId(new HandleRef(this, handle), out num);
  11.         if (num == processId && IsWindowExist(handle))
  12.         {
  13.             windowHandle = handle;
  14.             return true;
  15.         }
  16.         return false;
  17.     }

  18.     return windowHandle;
  19. }

  20. public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);

  21. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  22. public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);

  23. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  24. public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
複製代碼
根據行程ID關閉程式
  1. public void CloseWindow(int processId)
  2. {
  3.     var mwh = GetWindowHandle(processId);
  4.     SendMessage(mwh, MW_CLOSE, 0, 0);
  5. }
  6. const int MW_CLOSE = 0x0010;

  7. [DllImport("User32.dll", EntryPoint = "SendMessage")]
  8. public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
複製代碼
關閉所有此行程名稱的程式
  1. public void CloseAllWindows(string processName)
  2. {
  3.     Process[] processList = Process.GetProcessesByName(processName);
  4.     foreach (Process process in processList)
  5.     {
  6.         CloseMainWindow(process.Id);
  7.     }
  8. }
複製代碼
直接關閉所有行程
  1. public static bool CloseAllProcesses(string processName)
  2. {
  3.     try
  4.     {
  5.         Process[] psEaiNotes = Process.GetProcessesByName(processName);
  6.         foreach (Process psEaiNote in psEaiNotes)
  7.         {
  8.             psEaiNote.Kill();
  9.         }
  10.     }
  11.     catch
  12.     {
  13.         return false;
  14.     }
  15.     return true;
  16. }
複製代碼
重新啟動程序
  1. string appFileName = currentClientProcess.MainModule.FileName;

  2. Process newClient = new Process();
  3. newClient.StartInfo.FileName = appFileName;
  4. // 設定執行程序的開始目錄
  5. newClient.StartInfo.WorkingDirectory = System.Windows.Forms.Application.ExecutablePath;

  6. // 執行程序
  7. currentClientProcess.Kill();
  8. newClient.Start();
複製代碼





歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓