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

Shell a *.exe and use some input files

Status
Not open for further replies.

DaTommyboY

Programmer
Feb 10, 2004
40
NL
Hello everybody,

I use the code from Hypetia in tread 222-859655 (last post of that treat from Hypetia).
What I like to do is to shell two programs after each other.
The first program gets input data from two *.txt files, stored in the same folder, the second program gets his input data from the output of the first program whith is also in a *.txt file in the same folder.
Starting by hand works lika a charme, but with shell I get a error from the program that he likes to read past the end of file!!!
Is it possible that shell "runs" the program in an other folder than were it is safed?

Any helps is welkom,

Tom
 
Hello everybody,

I found something:
The shelled program is looking fore it's input files in the folder were the VB program is running, and not in the folder were the *.exe program is saved.

Anyone an idea of how it force the shelled program to look in the right folder.

Tom
 
thread222-859655

Steve

everyday something new is learned, hmmm...
 
can u pass the file path to the shelled program?
maybe give it some kind of apppath "..\..\*.txt" parameter?



Steve

everyday something new is learned, hmmm...
 
im sorry... looks like ur passing the actual path to the *.exe here:

Code:
Function ShellWait(strCmdLine As String,...

unless im mistaken, in form load when u pass the path to the function, send the entire path

Code:
ShellWait "c:\myfolder\my.exe", vbMinimizedNoFocus

have u tried that?

Steve

everyday something new is learned, hmmm...
 
Hello sramey00,

The code you suggested, is exact what i do in the Public Sub I call every time I like to run those two program's.

So thats not the solution to my problem, Iam sorry ;-)
Please feel free to give it another shot, I will be thankfull if something usefull will come out!!

Tom
 
Had a similar problem with a program that was looking for it's ini-file.
here's what I did:
saved the current folder
changed to the folder of the shelled exe
removed the path from the shellwait-parameter
after shellwait changed back to the original folder
 
Hi fheyn,

I don't get your solution. They only path I use is the path of the folder where the shelled exe is stored??
So which path do I have to change and were??

Thanks in advance

Tom.
 
post ur code

Steve

everyday something new is learned, hmmm...
 
Hi sramey00,

This is my code,

Put this under a commandbuttom on a form:
Code:
PING = C:\Batchfile.bat
ShellWait PING, vbNormalFocus
This in a modul:
Code:
Option Explicit

Public PING as string

Private Type STARTUPINFO
 cb As Long
 lpReserved As String
 lpDesktop As String
 lpTitle As String
 dwX As Long
 dwY As Long
 dwXSize As Long
 dwYSize As Long
 dwXCountChars As Long
 dwYCountChars As Long
 dwFillAttribute As Long
 dwFlags As Long
 wShowWindow As Integer
 cbReserved2 As Integer
 lpReserved2 As Long
 hStdInput As Long
 hStdOutput As Long
 hStdError As Long
End Type

Private Type PROCESS_INFORMATION
 hProcess As Long
 hThread As Long
 dwProcessID As Long
 dwThreadID As Long
End Type

Const STARTF_USESHOWWINDOW = &H1&
Const NORMAL_PRIORITY_CLASS = &H20&
Const INFINITE = -1&

Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

'--- Shells the passed command line and waits for the process to finish
'--- Returns the exit code of the shelled process
Function ShellWait(strCmdLine As String, Optional ShowCommand As VbAppWinStyle = vbNormalFocus) As Long
  Dim udtProc As PROCESS_INFORMATION, udtStart As STARTUPINFO

  'initialize the STARTUPINFO structure
  udtStart.cb = Len(udtStart) 'size
  udtStart.dwFlags = STARTF_USESHOWWINDOW 'uses show window command
  udtStart.wShowWindow = ShowCommand 'the show window command

  'Launch the application
  CreateProcess vbNullString, strCmdLine, ByVal 0&, ByVal 0&, 0, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, udtStart, udtProc

  'Wait for the shelled application to finish
  WaitForSingleObject udtProc.hProcess, INFINITE

  'get the exit code
  GetExitCodeProcess udtProc.hProcess, ShellWait

  'close handles
  CloseHandle udtProc.hThread
  CloseHandle udtProc.hProcess
End Function
Make a batchfile with the next code, it will ping a connection with another pc, it probebly will fail, but it will make a file with the name ERRORMESSAGE.txt, with the errorlevel of the dos operation.
Code:
@echo off
PING 10.145.10.225 | FIND "TTL" > NUL
FOR %%A IN (1 10 100) DO SET ERR%%A=
FOR %%A IN (0 1 2) DO IF ERRORLEVEL %%A00 SET ERR100=%%A
IF %ERR100%==2 GOTO 200
IF %ERR100%==0 IF NOT "%1"=="/0" SET ERR100=
FOR %%A IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL %ERR100%%%A0 SET ERR10=%%A
IF "%ERR100%"=="" IF %ERR10%==0 SET ERR1O=
:1
FOR %%A IN (0 1 2 3 4 5) DO IF ERRORLEVEL %ERR100%%ERR10%%%A SET ERR1=%%A
IF NOT ERRORLEVEL 250 FOR %%A IN (6 7 8 9) DO IF ERRORLEVEL %ERR100%%ERR10%%%A SET ERR1=%%A
GOTO End
:200
FOR %%A IN (0 1 2 3 4 5) DO IF ERRORLEVEL 2%%A0 SET ERR10=%%A
IF ERR10==5 FOR %%A IN (0 1 2 3 4 5) DO IF ERRORLEVEL 25%%A SET ERR1=%%A
IF NOT ERR10==5 GOTO 1
:End
SET ERRORLEV=%ERR100%%ERR10%%ERR1%
FOR %%A IN (1 10 100) DO SET ERR%%A=
ECHO ERRORLEVEL  %ERRORLEV%>ERRORMESSAGE.TXT

if it all runs like it does by my, you will see the responce of the batchfile is in het folder where you run your VB program from, and not where you stored the batchfile.
And that is not what I like!!!!

Hope you can get some solutions,

Thanks Tom
 
Add your path required in the batch file last line:

ECHO ERRORLEVEL %ERRORLEV%>C:\myPath\ERRORMESSAGE.TXT

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Okee thanks, that solves one problem.
The other problem is how to force the shell command to look in the right folder for a input file.
situation:
I have my VB program, called Invoer.exe and it is safed in C:\X\YThe Invoer.exe shells a program called Calculate.exe which is stored in H:\ZTo run Calculate.exe it needs a input file, this file is also in H:\Z\.
But the shell function is looking for the inputfile in the C:\X\Y\, because INvoer.exe is running from there. Also the output files of Calculate.exe will be stored in C:X\Y\.
It looks to my that the shell function runs the Calculate.exe in C:\X\Y\ and not in H:\Z\.

And that is not what I like to have. So my question is: How can i force the shell function to run Calculate.exe from H:\Z\, so that the in and out put files are in the right folder???

Thanks in advance

Tom
 
well, here's what I meant:

NewPath : there's the called exe with it's input file(s)

CurrentDir = CurDir()

Call ChDir(NewPath)

ShellWait(....)

Call ChDir(CurrentDir)

hope this helps
 
i've tested your code, and i can only open *.exe files. ive been able to open notepad.exe with full path name, but i wasn't able to open a *.txt file. i opened calc.exe with a full path name also. those two executables were in my winnt folder. then i linked to a *.exe file located in my program files directory. worked fine.
the only problem i had was linking to files that weren't *.exe. i believe theres a setting that needs to be changed or u need to use a different function.
this is only a theory (just cuz i dont know any better) but can't a process only be a *.exe? not an instance of a program such as *.txt?
maybe you will have to open cmd.exe then call ur *.bat file from the command line through code

Steve

everyday something new is learned, hmmm...
 
You can shell to RunDLL.exe like this:

In a code module:[tt]
Public Sub OpenDocument(DocumentWithPath As String)
Shell "RUNDLL32.EXE URL.DLL, FileProtocolHandler " & DocumentWithPath, vbNormalFocus
End Sub
[/tt]
Then call from wherever:
[tt]
OpenDocument("c:\fred.txt")[/tt]

or you can use the ShellexecuteEx API. For details do a search in this forum for ShellExecuteEx or look here:

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Hi johnwm,

It looks that I am a litte stupid :-(
My vb prog keeps telling me, while highlighting "shell", which is in a module that
*****************
Compile Error:
Expected variable or procedure, not module
*****************
Looks like it doesnt know the command Shell, also Shellexecute and shellexecuteex doesnt work.

Probebly my shortcomming of VB experience,

Thanks
 
if im not mistaken, u have to declare the shellexecute function at the top of ur code

Code:
'just under option explicit
option explicit
Private Declare Function ShellExecuteEx Lib "shell32.dll" (ByRef lpExecInfo As SHELLEXECUTEINFOA) As Long

then u can create a function or sub and call it with you document path

Code:
sub opendocument(filename)
    
end sub

i was looking at the site johnwm provided (about shell execute) click on that code link and use the function in their example. that should work

Steve

everyday something new is learned, hmmm...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top