ExecuteAndWait



I've been using this sub for quite a while and suddenly it just stopped working..
dunno what caused it, might be a faulty commandline I'm sending..
what I would like the sub to do is start a program called bmconv.exe which is located in c:\MyProg\Ext\ the commandline would be extract.txt, this file is located in WINTEMPFOLDER\prog\themes\
WINTEMPFOLDER beïng c:\..., the tempfolder of windows :)

so what do I use to (try to) make it work
Call ExecuteAndWait ("c:\MyProg\Ext\bmconv.exe", "WINTEMPFOLDER\prog\themes\extract.txt")

The code below is pasted in a module:
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
Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadID As Long
End Type
Global Const NORMAL_PRIORITY_CLASS = &H20&
Global Const INFINITE = -1&

Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As long, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long


the code below is the sub:
Public Sub ExecuteAndWait(cmdline$, Optional SDir$)
    Dim NameOfProc As PROCESS_INFORMATION
    Dim NameStart As STARTUPINFO
    Dim x As Long

    NameStart.cb = Len(NameStart)
    If SHOWMODE = 0 Then
        NameStart.dwFlags = 1
        NameStart.wShowWindow = 0
    End If
    x = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, SDir$, NameStart, NameOfProc)
    x = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
    x = CloseHandle(NameOfProc.hProcess)
End Sub


I hope someone can help me out here,
thanks in advance
TheWiepAsked:
Who is Participating?
 
Mark_FreeSoftwareCommented:
try this:

"""c:\My Prog\Ext\bmconv.exe"" ""WINTEMPFOLDER\prog\themes\extract.txt"""
(in vb)

in cmd use this:

"c:\My Prog\Ext\bmconv.exe" "WINTEMPFOLDER\prog\themes\extract.txt"
0
 
justchat_1Commented:
open the command prompt...
navigate to WINTEMPFOLDER\prog\themes\extract.txt
try running c:\MyProg\Ext\bmconv.exe

Are there any problems doing that?
0
 
TheWiepAuthor Commented:
not sure how you want me to execute bmconv without the parameters (extract.txt)
as you see I don't know that much about cmd :)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
justchat_1Commented:
Isnt sdir$ the directery that the program is started in, not the parameters?...
0
 
TheWiepAuthor Commented:
oops sorry,
the original line has lots of strings which would make it harder to follow so I changed with another path
and I made a mistake.. sorry
it has to be like below.

Call ExecuteAndWait ("c:\MyProg\Ext\bmconv.exe WINTEMPFOLDER\prog\themes\extract.txt", "WINTEMPFOLDER\prog\themes\")
0
 
justchat_1Commented:
Thats better...
try start->run->"c:\MyProg\Ext\bmconv.exe WINTEMPFOLDER\prog\themes\extract.txt"
Does that work?
0
 
Mark_FreeSoftwareCommented:
does this code work?:



Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&

' Start the indicated program and wait for it
' to finish, hiding while we wait.
Private Sub ShellAndWait(ByVal program_name As String, ByVal window_style As VbAppWinStyle)
Dim process_id As Long
Dim process_handle As Long
    ' Start the program.
    On Error GoTo ShellError
    process_id = Shell(program_name, window_style)
    On Error GoTo 0
    ' Hide.
    Me.Visible = False
    DoEvents
    ' Wait for the program to finish.
    ' Get the process handle.
    process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
    If process_handle <> 0 Then
        WaitForSingleObject process_handle, INFINITE
        CloseHandle process_handle
    End If
    ' Reappear.
    Me.Visible = True
    Exit Sub
ShellError:
    MsgBox "Error starting task " & _
        txtProgram.Text & vbCrLf & _
        Err.Description, vbOKOnly Or vbExclamation, _
        "Error"
End Sub
0
 
EDDYKTCommented:
may be

Try this

Dim WshShell

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "Your full path file name", 6, true
Set WshShell = nothing
0
 
TheWiepAuthor Commented:
I tried with run as justchat_1 suggested and it didn't work

I then "figured out" that the problem was a space in the path
when I remove the space from the path it works flawless
so.. now all I need is a way to use ExecuteAndWait with spaces

this works
start->run->"c:\MyProg\Ext\bmconv.exe WINTEMPFOLDER\prog\themes\extract.txt"

this doesn't (as there is a space between My and Prog)
start->run->"c:\My Prog\Ext\bmconv.exe WINTEMPFOLDER\prog\themes\extract.txt"

thanks for the replies already
0
 
TheWiepAuthor Commented:
thanks a lot
that did the trick
I used Chr(34) as I find it easier on the eye :)

thanks all for the help
gonna play around with it and add the Chr(34) everywhere
0
 
Mark_FreeSoftwareCommented:

thanks for the points, and happy coding

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.