`

VB 管道取得命令行程序回显(CMD GUI)

    博客分类:
  • vb
阅读更多
Option Explicit
Option Base 0
'Code written by JoshT. Use at your own risk
Private Declare Function CreateProcess _
Lib "kernel32" _
Alias "CreateProcessA" (ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
lpProcessAttributes
As SECURITY_ATTRIBUTES, _
lpThreadAttributes
As SECURITY_ATTRIBUTES, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
lpEnvironment
As Any, _
ByVal lpCurrentDirectory As String, _
lpStartupInfo
As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function
CloseHandle _
Lib "kernel32.dll" (ByVal hObject 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 Long) As Long
Private Declare Function
WaitForSingleObject _
Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Private Declare Function
CreatePipe _
Lib "kernel32" (phReadPipe As Long, _
phWritePipe
As Long, _
lpPipeAttributes
As SECURITY_ATTRIBUTES, _
ByVal nSize As Long) As Long
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
Private Type SECURITY_ATTRIBUTES
nLength
As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End
Type
Private Const NORMAL_PRIORITY_CLASS As Long = &H20&
Private Const STARTF_USESTDHANDLES As Long = &H100&
Private Const STARTF_USESHOWWINDOW As Long = &H1&
Private Const SW_HIDE As Long = 0&
Private Const INFINITE As Long = &HFFFF&
Public Function RunCommand(CommandLine As String) As String
Dim
si As STARTUPINFO 'used to send info the CreateProcess
Dim pi As PROCESS_INFORMATION 'used to receive info about the created process
Dim retval As Long 'return value
Dim hRead As Long 'the handle to the read end of the pipe
Dim hWrite As Long 'the handle to the write end of the pipe
Dim sBuffer(0 To 63) As Byte 'the buffer to store data as we read it from the pipe
Dim lgSize As Long 'returned number of bytes read by readfile
Dim sa As SECURITY_ATTRIBUTES
Dim strResult As String 'returned results of the command line

'set up security attributes structure
100 With sa
102 .nLength = Len(sa)
104 .bInheritHandle = 1& 'inherit, needed for this to work
106 .lpSecurityDescriptor = 0&
End With

'create our anonymous pipe an check for success
' note we use the default buffer size
' this could cause problems if the process tries to write more than this buffer size
108 retval = CreatePipe(hRead, hWrite, sa, 0&)

110 If retval = 0 Then
112 Debug.Print "CreatePipe Failed"
114 RunCommand = ""
Exit Function
End If

'set up startup info
116 With si
118 .cb = Len(si)
120 .dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW 'tell it to use (not ignore) the values below
122 .wShowWindow = SW_HIDE
' .hStdInput = GetStdHandle(STD_INPUT_HANDLE)
124 .hStdOutput = hWrite 'pass the write end of the pipe as the processes standard output
' .hStdError = GetStdHandle(STD_ERROR_HANDLE)
End With

'run the command line and check for success
126 retval = CreateProcess(vbNullString, CommandLine & vbNullChar, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS,
ByVal 0&, vbNullString, si, pi)

128 If retval Then
'wait until the command line finishes
' trouble if the app doesn't end, or waits for user input, etc
130 WaitForSingleObject pi.hProcess, INFINITE

'read from the pipe until there's no more (bytes actually read is less than what we told it to)
132 Do While ReadFile(hRead, sBuffer(0), 64, lgSize, ByVal 0&)
'convert byte array to string and append to our result
134 strResult = strResult & StrConv(sBuffer(), vbUnicode)
'TODO = what's in the tail end of the byte array when lgSize is less than 64???
136 Erase sBuffer()

138 If lgSize <> 64 Then Exit Do
Loop

'close the handles of the process
140 CloseHandle pi.hProcess
142 CloseHandle pi.hThread
Else
144 Debug.Print "CreateProcess Failed" & vbCrLf
End If

'close pipe handles
146 CloseHandle hRead
148 CloseHandle hWrite
'return the command line output
150 RunCommand = Replace(strResult, vbNullChar, "")
End Function

 

分享到:
评论

相关推荐

    VB命令行管道控件(与CMD交互,风格类似WinSock)

    本控件与cmd控制台和其他命令行程序交互的ocx控件,效果很像Winsock或MSCOMCTL。TestForm是测试该控件用的窗体。在VB6/IE10(asp-VBS)/Excel2003下测试成功。完美兼容单换行符(chr10)和vbcrlf两种换行格式。本控件...

    基于匿名管道的cmd回显

    基于标题"基于匿名管道的cmd回显",我们可以理解这是一个使用C++(VC++)编写的程序,其功能是通过匿名管道实现命令行(cmd)的回显效果。下面将详细解释这个知识点。 匿名管道是一种简单的IPC机制,用于在具有亲缘...

    cmd_火山PC_cmd_CMD回显_

    标题中的"cmd_火山PC_cmd_CMD回显_"表明我们讨论的主题是关于在Windows操作系统中,使用命令行工具CMD(Command Prompt)与火山PC软件相结合,进行注册表的回显操作。火山PC是一款针对64位系统的软件,版本为...

    vc 命令管道源码 cmd 回显,

    在这个场景中,"vc 命令管道源码 cmd 回显"指的是使用C++编程语言,通过命令行接口(cmd)来实现命令管道的功能,并且具有回显(echo)效果的源代码。下面我们将详细探讨这些知识点。 **命令管道**是Unix和类Unix系统,...

    VC Runcmd cmd回显

    在给定的“VC Runcmd cmd回显”项目中,开发者使用MFC(Microsoft Foundation Classes)框架来编写一个小程序,该程序能够执行命令行(cmd)操作并实时显示其回显结果。下面将详细介绍这个过程中的关键知识点: 1. ...

    pipe-CMD.rar_CMD回显_CreateProcess_c++ cmd pipe_windows管道cmd_管道 c

    利用管道执行cmd,显示回显内容,管道加createprocess实现该功能

    隐藏cmd命令行运行

    这里,`@echo off`命令用于关闭命令行的回显,`start /min`则是让CMD窗口最小化启动,而`/c`参数后面则是你想要执行的具体命令。创建这样的.bat文件后,双击即可在后台运行。 2. VBScript(.vbe): VBScript是一...

    易语言使用管道运行DOS命令回显执行结果

    在“易语言使用管道运行DOS命令回显执行结果”这个主题中,我们要讨论的是如何利用易语言来创建一个API模块,以便通过管道通信方式执行DOS命令,并获取命令的返回结果。 首先,DOS命令是操作系统提供的一种命令行...

    在vb中获取DOS回显信息

    总的来说,掌握在VB中获取DOS回显信息的技术,对于开发需要与命令行交互的应用程序非常有用,例如自动化脚本、系统监控或者数据分析等场景。通过深入理解和实践,你将能够更好地驾驭VB和API调用,提升你的编程技能。

    VC++实现CMD命令执行与获得返回信息

    例如,我们可以修改CMD命令行,将输出重定向到管道: ```cpp TCHAR cmdLine[] = _T("cmd /c dir &gt; \\\\.\\pipe\\MyPipe"); // 将dir命令的输出重定向到管道 ``` 以上代码展示了如何在VC++6.0中使用`CreateProcess`...

    Cmd 函数执行.rar

    6. **C++与Cmd交互**:通过`system()`函数或创建子进程(`CreateProcess()`)在C++程序中调用Cmd命令,实现程序与命令行的交互,例如,实现回显功能。 7. **命令行参数**:学习如何在Cmd中传递参数给程序,并在程序...

    cmd命令行.TXT

    cmd命令行 第一章 批处理基础 第一节 常用批处理内部命令简介 第二节 常用特殊符号 第二章 FOR命令详解 第五章 set命令详解 第二节 常用特殊符号 1、@ 命令行回显屏蔽符 2、% 批处理变量引导符 3、&gt; 重定向...

    Windows命令行cmd高级教程.pdf

    Windows命令行cmd高级教程主要涵盖了使用Windows操作系统中cmd.exe命令行解释器进行管理和自动化任务的高级技术。本教程将详细介绍一系列批处理文件(.bat)的编写技巧、命令行工具的使用方法以及环境变量和文件属性的...

    Windows命令行cmd高级教程[定义].pdf

    《Windows命令行cmd高级教程》是一本专注于提升用户在Windows操作系统中使用命令行工具CMD技能的教程。CMD,全称为Command Prompt,是Windows系统提供的一个命令行界面,允许用户通过输入命令来执行各种系统操作。...

    delphi执行dos程序并回显

    在Delphi编程环境中,执行DOS程序并回显到 Memo 控件是一种常见的需求,这通常涉及到操作系统交互和控制台命令的处理。以下是对这个主题的详细解释。 首先,我们需要了解 Delphi 中如何执行外部程序。这主要通过 `...

    VB Dos模拟器

    "VB Dos模拟器"是一个基于Visual Basic (VB) 开发的应用程序,它的主要功能是模拟DOS(Disk Operating System)环境,让用户能够在Windows操作系统中执行类似于DOS命令行的指令。这个程序对于那些习惯于DOS命令或者...

    cmd命令行高级教程

    在这一背景下,掌握CMD命令行成为了深入理解计算机操作系统的必经之路,尤其对于那些希望探索人机交互深层机制的人来说,它是一项基础技能。 CMD命令行,又称命令提示符,是Windows操作系统中用于人机交互的重要...

    delphi cmd 获取命令返回(源码)

    标签"delphi"、"cmd"和"获取命令返回"强调了本主题的重点,即在Delphi环境中,如何利用CMD命令行接口进行系统操作,并获取这些操作的返回结果。通过理解并熟练运用这些技术,开发者可以构建更强大、更灵活的系统工具...

Global site tag (gtag.js) - Google Analytics