隐藏

c#杀死一个进程的某些相关线程的安全例子

发布:2014/6/24 17:18:51作者:管理员 来源:本站 浏览次数:1207

下面给出杀死一个进程的某些相关线程的安全例子:
Process[] myProcesses = Process.GetProcessesByName("IEXPLORE");

foreach(Process myProcess in myProcesses)
{
    FindPopupToKill(myProcess);
}

protected void FindPopupToKill(Process p)
{
    // traverse all threads and enum all windows attached to the thread
    foreach (ProcessThread t in p.Threads)
    {
        int threadId = t.Id;
 
        NativeWIN32.EnumThreadProc callbackProc = 
            new NativeWIN32.EnumThreadProc(MyEnumThreadWindowsProc);
        NativeWIN32.EnumThreadWindows(threadId, 
            callbackProc, IntPtr.Zero /*lParam*/);
    }
}

// callback used to enumerate Windows attached to one of the threads
bool MyEnumThreadWindowsProc(IntPtr hwnd, IntPtr lParam)
{
    public const int WM_SYSCOMMAND = 0x0112;
    public const int SC_CLOSE = 0xF060;


    // get window caption
    NativeWIN32.STRINGBUFFER sLimitedLengthWindowTitle;
    NativeWIN32.GetWindowText(hwnd, out sLimitedLengthWindowTitle, 256);

    String sWindowTitle = sLimitedLengthWindowTitle.szText;
    if (sWindowTitle.Length==0) return true;

    // find this caption in the list of banned captions
    foreach (ListViewItem item in listView1.Items)
    {
        if ( sWindowTitle.StartsWith(item.Text) )
            NativeWIN32.SendMessage(hwnd, NativeWIN32.WM_SYSCOMMAND,
                                          NativeWIN32.SC_CLOSE, 
                                          IntPtr.Zero);  // try soft kill
    }

    return true;
}

public class NativeWIN32
{
    public delegate bool EnumThreadProc(IntPtr hwnd, IntPtr lParam);

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern bool EnumThreadWindows(int threadId, 
        EnumThreadProc pfnEnum, IntPtr lParam);

    // used for an output LPCTSTR parameter on a method call
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    public struct STRINGBUFFER
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
        public string szText;
    }

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern int GetWindowText(IntPtr hWnd,  
        out STRINGBUFFER ClassName, int nMaxCount);

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern int SendMessage(IntPtr hWnd, 
        int msg, int wParam, int lParam);
}

protected void FindPopupToKill()
{
    IntPtr hParent = IntPtr.Zero;
    IntPtr hNext = IntPtr.Zero;
    String sClassNameFilter = "IEFrame"; // CLASSNAME of all IE windows

    do
    {
        hNext = NativeWIN32.FindWindowEx(hParent,hNext,
            sClassNameFilter,IntPtr.Zero);

        // we've got a hwnd to play with
        if ( !hNext.Equals(IntPtr.Zero) )
        {
            // get window caption
            NativeWIN32.STRINGBUFFER sLimitedLengthWindowTitle;
            NativeWIN32.GetWindowText(hNext, out 
                sLimitedLengthWindowTitle, 256);

            String sWindowTitle = sLimitedLengthWindowTitle.szText;
            if (sWindowTitle.Length>0)
            {
                // find this caption in the list of banned captions
                foreach (ListViewItem item in listView1.Items)
                {
                    if ( sWindowTitle.StartsWith(item.Text) )
                        NativeWIN32.SendMessage(hNext, 
                            NativeWIN32.WM_SYSCOMMAND,
                            NativeWIN32.SC_CLOSE,
                            IntPtr.Zero); // try soft kill
                }
            }
        }
    } 
    while (!hNext.Equals(IntPtr.Zero));

}

public class NativeWIN32
{
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern IntPtr FindWindowEx(IntPtr parent /*HWND*/, 
                                             IntPtr next /*HWND*/, 
                                             string sClassName,  
                                             IntPtr sWindowTitle);