Reputation: 2030
I am running following command from Command Prompt
dir > c:\log.txt 2>&1
The out is directed to c:\log.txt
file successfully.
Then, running the same command using CreateProcessA as below and nothing happens
Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
start.cb = Len(start)
start.dwFlags = 1
start.wShowWindow = 1
ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
ret = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ret&)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = ret&
End Function
Here cmdline$
is passed as dir > c:\log.txt 2>&1
I have tried Batch file - How to redirect output from exe after it has terminated? and Display & Redirect Output
Please suggest what is wrong here
Upvotes: 0
Views: 536
Reputation: 703
Why don't you use the shell function? Here is an example of how to redirect the output:
Option Explicit
Private Sub Form_Load()
ExecCmd "dir >c:\log.txt 2>&1"
End Sub
Private Sub ExecCmd(cmdline As String)
Shell "cmd /c " & cmdline, vbHide
End Sub
Upvotes: 1