コード例 #1
0
        //Posts a thread message to the first enumerated thread (when ensureTargetThreadHasWindow is false), or posts a thread message to the first enumerated thread with a window, unless no windows are found in which case the call fails.  If an appropriate thread was found, the return value is the return value of PostThreadMessage call, otherwise the return value is false.
        public static bool PostThreadMessage(this Process p, uint msg, IntPtr wParam, IntPtr lParam, bool ensureTargetThreadHasWindow = true)
        {
            uint targetThreadId = 0;

            if (ensureTargetThreadHasWindow)
            {
                IntPtr hwnd      = p.WindowHandles().FirstOrDefault();
                uint   processId = 0;
                if (hwnd != IntPtr.Zero)
                {
                    targetThreadId = Win32Helpers.GetWindowThreadProcessId(hwnd, out processId);
                }
            }
            else
            {
                targetThreadId = (uint)p.Threads[0].Id;
            }
            if (targetThreadId != 0)
            {
                return(Win32Helpers.PostThreadMessage(targetThreadId, msg, wParam, lParam));
            }
            else
            {
                return(false);
            }
        }
コード例 #2
0
ファイル: WindowHelpers.cs プロジェクト: eaho1/Spooky-Spoils
    internal static IntPtr FindWindowWithThreadProcessId(int processId)
    {
        var window = new IntPtr();

        Win32Helpers.EnumWindows(delegate(IntPtr wnd, IntPtr param)
        {
            var windowProcessId = 0;
            Win32Helpers.GetWindowThreadProcessId(wnd, out windowProcessId);
            if (windowProcessId == processId)
            {
                window = wnd;
                return(false);
            }

            return(true);
        },
                                 IntPtr.Zero);

        if (window.Equals(IntPtr.Zero))
        {
            UnityEngine.Debug.LogError("Could not find any window with process id " + processId);
        }

        return(window);
    }
コード例 #3
0
ファイル: WindowHelpers.cs プロジェクト: eaho1/Spooky-Spoils
    /// <summary>
    /// Gets the window handle for the game view window.
    /// </summary>
    /// <param name="processId">The id of the process that owns the game view window.</param>
    /// <returns>Window handle if the window was found, IntPtr.Zero otherwise.</returns>
    internal static IntPtr GetGameViewWindowHandle(int processId)
    {
        const string GameViewCaption         = "UnityEditor.GameView";
        const string UnityContainerClassName = "UnityContainerWndClass";

        var window = new IntPtr();

        Win32Helpers.EnumWindows(delegate(IntPtr hWnd, IntPtr lParam)
        {
            if (!Win32Helpers.IsWindowVisible(hWnd))
            {
                return(true);
            }

            var windowProcessId = 0;
            Win32Helpers.GetWindowThreadProcessId(hWnd, out windowProcessId);

            if (windowProcessId == processId)
            {
                StringBuilder builder = new StringBuilder(256);
                Win32Helpers.GetClassName(hWnd, builder, 256);

                if (builder.ToString() == UnityContainerClassName)
                {
                    //Ok, we found one of our containers, let's try to find the game view handle among the children
                    Win32Helpers.EnumChildWindows(hWnd, delegate(IntPtr childHwnd, IntPtr childParam)
                    {
                        if (!Win32Helpers.IsWindowVisible(childHwnd))
                        {
                            return(true);
                        }

                        int childLength = Win32Helpers.GetWindowTextLength(childHwnd);
                        if (childLength == 0)
                        {
                            return(true);
                        }

                        StringBuilder childBuilder = new StringBuilder(childLength);
                        Win32Helpers.GetWindowText(childHwnd, childBuilder, childLength + 1);

                        if (childBuilder.ToString() == GameViewCaption)
                        {
                            //Found it!
                            window = childHwnd;
                            return(false);
                        }

                        return(true);
                    },
                                                  IntPtr.Zero);
                }
            }

            return(true);
        }, IntPtr.Zero);

        return(window);
    }