0

I'm really stuck with a problem (for 7 days now) and before I go crazy I thought I'll just ask for advice.

Want I want to do is basically to start and remote control a cmd.exe process. However the StandardInput and StandardOutput streams don't do the job, since they aren't triggered by commands like "mysql" which wait for a user input.

So all I want is to read the content of the cmd.exe console and write to it. However the write method should be able to fire a new command.

I tried a lot and I'm quite sure the kernel32.dll will do the trick:

  • ReadConsoleOutput()
  • WriteConsoleInput()
  • GetConsoleScreenBufferInfo()
  • SetConsoleCursorPosition()

Of course I already have some stuff working, but I'm absolutly not sure whether or not I took the right road. Therefore I won't post any code since it would probably just confuse you.

It would be awesome if someone could point me to a working demo or tutorial or something like that. Even if it's C++ or C - would be better than nothing.

Thanks in advance.

Mewes Kochheim

PS: Please don't post just a link to pinvoke or MSDN, what I need is little bit more complex. And it seems like I don't have enough winapi experience to get there on my own :/

Edit: I've to try out some of the new ideas I got based on the links you guys posted. I'll will be back and post what I figured out. Thanks for the help guys...

MewesK
  • 1
  • 2
  • what do you mean by "invisible" ? in the TaskManager ? On the screen ? – Yahia Aug 17 '11 at 11:09
  • On the screen, but hiding the window is actually not really a problem :) If it's invisible in the Task Manager that would be okay too but it's not a requirement. I edited the queytion to clarify that point. "Want I want to do is basically to start and remote control a cmd.exe process." – MewesK Aug 17 '11 at 11:21
  • IF you already know about the kernel32.dll console API (http://msdn.microsoft.com/en-us/library/ms682073%28v=VS.85%29.aspx) what exactly do you need ? – Yahia Aug 17 '11 at 11:29
  • A working example. I have all the information I need, but obviously not the skills to get it right. I'm into Java web development for a living and WinAPI programming is quite new to me. – MewesK Aug 17 '11 at 11:40
  • check my answer - 3 links to different samples (not very specific though)... – Yahia Aug 17 '11 at 11:53
  • The basic example I want would be: Starting a cmd.exe process, reading it's content, starting mysql.exe in it, reading the consolebuffer of the cmd.exe process, writing something like "use mytable"+ENTER+"drop table mytable"+ENTER, and reading the output again. – MewesK Aug 17 '11 at 12:00
  • then check my answer below - there are no mysql samples in those links, but enough to give a good start – Yahia Aug 17 '11 at 12:01

1 Answers1

0

here you find several examples regarding "remote control of command prompt"...

another link (.NET this time from stackoverflow)...

and something from visualstudio magazine...

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • I already tried the examples from link 1 and 2 without much success. I'll look into link 3 this evening. But basically examples like that are what made the last couple of days a hell :) – MewesK Aug 17 '11 at 12:05
  • to me they look fine... as I wrote above: every program you want to control with this methos has its own "quirks" so it is some trial&error when you really want to implement it this way... – Yahia Aug 17 '11 at 12:06
  • btw: mysql takes a textfile as input - perhaps this could help with your goal... see http://dev.mysql.com/doc/refman/5.0/en/batch-commands.html – Yahia Aug 17 '11 at 12:08
  • MySQL was just an example. And since I just want communicate with the cmd, it shouldn't matter what kind of application is running in it, since the cmd should handle that. That's why I want to remote control the cmd process and won't just start the application itself. – MewesK Aug 17 '11 at 12:13
  • I don't know how much experience you have it is for example absolutely possible that MySQL (or anything else) is implemented in such a way that it circumvents STDIN (for whatever reason, be it security or anything else)... in such a case command couldn't "handle" what you expect... – Yahia Aug 17 '11 at 12:34
  • But in the end every console application has to write to and read from the console buffer. That's why I want access the console buffer directly. If the cmd.exe can print it, I should be able to grab it (which actually works quite easy), and if the keyboard can input stuff to the cmd.exe so should I be able to do. – MewesK Aug 17 '11 at 13:08
  • it is not true that every console app has to write and read from the console buffer - nearly all will write so grabbing shouldn't be a problem usually, but some (a minority!) will read keystrokes from a "lower level" and ignore the console buffer - then you would have to resort to that level to input something into that app... – Yahia Aug 17 '11 at 13:14
  • Okay in that case I will ignore those cases for know. Do you an example so I know how to test those cases? – MewesK Aug 17 '11 at 13:21
  • I don't have any lists or so esp. since the behaviour can even be different in different versions of the same app... that's why I wrote there is some trial&error in getting whatever you want to get when using this approach... – Yahia Aug 17 '11 at 13:24