Example #1
0
            /// <summary>
            /// Adds a string to the console input buffer.
            /// </summary>
            /// <param name="str">String to add to console input buffer.</param>
            /// <param name="newLine">True to add Enter after the string.</param>
            /// <returns>True if it was successful in adding all characters to console input buffer.</returns>
            internal static bool AddToConsoleInputBuffer(string str, bool newLine)
            {
                IntPtr handle = ConsoleInputWithNativeMethods.GetStdHandle(ConsoleInputWithNativeMethods.STD_INPUT_HANDLE);

                if (handle == IntPtr.Zero)
                {
                    return(false);
                }

                uint strLen = (uint)str.Length;

                ConsoleInputWithNativeMethods.INPUT_RECORD[] records = new ConsoleInputWithNativeMethods.INPUT_RECORD[strLen + (newLine ? 1 : 0)];

                for (int i = 0; i < strLen; i++)
                {
                    ConsoleInputWithNativeMethods.INPUT_RECORD.SetInputRecord(ref records[i], str[i]);
                }

                uint written;

                if (!ConsoleInputWithNativeMethods.WriteConsoleInput(handle, records, strLen, out written) || written != strLen)
                {
                    // I do not know of a case where written is not going to be strlen. Maybe for some character that
                    // is not supported in the console. The API suggests this can happen,
                    // so we handle it by returning false
                    return(false);
                }

                // Enter is written separately, because if this is a command, and one of the characters in the command was not written
                // (written != strLen) it is desireable to fail (return false) before typing enter and running the command
                if (newLine)
                {
                    ConsoleInputWithNativeMethods.INPUT_RECORD[] enterArray = new ConsoleInputWithNativeMethods.INPUT_RECORD[1];
                    ConsoleInputWithNativeMethods.INPUT_RECORD.SetInputRecord(ref enterArray[0], (char)13);

                    written = 0;
                    if (!ConsoleInputWithNativeMethods.WriteConsoleInput(handle, enterArray, 1, out written))
                    {
                        // I don't think this will happen
                        return(false);
                    }

                    Diagnostics.Assert(written == 1, "only Enter is being added and it is a supported character");
                }

                return(true);
            }
Example #2
0
            /// <summary>
            /// Adds a string to the console input buffer
            /// </summary>
            /// <param name="str">string to add to console input buffer</param>
            /// <param name="newLine">true to add Enter after the string</param>
            /// <returns>true if it was successful in adding all characters to console input buffer</returns>
            internal static bool AddToConsoleInputBuffer(string str, bool newLine)
            {
                IntPtr handle = ConsoleInputWithNativeMethods.GetStdHandle(ConsoleInputWithNativeMethods.STD_INPUT_HANDLE);
                if (handle == IntPtr.Zero)
                {
                    return false;
                }

                uint strLen = (uint)str.Length;

                ConsoleInputWithNativeMethods.INPUT_RECORD[] records = new ConsoleInputWithNativeMethods.INPUT_RECORD[strLen + (newLine ? 1 : 0)];

                for (int i = 0; i < strLen; i++)
                {
                    ConsoleInputWithNativeMethods.INPUT_RECORD.SetInputRecord(ref records[i], str[i]);
                }

                uint written;
                if (!ConsoleInputWithNativeMethods.WriteConsoleInput(handle, records, strLen, out written) || written != strLen)
                {
                    // I do not know of a case where written is not going to be strlen. Maybe for some character that
                    // is not supported in the console. The API suggests this can happen, 
                    // so we handle it by returning false
                    return false;
                }

                // Enter is written separately, because if this is a command, and one of the characters in the command was not written
                // (written != strLen) it is desireable to fail (return false) before typing enter and running the command
                if (newLine)
                {
                    ConsoleInputWithNativeMethods.INPUT_RECORD[] enterArray = new ConsoleInputWithNativeMethods.INPUT_RECORD[1];
                    ConsoleInputWithNativeMethods.INPUT_RECORD.SetInputRecord(ref enterArray[0], (char)13);

                    written = 0;
                    if (!ConsoleInputWithNativeMethods.WriteConsoleInput(handle, enterArray, 1, out written))
                    {
                        // I don't think this will happen
                        return false;
                    }

                    Diagnostics.Assert(written == 1, "only Enter is being added and it is a supported character");
                }

                return true;
            }