C#操作句柄

1、直接上例子吧:收集系统信息msinfo32时,会有一个弹窗,现在要隐藏该弹窗,首先看没有通过句柄隐藏弹窗的现象

2、收集系统信息导入到一个位置

代码:

Process[] msinfo32process;//创建一个PROCESS类数组
msinfo32process = Process.GetProcesses();//获取当前任务管理器所有运行中程序
foreach (Process proces in msinfo32process)//遍历若存在msinfo21.exe则杀掉
{
     if (proces.ProcessName == "msinfo32.exe")
     {
            proces.Kill();
     }
}
//通过调用CMD命令进行系统信息导出为一个文件 Common.CmdExcute("msinfo32 /nfo C:\tmp\msinfo32.nfo");

C#操作句柄

3、下面通过操作句柄进行隐藏收集信息框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process[] msinfo32process;//创建一个PROCESS类数组
            msinfo32process = Process.GetProcesses();//获取当前任务管理器所有运行中程序
            foreach (Process proces in msinfo32process)//遍历
            {
                if (proces.ProcessName == "msinfo32.exe")
                {
                    proces.Kill();
                }
            }
            Thread msinfo32 = new Thread(msinfo);
            msinfo32.Start();
        }
        private static void msinfo()
        {
            Common.CmdExcute("msinfo32 /nfo C:\vDesk\msinfo32.nfo");
        }

        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32")]
        private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childAfter, string className, string windowName);

        [DllImport("user32.dll")]

        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        private static void whatr()
        {
            bool stop = true;

            while (stop)
            {
                //Sometimes System.Windows.Forms.Application.ExecutablePath works for the caption depending on the system you are running under.
                IntPtr hWnd = FindWindow(null, "系统信息"); //put your console window caption here
                IntPtr hWnd_en = FindWindow(null, "System Information");
                if (hWnd != IntPtr.Zero)
                {
                    //Hide the window
                    IntPtr child = FindWindowEx(hWnd, IntPtr.Zero, "#32770", "系统信息");

                    ShowWindow(hWnd, 0); // 0 = SW_HIDE

                    if (child != IntPtr.Zero)
                    {
                        ShowWindow(child, 0); // 0 = SW_HIDE
                        //Notice:设置之后退出线程
                        stop = false;
                    }
                }
                else if (hWnd_en != IntPtr.Zero)
                {
                    IntPtr child_en = FindWindowEx(hWnd_en, IntPtr.Zero, "#32770", "System Information");
                    ShowWindow(hWnd_en, 0); // 0 = SW_HIDE
                    if (child_en != IntPtr.Zero)
                    {
                        ShowWindow(child_en, 0); // 0 = SW_HIDE
                        //Notice:设置之后退出线程
                        stop = false;
                    }
                }
                Thread.Sleep(50);
            }
        }
    }
}

这样就可以获得句柄进行隐藏收集系统信息的弹窗