none
Send password or string to external console application.

    Question

  • I am trying to pass password from one console application to another console application. I tried to pass the password to the application "console.exe" as below. But I am stuck here and unable to pass the password to console.exe.

    The command "Console.exe disable" asks for "Password:" and I need to pass the password to disable console.exe. How to acheive the same ?

                Process myProcess = new Process();

                myProcess.StartInfo.FileName = "C:\\Program Files\\Console.exe";
                myProcess.StartInfo.Arguments = "disable";
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.RedirectStandardOutput = true;
                myProcess.StartInfo.RedirectStandardInput = true;
                myProcess.Start();
                StreamWriter stdInputWriter = myProcess.StandardInput;
              

                stdInputWriter.Write("password");
                stdInputWriter.Close();
                myProcess.WaitForExit();
                myProcess.Close();
             
    Tuesday, May 07, 2013 8:48 AM

Answers

  • The timing may be difficult, depending on the other app, but give SendKeys.Send("some password") a try.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

    Mike

    Tuesday, May 07, 2013 10:58 AM
  • Yes. SendKeys.Send or SendKeys.SendWait should work. But timing is important. You can send the text after reading some specific text that the application shows before reading password


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Tuesday, May 07, 2013 5:41 PM
  • Ello.

    If the application uses a method unknown to you to read the password, sending the keys into the window might indeed be in order. Not optimal though, there's always a better way than to directly simulate input. Just good enough.

    Not using SendKeys, however. SendKeys just simulates keyboard input without any regard to the focused window or its state, and thus is far, far from robust.

    What might help you is a Windows API function called PostMessage. It does what it says, posts a message of your choice into the message queue of the window of your liking, waits for the message to finish and then returns.

    I made a little snippet for you to demonstrate this. It opens a command prompt, prints out "hello" and pauses.

    The function CmdPostKeys sends the characters one by one, posting a WM_KeyUp event for each of the desired letters, converting the chars to Keys with VkKeyScan API.

    [return: MarshalAs(UnmanagedType.Bool)]
            [DllImport("user32.dll", SetLastError = true)]
            private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
    
            [DllImport("user32.dll")]
            static extern short VkKeyScan(char ch);
    
            private const int WmKeyUp = 0x101;
    
            private static void Main(string[] args)
            {
                var cmdProcess = Process.Start("cmd.exe");
                Thread.Sleep(1000);
                CmdPostKeys(cmdProcess, "echo hello there!");
                CmdPostKeys(cmdProcess, "pause");
            }
    
            private static void CmdPostKeys(Process cmdProcess, string message)
            {
                foreach (var c in message)
                {
                    PostMessage(cmdProcess.MainWindowHandle, WmKeyUp, (IntPtr)VkKeyScan(c), IntPtr.Zero);
                }
    
                PostMessage(cmdProcess.MainWindowHandle, WmKeyUp, (IntPtr)Keys.Enter, IntPtr.Zero);
            }

    I would call this a somewhat dirty solution, but it's definitely better than using SendKeys. The window doesn't even have to be visible for the keys to be sent.

    Note that trying to send "hello!" would send "hello1" instead, because you're not sending text to the console input, but a key-down message into it's window. If you wanted to send the exclamation mark, you would have to first call KeyDown for SHIFT, and then for 1.


    Teo Selenius

    Tuesday, May 07, 2013 7:10 PM

All replies

  • If the console.exe, reads the password using a different methodology like masking the password, it will not be possible to send it through Process. It might have monitoring the keyboard events. 

    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Tuesday, May 07, 2013 8:53 AM
  • Process.Start(@"C:\my.exe","arg1 \"arg 2\"");


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Tuesday, May 07, 2013 9:22 AM
  • Yes. password is masked. So do you think it is impossible to pass the password ?
    Tuesday, May 07, 2013 10:25 AM
  • The timing may be difficult, depending on the other app, but give SendKeys.Send("some password") a try.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

    Mike

    Tuesday, May 07, 2013 10:58 AM
  • Yes. SendKeys.Send or SendKeys.SendWait should work. But timing is important. You can send the text after reading some specific text that the application shows before reading password


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Tuesday, May 07, 2013 5:41 PM
  • Ello.

    If the application uses a method unknown to you to read the password, sending the keys into the window might indeed be in order. Not optimal though, there's always a better way than to directly simulate input. Just good enough.

    Not using SendKeys, however. SendKeys just simulates keyboard input without any regard to the focused window or its state, and thus is far, far from robust.

    What might help you is a Windows API function called PostMessage. It does what it says, posts a message of your choice into the message queue of the window of your liking, waits for the message to finish and then returns.

    I made a little snippet for you to demonstrate this. It opens a command prompt, prints out "hello" and pauses.

    The function CmdPostKeys sends the characters one by one, posting a WM_KeyUp event for each of the desired letters, converting the chars to Keys with VkKeyScan API.

    [return: MarshalAs(UnmanagedType.Bool)]
            [DllImport("user32.dll", SetLastError = true)]
            private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
    
            [DllImport("user32.dll")]
            static extern short VkKeyScan(char ch);
    
            private const int WmKeyUp = 0x101;
    
            private static void Main(string[] args)
            {
                var cmdProcess = Process.Start("cmd.exe");
                Thread.Sleep(1000);
                CmdPostKeys(cmdProcess, "echo hello there!");
                CmdPostKeys(cmdProcess, "pause");
            }
    
            private static void CmdPostKeys(Process cmdProcess, string message)
            {
                foreach (var c in message)
                {
                    PostMessage(cmdProcess.MainWindowHandle, WmKeyUp, (IntPtr)VkKeyScan(c), IntPtr.Zero);
                }
    
                PostMessage(cmdProcess.MainWindowHandle, WmKeyUp, (IntPtr)Keys.Enter, IntPtr.Zero);
            }

    I would call this a somewhat dirty solution, but it's definitely better than using SendKeys. The window doesn't even have to be visible for the keys to be sent.

    Note that trying to send "hello!" would send "hello1" instead, because you're not sending text to the console input, but a key-down message into it's window. If you wanted to send the exclamation mark, you would have to first call KeyDown for SHIFT, and then for 1.


    Teo Selenius

    Tuesday, May 07, 2013 7:10 PM