c# read the name and handle of all executable files under the system

To get the handle of the form, you can use the GetWindow method to get the title of the form, use the GetWindowText method, use the ShowWindow method to display and hide a form, and give you an example

using System.Runtime.InteropServices;

         private const int WS_VISIBLE =268435456;//Form Visible
        private const int WS_MINIMIZEBOX= 131072;//There is a minimize button
        private const int WS_MAXIMIZEBOX= 65536;//There is a maximize button
        private const int WS_BORDER =8388608;//The form has a border
        private const int GWL_STYLE =(-16); //Form style
        private const int GW_HWNDFIRST =0;
        private const int GW_HWNDNEXT =2;
        private const int SW_HIDE = 0;
        private const int SW_SHOW = 5;

       [DllImport("User32.dll")]
        private extern static intGetWindow(int hWnd , int wCmd);
       [DllImport("User32.dll")]
        private extern static intGetWindowLongA(int hWnd, int wIndx);
       [DllImport("user32.dll")]
        private static extern boolGetWindowText(int hWnd, StringBuilder title, int maxBufSize);
       [DllImport("user32.dll", CharSet = CharSet. Auto)]
        private extern static intGetWindowTextLength(IntPtr hWnd);
       [DllImport("user32.dll")]
        public static extern intShowWindow(int hwnd, int nCmdShow);

        //Get the window with visible, bordered and maximized buttons The handle and title of the form (there are many kinds of attributes of the form)
        public staticList<FromInfo> GetHandleList(int Handle)
        {
            List<FromInfo> fromInfo = newList<FromInfo>();
            int handle =GetWindow( Handle, GW_HWNDFIRST);
            while (handle > 0)
            {
                int IsTask = WS_VISIBLE |WS_BORDER | WS_MAXIMIZEBOX;//窗体可见、有边框、有最大化按钮
                int lngStyle = GetWindowLongA(handle,GWL_STYLE);
                bool TaskWindow =((lngStyle & IsTask) == IsTask);
                if (TaskWindow)
                {
                    int length =GetWindowTextLength(new IntPtr(handle));
                    StringBuilder stringBuilder = newStringBuilder(2 * length + 1);
                    GetWindowText(handle,stringBuilder, stringBuilder.Capacity);
                    string strTitle =stringBuilder.ToString();
                    if (!string.IsNullOrEmpty(strTitle))
                    {
                        fromInfo.Add(newFromInfo(strTitle, handle));
                    }
                    else
                    {
                        fromInfo.Add(newFromInfo("", handle));
                    }
                }
                handle =GetWindow(handle, GW_HWNDNEXT);
            }
            return fromInfo;
        }
        //获得所有窗体的句柄和标题
        public staticList<FromInfo> GetHandleList(int Handle)
        {
            List<FromInfo> fromInfo= new List<FromInfo>();
            int handle =GetWindow(Handle, GW_HWNDFIRST);
            while (handle > 0)
            {
                int length =GetWindowTextLength(new IntPtr(handle));
                StringBuilder stringBuilder= new StringBuilder(2 * length + 1);
                GetWindowText(handle,stringBuilder, stringBuilder.Capacity);
                string strTitle =stringBuilder.ToString();
                if(!string.IsNullOrEmpty(strTitle))
                {
                    fromInfo.Add(newFromInfo(strTitle, handle));
                }
                else
                {
                    fromInfo.Add(newFromInfo("", handle));
                }
                handle =GetWindow(handle, GW_HWNDNEXT);
            }
            return fromInfo;
        }

        public class FromInfo
        {
            public FromInfo(string title,int handle)
            {
                this.title = title;
                this.handle = handle;
            }
            private string title;
            private int handle;

            public string Title
            {
                get { return title; }
                set { title = value; }
            }
            public int Handle
            {
                get { return handle; }
                set { handle = value; }
            }
        }

        //获得窗体句柄和标题
        private void button1_Click(objectsender, EventArgs e)
        {
            List<FromInfo> fromInfo= GetHandleList(this.Handle.ToInt32());
        }
        private void button2_Click(objectsender, EventArgs e)
        {
            //隐藏窗体
           ShowWindow(this.Handle.ToInt32(), SW_HIDE);
            //显示窗体
           ShowWindow(this.Handle.ToInt32(), SW_SHOW);
        }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324969285&siteId=291194637