sending data a dos app

Matt_h

Member
Joined
Apr 12, 2007
Messages
17
Programming Experience
Beginner
I have written a small app that basicly builds a file that is then required to be processed by a dos app. I have used the following code to call the app:

dim opsi as new processStartInfo(exepath)
dim oprocess as New Process

opsi.useshellexecute = false
opsi.redirectstandardinput = true
opsi.workingdirectory = IO.Path.GetdirectoryName(exepath)

oprocess = process.start(opsi)
oprocess.standardinput.writeline(inpfile)
oprocess.waitforexit()

Now when I run this in Visual Studio, all is well and the file gets processed, however, now I have come to distribute the app I run into the following problems:

* When using any of the .exe files created in the bin directory it appears that the Dos program either isn't called ( a dos box appears briefly then shuts down) or the dos box doesn't receive the file name I am sending. This happens if I also run my programs exe from obj/debug and obj/release directories.

* If I change the redirectstandardinput to use shellexecute then the dos program is called and appears on screen waiting for the name of the file to be processed, but I can't seem to work out how to send the filename this way as the dos program doesn't accept arguments.


Initially I thought it was a problem with the machine I am passing the executable to but it also does the same on my development machine. What am I doing wrong?

regards,

Matt
 
Parameters to console applications to be processed when they start is passed in the command line: like console.exe "c:\the path\filename.txt"
for ProcessStartInfo such parameters are set in Arguments property, in console application they are read from Environment.GetCommandLineArgs

StandardInput issed to to pass info to a running console when it is waiting for user to write something, the application is then doing one of the Console.Read methods.
 
JohnH,

Thanks for the reply.

So what you are saying is to write a console app just to call the dos appplication, then using the arguments that should pass the text file to the console app. Yes?

I have tried starting the dos app supplying command line arguments but it does nothing, the program just sits there waiting for a filename.

regards,

Matt
 
A "dos application" is a console application. How your console works I don't know, but you interact with it programmatically the same as you otherwise interact with it. I was simply telling the difference between startup parameters, which is how console applications in most cases is operated, and console input during runtime of the console.
 
John,

Thanks for reply - sorry I don't really follow, am new to this.

The DOS app is an old Fortran Program that requests the name of an input file, before processing it. The Fortran program does not accept command line arguments.

I have written a VB.net front end that builds the file, then calls the DOS app, sends the filename to it then waits for the output from the Dos app before processing the data into an excel spreadsheet.

When I run my front end in the VS 2003 IDE everything works fine, however, when I run the executables it appears as though either the dos app doesn't open correctly or the filename isn't passed to the dos app.

I don't know why it fails when I run the exe.

If I alter the above code to start the process and not send the filename, the DOS program is opened and awaits the user enterring a filename, once entered it processes the file as required.


regards,

Matt
 
So you were right on track with the code in first post after all :) You had some concern about useshellexecute also, this must be False as set. As far as I know, the initial code should work for its purpose, it does when I run such application, so I'm at a loss what goes wrong with yours.
 
John,

Thanks for your response again...

Am still stuck with this.

I have tried the following:

dim myprocess as new process
dim mystring as string = "C:\WINNT\system32\CMD.EXE")

myprocess.startinfo.filename = mystring
myprocess.start

send_dat("cd\")
send_dat("cd d:\matt_test\progs")
send_dat("mybatch")
send_dat("myfile.inp")

with send_dat being:

sub send_dat(byval line_txt as string)
appactivate("C:|WINNT\system32\CMD.EXE")
dim s as string
s=line_txt
sendkeys.send(s & vbcrlf)
sendkeys.flush()


I found this code on another forum and it works, well nearly works. It calls the dos cmd prompt, moves to the correct directory, runs the batch file and then when the program requests a file name instead of send ing the filename it sends a \ character.

Does anyone have any ideas as to how to solve this?

regards,

Matt
 
First you say Fortran Program and now a batch? A batch file is a executable script that may consist of calls to many applications among other processing. While I don't have any problems with running a console application through a batch file and sending it strings, this may have unknown implications about interaction. How does this batch look like? Also, what does this fortran/dos/batch do? Maybe VB.Net can do it also?
 
I eventually got ths sorted.

All that is required is the line

opsi.redirectstandardoutput = true

I assume that when the input is redirected it redirects the output, so you have to redirect the output back to it's original place.

Thanks for your help.

regards,

Matt
 
Redirection of standardinput/output is not related programming-wise, but you may have interaction we're not aware of. I didn't need to redirect output when I ran my input test here.
 
Back
Top