Get values from .exe in "stdout"

antifashionpimp

Registered User.
Local time
Today, 17:43
Joined
Jun 24, 2004
Messages
137
Hi,

Please help me as I am a newbie in VB, pretty much in all programming.
I searched many forums before, but do not know exactly what keywords to use. I hope my question makes sense. :)

I have the following Sub, which calls an .exe program.

Code:
Private Sub cmdCalc_Click()
On Error GoTo Err_cmdCalc_Click

    Dim stAppName As String
    Dim stArgument As String

    stAppName = "C:\Program Files\prog.exe"
    stArgument = " " & Me.cboAge& " " & Me.cboSex
    Call Shell(stAppName & stArgument, 1)

Exit_Calc_Click:
    Exit Sub

Err_cmdCalc_Click:
    MsgBox Err.Description
    Resume Exit_cmdCalc_Click
    
End Sub

Now what I need, is for the value which is returned by the .exe program to be captured by Visual Basic. I normally run this program in the command prompt, with the arguments, and when pressing Enter, the answer is displayed on the following line in the command prompt.

Does this involve reading the stream "stdout". If yes, how do I do it in Access VB?



Regards,
J
 
A simple solution to your problem, pipe the result from the exe call to a text file open the text file and read the value.

ie
Code:
 Shell(stAppName & stArgument "> Outpath\outfile.txt", 1)
 open "Outpath\outfile.txt" for input as #1
 .
 .
 .
 
Thanks for the reply

I have tried this, but it does not work!

Strange thing is, when I copy the "Pathname" variable of the Shell function (i.e. C:\Progra~1\Prog.exe 29 Male >> c:\temp\File1.txt ) and paste it in the command prompt, the Text file is created! Only when doing this through Access, it doesn't. Am I missing something here?

Here is the faulty line in Access VBA:

Code:
Call Shell("C:\Progra~1\Prog.exe 29 Male >> c:\temp\File1.txt", 1)


Thanks in advance!
 
Strange i swear i tested the code before posting and it worked fine, now i go back and test again and it doesnt work :confused: to solve the problem i have revised my syntax below to call a command propt to run the command in.


Code:
Shell Environ("comspec") & " /c [your program name and path] [vars] >[outfile name and path]", vbHide

eg, the example below works just copy into you immediate window and run to test.

Shell Environ("comspec") & " /c ping.exe /? >c:\file1.txt", vbHide





note you only need 1 ">", and you dont need to "CALL" a function.



if you are still haveing problems let me know what OS you are running on but the code above hase been tested on xp and 98 so should work without any problems.

:cool:
 
this is really strange!!

I get back to my PC this morning, and the program decides to not work anymore.

When I run the program with Environ("comspec") ...etc. I get an error that says that the program cannot find the .ini that is associated with this program. :confused:

However, when I run the program from the command prompt, inside the directory where the .exe AND .ini files found, it works. I.e. as follows:

C:\Program Files\MyProgram\bin>C:\WINDOWS\system32\cmd.exe /c C:\Progra~1\MyProgram\bin\prog.exe [arguments]

This did not happen before, I can not think what has happened now! Must I then always run the program only when I am inside the program's directory?

Thanks for the help once again. :)
 
What ini file cant it find?
When does the error accure? on the shell line or inside prog.exe?
What is the exact error code/message given?

is its an ini file accociated with your prog.exe or one accociated with cmd or Environ()

if its your prog.exe then you will probably need to edit the code on the ini calls to reflect the path properly.


Assuming the ini file is in the dir of the exe

for VB,

app.path & "\myini.INI"

or for Access VBA,

Currentproject.path & "\Myini.ini"

both will return the string

C:\Progra~1\MyProgram\bin\myini.ini


hope this helps


:cool:
 
Hi shadez,

Sorry, my last post was a bit inspecific. I found the solution while replying, your questions made me think of where to look for the error before asking again for help. :D

Problem:
The error message:

<nt_init> EnvironmentInit: Can't open Ini-File C:\prog.ini!!
<nt_init.c> RSNT_init: Error in EnvironmentInit(C:\prog.ini)

is displayed as the output of stdout, in this case inside the text file. When I leave out the part which sends the output to the text file
( > "C:\....\Output.txt" ) then I get the error message in the cmd line.

The .ini is associated with the prog.exe file. It is in the same directory as the prog.exe. I also changed the PATH environmental variable to reflect this directory, but no luck.

What does work however, is when I run prog.exe in the cmd line, after changing the directory with:

cd Progra~\Prog1

Solution:

I added the following line to the beginning of my event handler, to change the directory where all this executes:

ChDir "C:\Progra~1\Prog.exe"

Thanks once again for your help!

Regards,
Jean
 

Users who are viewing this thread

Back
Top Bottom