2

I follow the below steps to pass a string input to a console from C#:

  1. I allocate a console to Visual Studio Process using AllocConsole() method, by pInvoking Kernel32.dll.
  2. I call a Perl process from Visual Studio (C#). The Perl process attaches to this console automatically. (which is OK for me).
  3. I kill 'only' Perl Process by passing Ctrl C signal to it.( I suppress Ctrl C signal for Visual Studio)
  4. Now I want to send a command (a simple string basically) to this console.

What is the method to pass input string to the console?

I am trying to use writeConsoleInput(), by pInvoking Kernel32.dll, but I am stuck on the arguments of this function. The function signature is:

[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool WriteConsoleInput(IntPtr hConsoleInput,
    [Out] INPUT_RECORD[] lpBuffer,
    int nLength,
    out int lpNumberOfEventsWritten);

Can somebody help me to successfully call WriteConsoleInput() or by giving an alternative option.

Many Thanks!

lc.
  • 113,939
  • 20
  • 158
  • 187
CSharpLearner
  • 21
  • 2
  • 4
  • What "command" are you trying to send to the console? Do you just want to write text on the console? You'll have to explain what your larger purpose is, because the description in your question doesn't make sense. – Jim Mischel Aug 15 '13 at 02:39
  • Try this [link](http://stackoverflow.com/questions/854666/how-to-write-to-the-console-input-and-get-the-console-handle) – Nilesh Aug 15 '13 at 02:51
  • Surely you just need to write on the perl processes standard input? – David Heffernan Aug 15 '13 at 07:28
  • I think I can refine my question to make it more clear: It is similar to http://stackoverflow.com/questions/1469764/run-command-prompt-commands Instead of creating system.diagonostic.process, I already have a console attached to Visual studio process (by AllocConsole()) and I want to run my command prompt command in this console. – CSharpLearner Aug 15 '13 at 07:31
  • @jimMischel: Its a command prompt command. – CSharpLearner Aug 15 '13 at 07:37
  • @davidHeffernan : I kill the Perl Process. I want to run a command prompt command after that. – CSharpLearner Aug 15 '13 at 07:43
  • One more thing- Should I try to run this command prompt command from Visual Studio console (got through AllocConsole()) or open a new System.Diagonostics.Process (cmd.exe) and run this command there? – CSharpLearner Aug 15 '13 at 07:47
  • Normally you'd just start a process with `System.Diagnostics.Process`. – Jim Mischel Aug 15 '13 at 13:25
  • Thank you all. I achieved my task using System.Diagnostics.Process. @davidHeffernan: As you said: "A console is in itself dumb.", my visual studio console is something like C:\...\...\bin\Debug\project.vshost.exe. Out of curiosity, Is it possible to run command prompt commands from this console.I got this console through AllocConsole(), therefore dont know much about the functions that could allow me to pass an input string. Need to find out still! Thanks. – CSharpLearner Aug 16 '13 at 05:33
  • I covered that in my answer. You need a command interpreter to run commands. A console is dumb. But a command interpreter can be attached to any console. – David Heffernan Aug 16 '13 at 06:51

1 Answers1

1

The detail of the question appears to be this comment:

I kill the Perl Process. I want to run a command prompt command after that.

A console is in itself dumb. It cannot do anything beyond receive input, and display output. To run a command you need a command interpreter. On Windows that is cmd.exe. The generic way to find out the path to the command interpreter read the COMSPEC environment variable.

So, you are going to need to start a new command interpreter process to run your command. Pass the command as an argument when starting this new process.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490