How to intercept the output of the command line

zhaozj2021-02-08  291

Option expedition

Private Declare Function CreatePipe lib "kernel32" (phwritepipe as long, lppipeattributes as security_attributes, byval nsize as long) AS Long

Private Declare Function Readfile Lib "Kernel 32" (Byval Lpbuffer As String, Byval NNumberofbyteread 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 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

WSHOWINDOW 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 CreateProcessAsUser Lib "advapi32.dll" Alias ​​"CreateProcessAsUserA" (ByVal hToken As Long, ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As SECURITY_ATTRIBUTES, ByVal lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, BYVAL LPENVIRENTDIRECTORY AS STRINFO, BYVAL LPSTARTUPINFO AS STARTUPINFO, BYVAL LPPROCESSINFORMATION As Process_information) As long

Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO , LPPROCESSINFORMATION As Process_information) AS Longprivate Declare Function CloseHandle LIB "Kernel32" (Byval Hobject As Long) As long

PRIVATE CONST NORMAL_PRIORITY_CLASS = & H20

Private const startf_usestdhandles = & h100

Private const startf_useshowwindow = & h1

Private function ExecuteCommandLineoutput (Commandline As String, Optional Buffize As long = 256, Optional Timeout As long) AS String

DIM PROC AS Process_Information

Dim Start As StartupInfo

Dim Sa as security_attributes

DIM HREADPIPE AS Long

DIM HWRITEPIPE AS Long

DIM LBYTESREAD As Long

Dim SBuffer As String

If vba.len (commandline)> 0 THEN

Sa.nlength = len (sa)

'Sa.nlength = VBA.LEN (SA)

Sa.binherithandle = 1 &

Sa.lpsecurityDescriptor = 0 &

IF createPipe (Hreadpipe, hwritepipe, sa, 0)> 0 THEN

Start.cb = len (start)

Start.dwflags = startf_useestdhandles or startf_useshowwindow

Start.hstdoutput = hwritepipe

Start.hstderRor = hwritepipe

IF CreateProcessa (0 &, CommandLine, SA, SA, 1 &, NORMAL_PRIORITY_CLASS, 0 &, 0 &, Start, Proc) = 1 THEN

CloseHandle HwritePipe

SBuffer = vba.string (buffersize, vba.chr (0))

IF Timeout> 0 THEN

DIM Begintime As Date

Begintime = VBA.NOW

END IF

DO Until Readfile (Hreadpipe, Sbuffer, Buffitsize, LbytesRead, 0 &) = 0

Doevents

IF Timeout> 0 Thenif VBA.datediff ("s", begintime, vba.now> Timeout Then

ExecuteCommandLineoutput = "Timeout"

Exit do

END IF

END IF

ExecuteCommandLineoutput = ExecuteCommandLineoutput & VBa.Left (Sbuffer, LbytesRead)

Loop

CloseHandle Proc.hprocess

CloseHandle Proc.hthread

CloseHandle Hreadpipe

Else

ExecuteCommandLineoutput = "File or Command Not Found"

END IF

Else

ExecuteCommandLineoutput = "CreatePipe failed. Error:" & err.lastdller "."

END IF

END IF

END FUNCTION

Private sub fascist1_click () 'Test

'Vba.msgbox executeCommandLineoutput ("ping

Www.sina.com.cn ")

VBA.MSGBOX EXECUTECOMMAndLineoutput ("ping

www.xxxx.com.cn ",, 2)

End Sub

转载请注明原文地址:https://www.9cbs.com/read-1080.html

New Post(0)