Capture dos screen output from a command line

Source: Internet
Author: User
Tags readfile

This code is extremely useful if you ever need to capture output from a DOS screen/console. The simple demonstration below, shows how to capture the output from a batch file.

Note, to redirect other handles (stdin and stderr), create a pipe for each handle for which redirection is desired. the Code shoshould read from the read ends of the pipes for the redirected stdout and stderr. if stdin redirection was desired, the cocould shoshould write to the write end of the appropriate pipe.

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 declare function getnamedpipeinfo lib "Kernel32" (byval hnamedpipe as long, ltype as long, llenoutbuf as long, lleninbuf as long, lmaxinstances as long) 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 waitforsingleobject lib "Kernel32" (byval hhandle as long, byval dwmilliseconds as long) as long
Private declare function createprocessa lib "Kernel32" (byval lpapplicationname as long, byval lpcommandline as string, lpprocessattributes as any, stored 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

'Purpose: synchronously runs a DOS command line and returns the captured screen output.
'Inpututs: scommandline the DOS command line to run.
'[Bshowwindow] If true displays the DOS output window.
'Outputs: returns the screen output
'Author: Andrew Baker
'Date: 03/09/2000
'Notes: this routine will work only with those program that sends their output
'The standard output device (stdout ).
'Windows nt only.
'Revisions:

Function shellexecutecapture (scommandline as string, optional bshowwindow as Boolean = false) as string
Const clreadbytes as long = 256, infinite as long = & hffffffff
Const startf_useshowwindow = & H1, startf_usestdhandles = & h100 &
Const sw_hide = 0, sw_normal = 1
Const normal_priority_class = & H20 &

Const pipe_client_end = & h0' the handle refers to the client end of a named pipe instance. This is the default.
Const pipe_server_end = & H1 'The handle refers to the server end of a named pipe instance. If this value is not specified, the handle refers to the client end of a named pipe instance.
Const pipe_type_byte = & h0' the named pipe is a byte pipe. This is the default.
Const pipe_type_message = & H4 'the named pipe is a message pipe. If this value is not specified, the pipe is a byte Pipe


Dim tprocinfo as process_information, lretval as long, lsuccess as long
Dim tstartupinf as startupinfo
Dim tsecurattrib as security_attributes, lhwndreadpipe as long, lhwndwritepipe as long
Dim lbytesread as long, sbuffer as string
Dim lpipeoutlen as long, lpipeinlen as long, lmaxinst as long

Tsecurattrib. nlength = Len (tsecurattrib)
Tsecurattrib. binherithandle = 1 &
Tsecurattrib. lpsecuritydescriptor = 0 &

Lretval = createpipe (lhwndreadpipe, lhwndwritepipe, tsecurattrib, 0)
If lretval = 0 then
'Createpipe failed
Exit Function
End if

Tstartupinf. cb = Len (tstartupinf)
Tstartupinf. dwflags = startf_usestdhandles or startf_useshowwindow
Tstartupinf. hstdoutput = lhwndwritepipe
If bshowwindow then
'Show the DOS window
Tstartupinf. wshowwindow = sw_normal
Else
'Hide the DOS window
Tstartupinf. wshowwindow = sw_hide
End if

Lretval = createprocessa (0 &, scommandline, tsecurattrib, tsecurattrib, 1 &, normal_priority_class, 0 &, 0 &, tstartupinf, tprocinfo)
If lretval <> 1 then
'Createprocess failed
Exit Function
End if

'Process created, wait for completion. Note, this will cause your application
'To hang indefinately until this process completes.
'Note, you cocould alternatively use a loop, or a timeout (in MS) (see the "shellwait" function
'Http: // www.vbusers.com/code/codeget.asp? Threadid = 70 & postid = 1 for details)
Waitforsingleobject tprocinfo. hprocess, infinite

'Termine pipes Contents
Lsuccess = getnamedpipeinfo (lhwndreadpipe, pipe_type_byte, lpipeoutlen, lpipeinlen, lmaxinst)
If lsuccess then
'Got pipe info, create buffer
Sbuffer = string (lpipeoutlen, 0)
'Read output Pipe
Lsuccess = readfile (lhwndreadpipe, sbuffer, lpipeoutlen, lbytesread, 0 &)
If lsuccess = 1 then
'Pipe read successfully
Shellexecutecapture = left $ (sbuffer, lbytesread)
End if
End if

'Close handles
Call closehandle (tprocinfo. hprocess)
Call closehandle (tprocinfo. hthread)
Call closehandle (lhwndreadpipe)
Call closehandle (lhwndwritepipe)
End Function
 

'Demonstration routine
'Note: create a file called "C:/test. Bat" containing a single line:
'Dir *.*
Sub test ()
Debug. Print shellexecutecapture ("C:/test. Bat", false)
End sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.