GetWindowText() private method

private GetWindowText ( IntPtr hWnd, StringBuilder className, int stringLength ) : int
hWnd System.IntPtr
className StringBuilder
stringLength int
return int
        /// <summary>
        /// Finds a messagebox string on a messagebox.
        /// </summary>
        /// <param name="hwnd">The windows handle of the dialog</param>
        /// <param name="unmanagedMemoryLocation">A pointer to the memorylocation the string will be written to</param>
        /// <returns>True if found.</returns>
        private static bool FindMessageBoxString(IntPtr hwnd, IntPtr unmanagedMemoryLocation)
        {
            StringBuilder sb = new StringBuilder(512);

            NativeMethods.GetClassName(hwnd, sb, sb.Capacity);

            if (sb.ToString().ToLower().Contains("static"))
            {
                StringBuilder windowText = new StringBuilder(2048);
                NativeMethods.GetWindowText(hwnd, windowText, windowText.Capacity);

                if (windowText.Length > 0)
                {
                    IntPtr stringAsPtr = IntPtr.Zero;
                    try
                    {
                        stringAsPtr = Marshal.StringToHGlobalAnsi(windowText.ToString());
                        char[] stringAsArray = windowText.ToString().ToCharArray();

                        // Since unicode characters are copied check if we are out of the allocated length.
                        // If not add the end terminating zero.
                        if ((2 * stringAsArray.Length) + 1 < 2048)
                        {
                            Marshal.Copy(stringAsArray, 0, unmanagedMemoryLocation, stringAsArray.Length);
                            Marshal.WriteInt32(unmanagedMemoryLocation, 2 * stringAsArray.Length, 0);
                        }
                    }
                    finally
                    {
                        if (stringAsPtr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(stringAsPtr);
                        }
                    }
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// This is the thread method.
        /// </summary>
        private void HandleDialogBoxes()
        {
            // No synchronization numberOfDialogsToWaitFor since it is readonly
            IntPtr[] hwnds = new IntPtr[this.numberOfDialogsToWaitFor];
            bool[]   dialogBoxCloseResults = new bool[this.numberOfDialogsToWaitFor];

            try
            {
                // Signal that we started
                lock (Mutex)
                {
                    this.threadStarted.Set();
                }

                // The loop will be exited either if a message is send by the caller thread or if we found the dialog. If a message box text is specified the loop will not exit until the dialog is found.
                bool stayInLoop           = true;
                int  dialogBoxesToWaitFor = 1;

                while (stayInLoop)
                {
                    int hwndIndex = dialogBoxesToWaitFor - 1;

                    // We need to lock since the caller might set context to null.
                    lock (Mutex)
                    {
                        if (this.exitThread)
                        {
                            break;
                        }

                        // We protect the shell too from reentrency.
                        this.uiShell.GetDialogOwnerHwnd(out hwnds[hwndIndex]);
                    }

                    if (hwnds[hwndIndex] != IntPtr.Zero)
                    {
                        StringBuilder windowText = new StringBuilder(256);
                        NativeMethods.GetWindowText(hwnds[hwndIndex], windowText, windowText.Capacity);

                        if (windowText.ToString().Equals(this.expectedDialogBoxText))
                        {
                            lock (Mutex)
                            {
                                var result = NativeMethods.SendMessage(hwnds[hwndIndex], NativeMethods.WM_CLOSE, 0, IntPtr.Zero);

                                dialogBoxCloseResults[hwndIndex] = (result == 0);
                                if (dialogBoxesToWaitFor++ >= this.numberOfDialogsToWaitFor)
                                {
                                    stayInLoop = false;
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                //Let the main thread run a possible close command.
                System.Threading.Thread.Sleep(2000);

                foreach (IntPtr hwnd in hwnds)
                {
                    // At this point the dialog should be closed, if not attempt to close it.
                    if (hwnd != IntPtr.Zero)
                    {
                        NativeMethods.SendMessage(hwnd, NativeMethods.WM_CLOSE, 0, new IntPtr(0));
                    }
                }

                lock (Mutex)
                {
                    // Be optimistic.
                    this.dialogBoxCloseResult = true;

                    for (int i = 0; i < dialogBoxCloseResults.Length; i++)
                    {
                        if (!dialogBoxCloseResults[i])
                        {
                            this.dialogBoxCloseResult = false;
                            break;
                        }
                    }

                    this.threadDone.Set();
                }
            }
        }