vb6截取控制台命令执行结果的代码

简介: <p>Option Explicit <br>' Contants <br>Private Const STARTF_USESHOWWINDOW     As Long = &H1 <br>Private Const STARTF_USESTDHANDLES     As Long = &H100 <br>Private Const SW_HIDE

Option Explicit
' Contants
Private Const STARTF_USESHOWWINDOW     As Long = &H1
Private Const STARTF_USESTDHANDLES     As Long = &H100
Private Const SW_HIDE                  As Integer = 0
' Types
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
    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
' Declares
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, _
                                                  lpBuffer As Any, _
                                                  ByVal nNumberOfBytesToRead As Long, _
                                                  lpNumberOfBytesRead As Long, _
                                                  lpOverlapped As Any) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, _
                                                                              ByVal lpCommandLine As String, _
                                                                              lpProcessAttributes As Any, _
                                                                              lpThreadAttributes As Any, _
                                                                              ByVal bInheritHandles As Long, _
                                                                              ByVal dwCreationFlags As Long, _
                                                                              lpEnvironment As Any, _
                                                                              ByVal lpCurrentDriectory As String, _
                                                                              lpStartupInfo As STARTUPINFO, _
                                                                              lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

'---------------------------------------------------
' Call this sub to execute and capture a console app.
' Ex: Call ExecAndCapture("ping localhost", Text1)
Public Function ExecAndCapture(ByVal sCommandLine As String, _
                          Optional ByVal sStartInFolder As String = vbNullString) As String

Const BUFSIZE         As Long = 1024 * 10
Dim hPipeRead         As Long
Dim hPipeWrite        As Long
Dim sa                As SECURITY_ATTRIBUTES
Dim si                As STARTUPINFO
Dim pi                As PROCESS_INFORMATION
Dim baOutput(BUFSIZE) As Byte
Dim sOutput           As String
Dim lBytesRead        As Long
    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          ' hide the window
        .hStdOutput = hPipeWrite
        .hStdError = hPipeWrite
    End With 'SI
    If CreateProcess(vbNullString, sCommandLine, ByVal 0&, ByVal 0&, 1, 0&, ByVal 0&, sStartInFolder, si, pi) Then
        Call CloseHandle(hPipeWrite)
        Call CloseHandle(pi.hThread)
        hPipeWrite = 0
        Do
            'DoEvents
            If ReadFile(hPipeRead, baOutput(0), BUFSIZE, lBytesRead, ByVal 0&) = 0 Then
                Exit Do
            End If
            sOutput = sOutput & Left$(StrConv(baOutput(), vbUnicode), lBytesRead)
            'cTextBox.SelText = sOutput
        Loop
        Call CloseHandle(pi.hProcess)
    End If
    ' To make sure...
    Call CloseHandle(hPipeRead)
    Call CloseHandle(hPipeWrite)
    ExecAndCapture = sOutput
End Function

目录
相关文章
|
7月前
|
JavaScript
【vue】vue2 禁用控制台、右键 | 代码防扒
【vue】vue2 禁用控制台、右键 | 代码防扒
285 2
|
7月前
|
JavaScript 前端开发 算法
Node.js 艺术:用代码打印出绚丽多彩的控制台柱状图
Node.js 艺术:用代码打印出绚丽多彩的控制台柱状图
111 0
|
7月前
|
Linux Shell PHP
linux(十五)文件和目录相关命令-控制台输出命令echo和重定向
linux(十五)文件和目录相关命令-控制台输出命令echo和重定向
131 0
|
Java
【IntelliJ IDEA】中文乱码问题 ( 代码乱码 | 编译乱码 | 控制台乱码 )
【IntelliJ IDEA】中文乱码问题 ( 代码乱码 | 编译乱码 | 控制台乱码 )
2263 0
【IntelliJ IDEA】中文乱码问题 ( 代码乱码 | 编译乱码 | 控制台乱码 )
|
4月前
|
Windows
基本的Dos命令 在控制台如何进入某一个文件或者进入不同的盘符
这篇文章介绍了如何在Windows DOS命令行环境中进入不同盘符和文件夹,包括使用Win+R快捷键打开运行窗口、通过输入盘符加冒号(如"D:")切换到指定盘符、使用"cd"命令进入文件夹,以及一些基本的文件操作命令如"dir"查看文件和"del"删除文件等。
基本的Dos命令 在控制台如何进入某一个文件或者进入不同的盘符
|
7月前
|
网络安全 数据库 Windows
Metasploit6.0系列教程 -- MSF控制台命令
Metasploit6.0系列教程 -- MSF控制台命令
66 1
|
7月前
|
编解码 Linux Shell
【Shell 命令集合 系统设置 】Linux 设置Linux系统的控制台参数和属性setconsole命令 使用指南
【Shell 命令集合 系统设置 】Linux 设置Linux系统的控制台参数和属性setconsole命令 使用指南
112 0
HMI-45-【控制台】控制台代码迁移
今天实在是看不下去控制台了,多媒体先放一下,先搞一下控制台,说好听点就是优化一下控制台控制逻辑。
|
Linux 开发工具 数据安全/隐私保护
Linux基础操作2(常用基础命令,控制台操作)
Linux基础操作2(常用基础命令,控制台操作)
94 0
Linux基础操作2(常用基础命令,控制台操作)
|
测试技术 Android开发
消失的Eclipse-控制台console、右侧项目导航Package Explorer、点击类不弹出内容、copy过来正确的测试单元类'废了'的解决措施、eclipse设置编译前自动保存代码
消失的Eclipse-控制台console、右侧项目导航Package Explorer、点击类不弹出内容、copy过来正确的测试单元类'废了'的解决措施、eclipse设置编译前自动保存代码
257 0
消失的Eclipse-控制台console、右侧项目导航Package Explorer、点击类不弹出内容、copy过来正确的测试单元类'废了'的解决措施、eclipse设置编译前自动保存代码