Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i use WriteFile or WriteConsole?

Status
Not open for further replies.

ctlin

Technical User
Apr 17, 2002
77
US
so i need to send in some input to a console-style app that will be running hopefully hidden in the windows background. SendKeys is what i've been using when running the console normally, but i'm not sure how to get it to type to hidden processes(started with Shell, option '0') so i think i may need to look at using handles or pipes. that's where you guys come in.

based on my limited searching skills(MSDN reference library) and a way-over-my-head program strongm was nice enough to put together for me, i have come to the conclusion that WriteFile or WriteConsole may be the answer.

the question is: can i just send the handle returned from my Shell call to the WriteFile and have it write to that hidden process? something like:

consolehandleID = Shell("c:\myconsoleapp.exe", 0)
WriteFile(consolehandleID, &quot;somestring&quot;, other_variables_i_can't_decypher) ' <--- this is where i'm unclear
 
Um, the code I presented earlier should have given you this ability. Was there a problem with it? Or are you trying to achieve something completely different?
 
heheh i was hoping you would read this. no the code is great. i want to figure out exactly what everything does(maybe this is the problem) and am having some problems identifying what some things do in the program.

this is the main i am using now:
----
Private Sub Command1_Click()

MakeBatchFile
mvarConsoleAllocated = False ' Global flag indicating whether VB has a console allocated or not
InheritableConsole &quot;C:\WINNT\system32\notepad.exe&quot;
Sleep 500 ' Give new console a chance to respond
WriteIOConsole &quot;blah blah&quot;

End Sub
-------

when i run this, the notepad window pops up, but nothing is typed into it. also a command window running vb6.exe is opened. is this code command-line-program specific? my guess is that it has the handle to the command window and not notepad. i can't find where the code opens the command line window as opposed to just the notepad window. i don't have a command line program executable to try yet, but if i ran a program that opened inside the command window, this program will work right?

I suspect the source of my confusion is somewhere inside the StartProcess function which contains the CreateProcessA call. I wanted to try using Shell because it seemed simpler but if you can help me adapt this code, that would be great too.
 
No, the confusion is over consoles. Notepad is not a console application, it is a GUI application. Therefore it doesn't have a console handle, nor any of the standard streams.

I should also point out that the &quot;command window running vb6.exe&quot; is not actually quite that. It is a console window that is owned by the current VB program; it merely exists so that it can be inherited by any console application (see below) that you launch from the VB program.

To try and hopefully clarify slightly some of your problems (which I suspect derive from the terminology I use, so perhaps are my fault): a console application is an application that has what you call a command window and, generally, uses that command window as it's sole means of accepting input and providing output.

 
haha maybe i should have said the source of my confusion is programming. fair warning to everyone: i'm not a programmer and have only been doing VB for a few weeks, but am trying to learn.

anyways, i think what i will eventually have at my disposal is indeed a console application and will be able to work with your program.
 
with the createprocessA call, can i make it so that the console window is hidden so that the program runs completely in background - some parameter i throw in? will this still allow me to send text to it from the GUI? i've tried looking for the createprocess method on MSDN reference library but i couldn't find anything for VB. Thanks
 
It get's more complicated than it already is...Instead of using AllocConsole to give VB a console (the easy way), you actaully need to use CreateProcess to allocate a console, using a bunch of specific flags (the complicated way).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top