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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to invoke an exe from VB,pass parameters,Store results

Status
Not open for further replies.

indiaa123

Programmer
Aug 27, 2003
25
US
I need to invoke an exe from VB. I have to pass some parammeters to run the exe. The exe is giving a 16 digit number as output i need to store the result in a output file.
Is there any feasible solution.

Thanks
 
Look at the "Shell" function. The big caveat is that the shelled process runs asynchronously, and the shell call returns immediately, so you need to have a way to know that the shelled process has completed before you try to get results back from it.

One way I've accomplished this is to write to "FILENAME.TMP" during the shelled EXE, and then rename the file to "FILENAME.OUT" as the last thing the shelled EXE does. Then, you just watch for the "FILENAME.OUT" to appear, and you'll know the shelled process has completed. If the shell starts again and finds "FILENAME.OUT" you can either wait for the file to go away, or kill the old file and create a new one.

Of course, there are prettier ways to do it using APIs, but I've used the rename trick for handshaking between asynchronous processes (often to coordinate mainframe and PC applications) in a lot of different circumstances with good success rates, regardless of operating systems, programs, or whatever. Rename is an OS-based function, and it's fast on all types of systems.

If the EXE sends to STDOUT, then pipe to a file, and shell a batch file that does a rename call after the EXE ends.

DEL outfile.out >NUL
MYPROG.EXE inputstring >outfile.tmp
RENAME outfile.tmp outfile.out


Howard
 
forgot to mention -

to shell a batch file, execute(shell) "COMMAND /Cbatchfile.bat"

hd
 
I tried like this

filepath = str_srcfoldername & ofile.Name
Call Shell("c:\md5.exe &filepath>c:\s1.txt")

BUt no fruitful results.

Any other ways of getting the solution using windows API.

Let me know

 
intResult = WshShell.Run("notepad.exe", 1, True)
msgbox intResult
 
How to pass a parammeter using the above function to run the exe.

Thanks
 
intResult = WshShell.Run("notepad.exe c:\test.txt", 1, True)
msgbox intResult

'just put a space and the para list. it is just like you are typing something at the command prompt i guess
 
NO it is not working...any other solution.

Thanks
Suresh
 
the solution i have presented, along with the accompanying forum thread will work fine. post your code and i will take a look.
can u test by creating a vbs file with the following in it

wscript.quit 888

then in you vb app have

intResult = WshShell.Run("c:\return888.vbs", 1, True)

msgbox intResult
 
The code which i have created is

Dim RetVal As Long
Dim WSH As IWshShell
Dim WaitForTermination As Boolean
Dim CommandLine As String


Private Declare Sub ExitProcess Lib "kernel32" (ByVal ReturnCode As Long)


Private Sub Command1_Click()
Dim oFso As New FileSystemObject
Dim oFolder As Folder
Dim ofile As File
Dim str_srcfoldername As String
Dim str_dstfoldername As String
Dim int_totalcount As Integer
Dim int_converted As Integer
Dim int_pgbarunit As Integer

Set WSH = New IWshShell_Class

Open "c:\testing.txt" For Output As #1
str_srcfoldername = c:\inputSet oFolder = oFso.GetFolder(str_srcfoldername)
MsgBox (str_srcfoldername)
For Each ofile In oFolder.Files
Print #1, ofile.Name
WaitForTermination = True
filepath = str_srcfoldername & ofile.Name

CommandLine = "c:\md5.exe &filepath"
RetVal = WSH.Run(CommandLine, vbNormalFocus, WaitForTermination)
MsgBox "Process returned: " & RetVal



Next ofile
Set WSH = Nothing
Close #1
End Sub
 
CommandLine = "c:\md5.exe &filepath"
shuld be
CommandLine = "c:\md5.exe " & filepath
 
I am having some other problems in the following line

Dim WSH As IWshShell
 
i am not that good with VB so i didnt know about the Set WSH = New IWshShell_Class. i will use it in future myself as no doubt it doesnt eat as much memory as what i have done in the past which is...

Set WshShell = CreateObject("WScript.Shell")

or perhaps it makes no difference
 
CAn you just post the full correct code once again.

I know that i am bugging for long....

Thank you
 
Dim RetVal As Long
Dim WSH
Dim WaitForTermination As Boolean
Dim CommandLine As String


Private Declare Sub ExitProcess Lib "kernel32" (ByVal ReturnCode As Long)


Private Sub Command1_Click()
Dim oFso As New FileSystemObject
Dim oFolder As Folder
Dim ofile As File
Dim str_srcfoldername As String
Dim str_dstfoldername As String
Dim int_totalcount As Integer
Dim int_converted As Integer
Dim int_pgbarunit As Integer

Set WshShell = CreateObject("WScript.Shell")

Open "c:\testing.txt" For Output As #1
str_srcfoldername = c:\inputSet oFolder = oFso.GetFolder(str_srcfoldername)
MsgBox (str_srcfoldername)
For Each ofile In oFolder.Files
Print #1, ofile.Name
WaitForTermination = True
filepath = str_srcfoldername & ofile.Name

CommandLine = "c:\md5.exe " & filepath
RetVal = WSH.Run(CommandLine, vbNormalFocus, WaitForTermination)
MsgBox "Process returned: " & RetVal



Next ofile
Set WSH = Nothing
Close #1
End Sub
 
sorry line

"Set WshShell" should be "Set WSH"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top