Solved

ExecuteAndWait

Posted on 2006-07-06
11
926 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 300 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

623 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