?
Solved

ExecuteAndWait

Posted on 2006-07-06
11
Medium Priority
?
932 Views
Last Modified: 2008-01-09


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
0
Comment
Question by:TheWiep
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 9

Expert Comment

by:justchat_1
ID: 17053061
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
 

Author Comment

by:TheWiep
ID: 17053428
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
 
LVL 9

Expert Comment

by:justchat_1
ID: 17053463
Isnt sdir$ the directery that the program is started in, not the parameters?...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:TheWiep
ID: 17053564
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
 
LVL 9

Expert Comment

by:justchat_1
ID: 17054196
Thats better...
try start->run->"c:\MyProg\Ext\bmconv.exe WINTEMPFOLDER\prog\themes\extract.txt"
Does that work?
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 17054673
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 17056400
may be

Try this

Dim WshShell

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "Your full path file name", 6, true
Set WshShell = nothing
0
 

Author Comment

by:TheWiep
ID: 17059142
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
 
LVL 13

Accepted Solution

by:
Mark_FreeSoftware earned 1200 total points
ID: 17059906
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
 

Author Comment

by:TheWiep
ID: 17060115
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
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 17060152

thanks for the points, and happy coding

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question