Re: CMD
- From: "Karl E. Peterson" <karl@xxxxxxxx>
- Date: Thu, 11 Jan 2007 12:19:42 -0800
peek^ <sniffpeek@xxxxxxxxxxx> wrote:
Hi. I have a DOS, well windows console, program. I wonder if there is any
way to call it, but instead of a CMD window, I want it to appear in my own
window. Like have the prompt displayed on TEdit control, or something.
Anyway to do that?
Possibly. You'll need to create a pipe, to read the standard output from the
created process. I picked the essence of this routine from the commented link
within:
Private Function ExecAndCapture(ByVal CmdLine As String, Optional ByVal
StartFolder As String = vbNullString) As String
' http://www.vb-helper.com/howto_capture_console_stdout.html
Dim hPipeRead As Long
Dim hPipeWrite As Long
Dim sa As SECURITY_ATTRIBUTES
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim Buffer() As Byte
Dim StdOut As New CStringBuilder
Dim lBytesRead As Long
Dim ExitCode As Long
Const BUFSIZE As Long = 1024 * 10
With sa
.nLength = Len(sa)
.bInheritHandle = 1 ' get inheritable pipe handles
End With 'SA
If CreatePipe(hPipeRead, hPipeWrite, sa, 0) = 0 Then
Exit Function
End If
With si
.cb = Len(si)
.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
.wShowWindow = SW_HIDE
.hStdOutput = hPipeWrite
.hStdError = hPipeWrite
End With 'SI
If CreateProcess(vbNullString, CmdLine, ByVal 0&, ByVal 0&, 1, 0&, ByVal 0&,
StartFolder, si, pi) Then
Call CloseHandle(hPipeWrite)
Call CloseHandle(pi.hThread)
ReDim Buffer(0 To BUFSIZE - 1) As Byte
Do
DoEvents
If ReadFile(hPipeRead, Buffer(0), UBound(Buffer) + 1, lBytesRead, ByVal
0&) = 0 Then
Exit Do
End If
Debug.Print Replace$(Left$(StrConv(Buffer, vbUnicode), lBytesRead),
vbCr & vbCrLf, vbCrLf);
StdOut.Append Replace$(Left$(StrConv(Buffer, vbUnicode), lBytesRead),
vbCr & vbCrLf, vbCrLf)
Loop
Call CloseHandle(pi.hProcess)
End If
' To make sure...
Call CloseHandle(hPipeRead)
Call CloseHandle(hPipeWrite)
' Return results.
ExecAndCapture = StdOut.ToString
End Function
Probably not drop-in ready, but should give you an idea or three.
--
..NET: It's About Trust!
http://vfred.mvps.org
.
- References:
- CMD
- From: peek^
- CMD
- Prev by Date: CMD
- Next by Date: Alpha Blended Windows in Vista
- Previous by thread: CMD
- Next by thread: Alpha Blended Windows in Vista
- Index(es):
Relevant Pages
|