当wm下GetProcesses不好用了,要杀特定的进程就只能通过窗体的标题了:


        
public bool KillProcessByWindowsTitle(string title)
        {
            IntPtr hWnd 
= SafeNativeMethods.FindWindow(null, title);
            
if (hWnd != IntPtr.Zero)
            {
                
int processId = 0;
                SafeNativeMethods.GetWindowThreadProcessId(hWnd, 
ref processId);
                IntPtr hProcess 
= IntPtr.Zero;
                hProcess 
= SafeNativeMethods.OpenProcess(SafeNativeMethods.PROCESS_ALL_ACCESS, 0, processId);
                
if (hProcess != IntPtr.Zero)
                {
                    
if (SafeNativeMethods.TerminateProcess(hProcess, 1))
                    {
                        SafeNativeMethods.CloseHandle(hProcess);
                        
return true;
                    }
                    
else
                    {
                        SafeNativeMethods.CloseHandle(hProcess);
                        
return false;
                    }
                }
                
else
                {
                    
return false;
                }
            }
            
return false;
        }



        
internal sealed class SafeNativeMethods
        {
            [DllImport(
"coredll.dll")]
            
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

            
public const int PROCESS_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFF;
            [DllImport(
"coredll.dll")]
            
public extern static int GetWindowThreadProcessId(IntPtr hWnd, ref int lpdwProcessId);
            [DllImport(
"coredll.dll")]
            
public extern static IntPtr OpenProcess(int fdwAccess, int fInherit, int IDProcess);
            [DllImport(
"coredll.dll")]
            
public extern static bool TerminateProcess(IntPtr hProcess, int uExitCode);
            [DllImport(
"coredll.dll")]
            
public extern static bool CloseHandle(IntPtr hObject);
        }

相关文章:

  • 2022-02-05
  • 2021-11-30
  • 2021-12-31
  • 2021-10-21
  • 2021-07-15
  • 2022-01-03
猜你喜欢
  • 2022-01-31
  • 2021-11-26
  • 2021-12-04
  • 2022-01-03
  • 2021-08-07
相关资源
相似解决方案