Xcelled194 28 Junior Poster in Training

Hi guys, my program needs to manipulate the command line, and it needs administrator access. So, I'm using a Process with the administrator user name and password, then piping input back and forth. It works 100% if i leave out the credentials, but if I add them in, on the CMD window (NOT my app), it says "The handle is invalid" and then exits. Any idea what's wrong?

Command.exe is merely a copy of cmd.exe that's copied to the program directory for test purposes.

static System.Diagnostics.Process P;
		static string Prompt = ">>>";
		static System.IO.StreamReader Out;
		static System.IO.StreamWriter In;
        static System.IO.StreamReader Err;
        static bool GodModeOn = true;

        private static void StartProcess()
        {
            Console.WriteLine("Initializing Process Variable....");
            P = new System.Diagnostics.Process();
            Console.WriteLine("Setting startup parameters:");
            P.StartInfo.FileName = "command.exe";
            Console.WriteLine("\tFilename: " + P.StartInfo.FileName);
            P.StartInfo.Arguments = "-D";
            Console.WriteLine("\tArguments: " + P.StartInfo.Arguments);
            P.StartInfo.CreateNoWindow = false;
            Console.WriteLine("\tCreate No Window: " + P.StartInfo.CreateNoWindow);
            P.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            Console.WriteLine("\tWindow Style: " + P.StartInfo.WindowStyle);
            P.StartInfo.UseShellExecute = false;
            Console.WriteLine("\tUse Shell Execute: " + P.StartInfo.UseShellExecute);
            P.StartInfo.RedirectStandardInput = true;
            Console.WriteLine("\tRedirect Standard Input: " + P.StartInfo.RedirectStandardInput);
            P.StartInfo.RedirectStandardOutput = true;
            Console.WriteLine("\tRedirect Standard Output: " + P.StartInfo.RedirectStandardOutput);
            P.StartInfo.RedirectStandardError = true;
            Console.WriteLine("Redirect Standard Error: " + P.StartInfo.RedirectStandardError);
            Console.WriteLine("\tGod Mode: " + GodModeOn);
            if (GodModeOn)
            {
                Console.WriteLine("\tConfiguring GodMode....");
                P.StartInfo.UserName = "Admin";
                P.StartInfo.Password = new System.Security.SecureString();
                foreach (char C in "123456".ToCharArray())
                {
                    P.StartInfo.Password.AppendChar(C);
                }
                Console.WriteLine(GodMode);
            }
            Console.Write("Starting process....");
            try
            {
                P.Start();
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("Done");
            Console.WriteLine("Process started at: " + P.StartTime);
            Console.Write("Redirecting streams....");
            Out = P.StandardOutput;
            In = P.StandardInput;
            Err = P.StandardError;
            In.WriteLine("@Echo off");
            In.WriteLine("Echo ~!~");
            Console.WriteLine("Done");
            EmptyStream(Out, "~!~", "Echo ~!~");
        }

        private static void EmptyStream(System.IO.StreamReader S, string PC, string Not)
        {
//Simply empties the command output after start up to remove the 
//header information and residuals of config commands
            string t = "";
                while (!t.Contains(PC)) {
                    t = S.ReadLine();
                    if (t.Contains(Not))
                        t = "";
                    if (t == null)
                        return;
                }
        }

Also note, if I run another program (like NOTEPAD) it works fine in both "modes".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.