I am working on a project that requires me to run an old 16bit
executable from within my VB.Net application. Could someone please
help me with some code to do this? Here are the details:
1. VB.Net application builds the source file for the old executable.
2. Then I need to run the executable.
- The command window opens and there is a prompt for input.
3. Input the source file name.
- After the source file name is entered and you press enter, another
prompt comes up.
4. Then Input the output file name.
5. Then the executable does its thing and generates the output file.
6. Close the old application.
...
I would prefer an answer written in VB.Net. Any help would be greatly appreciated! |
Request for Question Clarification by
studboy-ga
on
16 Mar 2004 14:46 PST
Hi tiewire-ga
I need a little more clarification--
1) What's the format of the source file? I need to understand
what exactly is this "old executable" and what does it do.
2) If the VB.Net application builds the source file, then why
do you need to enter the source file name? It builds it, it knows it, no?
I assume the "old executable" takes the names of an input file and an old put file:
oldexecute infile outfile
Is that right?
|
Clarification of Question by
tiewire-ga
on
16 Mar 2004 16:16 PST
1. The format of the source file is .txt
2. The 'old executeable' takes a the source file and uses its contents
to perform calculations and generate an output file that my
application will use to generate reports. My application is basically
a user friendly wrapper for the old console application.
The way the console application works is: When you run the .exe file
the console window comes up. There is a line requesting the name of
the source file. You enter the name of the source file and press
enter. Then another line comes up asking for the name of the output
file. You then enter the name of the output file and press enter
again. The program then runs and produces the output file in the same
directory as the .exe file.
My application performs some preliminary calculations, currently being
done by hand, and updates the input file. What I need is to have my
application run the old executable, input the source file name and the
output file name for use by the old executable, have the old .exe
generate the output file, and close the old app. Once that's done,
I'm good to go.
|
Request for Question Clarification by
studboy-ga
on
17 Mar 2004 08:24 PST
OK, I understand now. You already have the code which does the
preliminary calculation and generate the input file. All you need
from me is the code to execute/call the binary. I'm working on it.
|
VB.NET makes it easier to execute an external program
and controls it as an "interactive" process.
First, please read this execellent article on the
subject:
http://www.devx.com/dotnet/Article/7914
What you're looking for is described on page 5
of the article. In your case, the code will be
something like the following:
----------------------------------------------------
Imports System
Imports System.Diagnostics
Dim myProcess As Process = New Process()
myProcess.StartInfo.FileName = "16bitbinary.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()
Dim sIn As StreamWriter = myProcess.StandardInput
sIn.AutoFlush = True
Dim sOut As StreamReader = myProcess.StandardOutput
Dim sErr As StreamReader = myProcess.StandardError
sIn.Write("inputfile.txt" & System.Environment.NewLine)
sIn.Write("outputfile.txt" & System.Environment.NewLine)
If Not myProcess.HasExited Then
myProcess.Kill()
End If
sIn.Close()
sOut.Close()
sErr.Close()
myProcess.Close()
----------------------------------------------------
The program basically fires up the binary, enter the
input filename and hit enter (newline), then enter
the outfile filename and hit enter. The rest is history. |
Clarification of Answer by
studboy-ga
on
17 Mar 2004 13:07 PST
Better yet, run it through cmd.exe:
----------------------------------------------------
Imports System
Imports System.Diagnostics
Dim myProcess As Process = New Process()
myProcess.StartInfo.FileName = "cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()
Dim sIn As StreamWriter = myProcess.StandardInput
sIn.AutoFlush = True
Dim sOut As StreamReader = myProcess.StandardOutput
Dim sErr As StreamReader = myProcess.StandardError
sIn.Write("16bitbinary.exe" & System.Environment.NewLine)
sIn.Write("inputfile.txt" & System.Environment.NewLine)
sIn.Write("outputfile.txt" & System.Environment.NewLine)
If Not myProcess.HasExited Then
myProcess.Kill()
End If
sIn.Close()
sOut.Close()
sErr.Close()
myProcess.Close()
----------------------------------------------------
|
Clarification of Answer by
studboy-ga
on
17 Mar 2004 13:09 PST
Or even:
myProcess.StartInfo.FileName = "cmd.exe"
myProcess.StartInfo.Arguments = "/C 16bitbinary.exe "
and then
sIn.Write("inputfile.txt" & System.Environment.NewLine)
sIn.Write("outputfile.txt" & System.Environment.NewLine)
as before
|
Request for Answer Clarification by
tiewire-ga
on
17 Mar 2004 17:00 PST
Sorry studboy, but no dice:-(
The info in that article put a few new twists on what I have already
tried, but it still doesn't work. The sendkeys method mentioned in
the article was the most productive. Redirecting IO streams didn't
work at all. I tried the following code and I, at least, was able to
have it send something to the console window. Unfortunately, instead
of sending the string defined in the code, it simply sent "/"
characters.
Dim myProcess As New Process
myProcess.StartInfo.FileName = "cmd.exe"
myProcess.StartInfo.Arguments = "/K c:\test\16bitbinary.exe"
myProcess.StartInfo.UseShellExecute = True
myProcess.StartInfo.WindowStyle = _
ProcessWindowStyle.Normal
myProcess.Start()
If myProcess.Responding Then
Windows.Forms.SendKeys.SendWait("test.txt")
Else
myProcess.Kill()
End If
Using this code, the console window opens and displays the usual line
requesting the input file. Then, it displays a "/" character on 7
different lines as if I had hit that key and enter 7 times.
Do you know what is causing it to behave like this?
|
Clarification of Answer by
studboy-ga
on
17 Mar 2004 18:54 PST
Hi tiewire-ga
This is very interesting--some observations:
1) The IO stream method did not work for you.
2) The SendKeys method sends something you did not send.
My guess is there's something strange with the old 16bitbary--
perhaps something in there that was causing the problem.
Can you try delaying the response?
myProcess.WaitForInputIdle(1000)
Also, what's inside test.txt?
I wish there's some way you can send me the 16bitbinary so I can try it
over here (maybe upload it to a free website like geocities.com? if
it's not confidential...)
|
Clarification of Answer by
studboy-ga
on
17 Mar 2004 19:16 PST
Just as a thought, also try command.exe instead of cmd.exe
and SendKeys to cmd.exe without the arguments--
ie, use SendKeys to send 16bitbinary as well.
|
Request for Answer Clarification by
tiewire-ga
on
19 Mar 2004 09:50 PST
studboy,
Tried all that stuff already with no luck. Any more suggestions?
|
Clarification of Answer by
studboy-ga
on
19 Mar 2004 13:51 PST
Hi tiewire-ga
Without having your binary in front of me I wouldn't be able to debug
it from here. Now, the problem *could* be with your
computer/installation and not the 16bitbinary. Try this:
1) Try a different binary, or just send cmd commands like dir to see
if it works on *this* particular computer.
2) Then try it on a different computer--perhaps one with the same
and/or different OS version.
Let me know how it turns out. Also, please tell me if the symptoms
are always the same--ie, what happen when you do the IO stream in each
case? Does the "/" always show up for SendKeys in each case? etc.
and etc. I'm sure we will get to the bottom of this in due time.
|