Microsoft KB Archive/173085: Difference between revisions
m (Text replacement - ">" to ">") |
m (Text replacement - """ to """) |
||
Line 85: | Line 85: | ||
<pre class="codesample"> Option Explicit | <pre class="codesample"> Option Explicit | ||
Private Declare Function CreatePipe Lib | Private Declare Function CreatePipe Lib "kernel32" ( _ | ||
phReadPipe As Long, _ | phReadPipe As Long, _ | ||
phWritePipe As Long, _ | phWritePipe As Long, _ | ||
Line 91: | Line 91: | ||
ByVal nSize As Long) As Long | ByVal nSize As Long) As Long | ||
Private Declare Function ReadFile Lib | Private Declare Function ReadFile Lib "kernel32" ( _ | ||
ByVal hFile As Long, _ | ByVal hFile As Long, _ | ||
ByVal lpBuffer As String, _ | ByVal lpBuffer As String, _ | ||
Line 132: | Line 132: | ||
End Type | End Type | ||
Private Declare Function CreateProcessA Lib | Private Declare Function CreateProcessA Lib "kernel32" (ByVal _ | ||
lpApplicationName As Long, ByVal lpCommandLine As String, _ | lpApplicationName As Long, ByVal lpCommandLine As String, _ | ||
lpProcessAttributes As Any, lpThreadAttributes As Any, _ | lpProcessAttributes As Any, lpThreadAttributes As Any, _ | ||
Line 139: | Line 139: | ||
lpStartupInfo As Any, lpProcessInformation As Any) As Long | lpStartupInfo As Any, lpProcessInformation As Any) As Long | ||
Private Declare Function CloseHandle Lib | Private Declare Function CloseHandle Lib "kernel32" (ByVal _ | ||
hObject As Long) As Long | hObject As Long) As Long | ||
Line 161: | Line 161: | ||
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0) | ret = CreatePipe(hReadPipe, hWritePipe, sa, 0) | ||
If ret = 0 Then | If ret = 0 Then | ||
MsgBox | MsgBox "CreatePipe failed. Error: " & Err.LastDllError | ||
Exit Sub | Exit Sub | ||
End If | End If | ||
Line 173: | Line 173: | ||
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc) | NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc) | ||
If ret <> 1 Then | If ret <> 1 Then | ||
MsgBox | MsgBox "CreateProcess failed. Error: " & Err.LastDllError | ||
End If | End If | ||
Line 180: | Line 180: | ||
List1.AddItem Left(mybuff, bytesread) | List1.AddItem Left(mybuff, bytesread) | ||
Else | Else | ||
MsgBox | MsgBox "ReadFile failed. Error: " & Err.LastDllError | ||
End If | End If | ||
Line 193: | Line 193: | ||
<li><p>Add the following code to the Command1_Click event:</p> | <li><p>Add the following code to the Command1_Click event:</p> | ||
<pre class="codesample"> Private Sub Command1_Click() | <pre class="codesample"> Private Sub Command1_Click() | ||
ExecCmd ( | ExecCmd ("C:\Console\Debug\console.exe") | ||
End Sub | End Sub | ||
</pre></li></ol> | </pre></li></ol> |
Revision as of 11:06, 21 July 2020
Article ID: 173085
Article Last Modified on 6/29/2004
APPLIES TO
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
- Microsoft Visual Basic 5.0 Control Creation Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Visual Basic 4.0 Professional Edition
- Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
This article was previously published under Q173085
SUMMARY
This example illustrates a Visual Basic application starting another process with the purpose of redirecting that process's standard IO handles. The Visual Basic application redirects the created process's standard output handle to an anonymous pipe, then proceeds to read the output through the pipe. This sample just redirects STDOUT of the new process. To redirect other handles (STDIN and STDERR), create a pipe for each handle for which redirection is desired. The Visual Basic application would read from the read ends of the pipes for the redirected STDOUT and STDERR. If STDIN redirection was desired, the Visual Basic application would write to the write end of the appropriate pipe. An example follows:
'A pipe for redirection of STDOUT CreatePipe(hReadPipe1, hWritePipe1, sa, 0) 'A pipe for redirection of STDERR CreatePipe(hReadPipe2, hWritePipe2, sa, 0) 'A pipe for redirection of STDIN CreatePipe(hReadPipe3, hWritePipe3, sa, 0) 'Preparing to start the process with redirected handles start.hStdOutput = hWritePipe1 start.hStdError = hWritePipe2 start.hStdInput = hReadPipe3 'Reading output from the started process's STDOUT ReadFile(hReadPipe1, mybuff1, 100, bytesread, ByVal 0&) 'Reading output from the started process's STDERR ReadFile(hReadPipe2, mybuff2, 100, bytesread, ByVal 0&) 'Writing to the started process's STDIN WriteFile(hWritePipe3, mybuff3, 100, byteswritten, ByVal 0&)
MORE INFORMATION
- Start a new project in Visual Basic. Form1 is created by default.
Add the following code to the General Declarations section of Form1:
Option Explicit Private Declare Function CreatePipe Lib "kernel32" ( _ phReadPipe As Long, _ phWritePipe As Long, _ lpPipeAttributes As Any, _ ByVal nSize As Long) As Long Private Declare Function ReadFile Lib "kernel32" ( _ ByVal hFile As Long, _ ByVal lpBuffer As String, _ ByVal nNumberOfBytesToRead As Long, _ lpNumberOfBytesRead As Long, _ byval lpOverlapped As Any) As Long Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Private Type STARTUPINFO cb As Long lpReserved As Long lpDesktop As Long lpTitle As Long 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 Private Declare Function CreateProcessA Lib "kernel32" (ByVal _ lpApplicationName As Long, ByVal lpCommandLine As String, _ lpProcessAttributes As Any, lpThreadAttributes As Any, _ ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _ lpStartupInfo As Any, lpProcessInformation As Any) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal _ hObject As Long) As Long Private Const NORMAL_PRIORITY_CLASS = &H20& Private Const STARTF_USESTDHANDLES = &H100& Private Sub ExecCmd(cmdline$) Dim proc As PROCESS_INFORMATION, ret As Long, bSuccess As Long Dim start As STARTUPINFO Dim sa As SECURITY_ATTRIBUTES, hReadPipe As Long, hWritePipe _ As Long Dim bytesread As Long, mybuff As String Dim i As Integer mybuff = String(256, Chr$(65)) sa.nLength = Len(sa) sa.bInheritHandle = 1& sa.lpSecurityDescriptor = 0& ret = CreatePipe(hReadPipe, hWritePipe, sa, 0) If ret = 0 Then MsgBox "CreatePipe failed. Error: " & Err.LastDllError Exit Sub End If start.cb = Len(start) start.dwFlags = STARTF_USESTDHANDLES start.hStdOutput = hWritePipe ' Start the shelled application: ret& = CreateProcessA(0&, cmdline$, sa, sa, 1&, _ NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc) If ret <> 1 Then MsgBox "CreateProcess failed. Error: " & Err.LastDllError End If bSuccess = ReadFile(hReadPipe, mybuff, 100, bytesread, 0&) If bSuccess = 1 Then List1.AddItem Left(mybuff, bytesread) Else MsgBox "ReadFile failed. Error: " & Err.LastDllError End If ret& = CloseHandle(proc.hProcess) ret& = CloseHandle(proc.hThread) ret& = CloseHandle(hReadPipe) ret& = CloseHandle(hWritePipe) End Sub
- Add a CommandButton (Command1) to Form1.
Add the following code to the Command1_Click event:
Private Sub Command1_Click() ExecCmd ("C:\Console\Debug\console.exe") End Sub
Additional query words: kbInetdev kbInternet kbVBp400 kbVBp500 kbVBp600 kbdsi kbDSupport kbVBp
Keywords: kbhowto KB173085