Ejemplo n.º 1
0
        public WindowInfo GetWindowInfo()
        {
            var process = Process;
            var info    = new WindowInfo();

            info.GetWindowText      = GetWindowText();
            info.WM_GETTEXT         = GetWmGettext();
            info.GetClassName       = GetClassName();
            info.RealGetWindowClass = RealGetWindowClass();
            info.Handle             = Handle;
            info.ParentHandle       = NativeMethods.GetParent(Handle);
            info.Size         = Size;
            info.ProcessId    = ProcessId;
            info.ThreadId     = WindowUtils.GetThreadId(Handle);
            info.GWL_STYLE    = NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_STYLE);
            info.GWL_EXSTYLE  = NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_EXSTYLE);
            info.GWL_ID       = NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_ID);
            info.GWL_USERDATA = NativeMethods.GetWindowLong(Handle, NativeConstants.GWL_USERDATA);
            info.GCL_STYLE    = NativeMethods.GetClassLong(Handle, NativeConstants.GCL_STYLE);
            info.GCL_WNDPROC  = NativeMethods.GetClassLong(Handle, NativeConstants.GCL_WNDPROC);
            info.DWL_DLGPROC  = NativeMethods.GetClassLong(Handle, NativeConstants.DWL_DLGPROC);
            info.DWL_USER     = NativeMethods.GetClassLong(Handle, NativeConstants.DWL_USER);
            info.FullPath     = process == null ? "" : process.GetMainModuleFileName();
            info.FullPath     = info.FullPath == null ? "" : info.FullPath;
            info.Priority     = ProcessPriority;
            info.StartTime    = process == null ? (DateTime?)null : process.StartTime;

            try
            {
                var processInfo = SystemUtils.GetWmiProcessInfo(process.Id);
                info.Owner          = processInfo.Owner;
                info.CommandLine    = processInfo.CommandLine;
                info.ThreadCount    = processInfo.ThreadCount;
                info.HandleCount    = processInfo.HandleCount;
                info.VirtualSize    = processInfo.VirtualSize;
                info.WorkingSetSize = processInfo.WorkingSetSize;
            }
            catch
            {
            }

            try
            {
                info.FontName = GetFontName();
            }
            catch
            {
            }

            try
            {
                var windowInfo = new WINDOW_INFO();
                windowInfo.cbSize = Marshal.SizeOf(windowInfo);
                if (NativeMethods.GetWindowInfo(Handle, ref windowInfo))
                {
                    info.WindowInfoExStyle = windowInfo.dwExStyle;
                }
            }
            catch
            {
            }

            try
            {
                uint key;
                Byte alpha;
                uint flags;
                var  result        = NativeMethods.GetLayeredWindowAttributes(Handle, out key, out alpha, out flags);
                var  layeredWindow = (LayeredWindow)flags;
                info.LWA_ALPHA    = layeredWindow.HasFlag(LayeredWindow.LWA_ALPHA);
                info.LWA_COLORKEY = layeredWindow.HasFlag(LayeredWindow.LWA_COLORKEY);
            }
            catch
            {
            }

            try
            {
                info.Instance = process == null ? IntPtr.Zero : process.Modules[0].BaseAddress;
            }
            catch
            {
            }

            try
            {
                info.Parent = Path.GetFileName(process.GetParentProcess().MainModule.FileName);
            }
            catch
            {
            }

            try
            {
                var fileVersionInfo = process.MainModule.FileVersionInfo;
                info.ProductName    = fileVersionInfo.ProductName;
                info.ProductVersion = fileVersionInfo.ProductVersion;
                info.FileVersion    = fileVersionInfo.FileVersion;
                info.Copyright      = fileVersionInfo.LegalCopyright;
            }
            catch
            {
            }

            /*try
             * {
             *  var control = Control.FromHandle(Handle);
             *  var accessibilityObject = control.AccessibilityObject;
             *  info.AccessibleName = accessibilityObject == null ? "" : accessibilityObject.Name;
             *  info.AccessibleValue = accessibilityObject == null ? "" : accessibilityObject.Value;
             *  info.AccessibleRole = accessibilityObject == null ? "" : accessibilityObject.Role.ToString();
             *  info.AccessibleDescription = accessibilityObject == null ? "" : accessibilityObject.Description;
             * }
             * catch
             * {
             * }*/

            return(info);
        }
Ejemplo n.º 2
0
        public static WindowInformation GetWindowInformation(IntPtr hWnd)
        {
            var text            = GetWindowText(hWnd);
            var wmText          = GetWmGettext(hWnd);
            var className       = GetClassName(hWnd);
            var realWindowClass = RealGetWindowClass(hWnd);
            var hWndParent      = NativeMethods.GetParent(hWnd);
            var size            = GetWindowSize(hWnd);
            var placement       = GetWindowPlacement(hWnd);
            var threadId        = NativeMethods.GetWindowThreadProcessId(hWnd, out var processId);
            var process         = GetProcessByIdSafely(processId);

            var gwlStyle    = NativeMethods.GetWindowLong(hWnd, NativeConstants.GWL_STYLE);
            var gwlExstyle  = NativeMethods.GetWindowLong(hWnd, NativeConstants.GWL_EXSTYLE);
            var gwlUserData = NativeMethods.GetWindowLong(hWnd, NativeConstants.GWL_USERDATA);
            var gclStyle    = NativeMethods.GetClassLong(hWnd, NativeConstants.GCL_STYLE);
            var gclWndproc  = NativeMethods.GetClassLong(hWnd, NativeConstants.GCL_WNDPROC);
            var dwlDlgproc  = NativeMethods.GetClassLong(hWnd, NativeConstants.DWL_DLGPROC);
            var dwlUser     = NativeMethods.GetClassLong(hWnd, NativeConstants.DWL_USER);

            var windowDetailes = new Dictionary <string, string>();

            windowDetailes.Add("GetWindowText", text);
            windowDetailes.Add("WM_GETTEXT", wmText);
            windowDetailes.Add("GetClassName", className);
            windowDetailes.Add("RealGetClassName", realWindowClass);
            try
            {
                windowDetailes.Add("Font Name", GetFontName(hWnd));
            }
            catch
            {
            }

            windowDetailes.Add("Window Handle", $"0x{hWnd.ToInt64():X}");
            windowDetailes.Add("Parent Window Handle", hWndParent == IntPtr.Zero ? "-" : $"0x{hWndParent.ToInt64():X}");
            windowDetailes.Add("Window Placement", placement.showCmd.ToString());
            windowDetailes.Add("Window Size", $"{size.Width} x {size.Height}");

            try
            {
                windowDetailes.Add("Instance", $"0x{process.Modules[0].BaseAddress.ToInt64():X}");
            }
            catch
            {
            }

            windowDetailes.Add("GCL_WNDPROC", $"0x{gclWndproc:X}");
            windowDetailes.Add("DWL_DLGPROC", $"0x{dwlDlgproc:X}");
            windowDetailes.Add("GWL_STYLE", $"0x{gwlStyle:X}");
            windowDetailes.Add("GCL_STYLE", $"0x{gclStyle:X}");
            windowDetailes.Add("GWL_EXSTYLE", $"0x{gwlExstyle:X}");

            try
            {
                var windowInfo = new WINDOW_INFO();
                windowInfo.cbSize = Marshal.SizeOf(windowInfo);
                if (NativeMethods.GetWindowInfo(hWnd, ref windowInfo))
                {
                    windowDetailes.Add("WindowInfo.ExStyle", $"0x{windowInfo.dwExStyle:X}");
                }
            }
            catch
            {
            }

            try
            {
                uint key;
                Byte alpha;
                uint flags;
                var  result        = NativeMethods.GetLayeredWindowAttributes(hWnd, out key, out alpha, out flags);
                var  layeredWindow = (LayeredWindow)flags;
                windowDetailes.Add("LWA_ALPHA", layeredWindow.HasFlag(LayeredWindow.LWA_ALPHA) ? "+" : "-");
                windowDetailes.Add("LWA_COLORKEY", layeredWindow.HasFlag(LayeredWindow.LWA_COLORKEY) ? "+" : "-");
            }
            catch
            {
            }

            windowDetailes.Add("GWL_USERDATA", $"0x{gwlUserData:X}");
            windowDetailes.Add("DWL_USER", $"0x{dwlUser:X}");

            var processDetailes = new Dictionary <string, string>();

            try
            {
                try
                {
                    processDetailes.Add("Full Path", process.MainModule.FileName);
                }
                catch
                {
                    var fileNameBuilder = new StringBuilder(1024);
                    var bufferLength    = (uint)fileNameBuilder.Capacity + 1;
                    var fullPath        = NativeMethods.QueryFullProcessImageName(process.Handle, 0, fileNameBuilder, ref bufferLength) ? fileNameBuilder.ToString() : "";
                    processDetailes.Add("Full Path", fullPath);
                }
            }
            catch
            {
            }

            var processInfo = (WmiProcessInfo)null;

            try
            {
                processInfo = GetWmiProcessInfo(processId);
                processDetailes.Add("Command Line", processInfo.CommandLine);
            }
            catch
            {
            }

            try
            {
                processDetailes.Add("Started at", $"{process.StartTime:dd.MM.yyyy HH:mm:ss}");
            }
            catch
            {
            }

            try
            {
                processDetailes.Add("Owner", processInfo.Owner);
            }
            catch
            {
            }

            processDetailes.Add("Process Id", processId.ToString());
            try
            {
                var parentProcess = process.GetParentProcess();
                processDetailes.Add("Parent Process Id", parentProcess.Id.ToString());
                processDetailes.Add("Parent", Path.GetFileName(parentProcess.MainModule.FileName));
            }
            catch
            {
            }

            processDetailes.Add("Thread Id", threadId.ToString());

            try
            {
                processDetailes.Add("Priority", process.GetProcessPriority().ToString());
            }
            catch
            {
            }


            try
            {
                processDetailes.Add("Threads", processInfo.ThreadCount.ToString());
                processDetailes.Add("Handles", processInfo.HandleCount.ToString());
                processDetailes.Add("Working Set Size", processInfo.WorkingSetSize.ToString());
                processDetailes.Add("Virtual Size", processInfo.VirtualSize.ToString());
            }
            catch
            {
            }

            try
            {
                var fileVersionInfo = process.MainModule.FileVersionInfo;
                processDetailes.Add("Product Name", fileVersionInfo.ProductName);
                processDetailes.Add("Copyright", fileVersionInfo.LegalCopyright);
                processDetailes.Add("File Version", fileVersionInfo.FileVersion);
                processDetailes.Add("Product Version", fileVersionInfo.ProductVersion);
            }
            catch
            {
            }

            return(new WindowInformation(windowDetailes, processDetailes));
        }