Vb6 重定向输入/输出控制台命令
我有以下我的作者代码使用“管道”创建控制台,我的问题是是否可以使用“管道”的替代方案 问候:DVb6 重定向输入/输出控制台命令,vb6,cmd,pipe,Vb6,Cmd,Pipe,我有以下我的作者代码使用“管道”创建控制台,我的问题是是否可以使用“管道”的替代方案 问候:D ' ****************************************************************************************************************************** ' ' ' --- Autor: Jhonjhon_123 (Jhon Jairo Pro Developer) ' --- Versión: 1
' ****************************************************************************************************************************** '
'
' --- Autor: Jhonjhon_123 (Jhon Jairo Pro Developer)
' --- Versión: 1.0
' --- Descripción: Shell a nivel local en windows
' --- Fallos y Mejoras: MSN; j.j.g.p@hotmail.com
' --- Licencia: GNU General Public License
'
' ****************************************************************************************************************************** '
Option Explicit
Private Declare Function PeekNamedPipe Lib "kernel32" (ByVal hNamedPipe As Long, lpBuffer As Any, ByVal nBufferSize As Long, lpBytesRead As Long, lpTotalBytesAvail As Long, lpBytesLeftThisMessage 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 WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As Any, ByVal nSize As Long) 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
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function DuplicateHandle Lib "kernel32" (ByVal hSourceProcessHandle As Long, ByVal hSourceHandle As Long, ByVal hTargetProcessHandle As Long, lpTargetHandle As Long, ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwOptions As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Const STARTF_USESTDHANDLES As Long = &H100
Private Const STARTF_USESHOWWINDOW As Long = &H1
Private Const DUPLICATE_SAME_ACCESS As Long = &H2
Private Const NORMAL_PRIORITY_CLASS As Long = &H20
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
Dim lHInput As Long
Dim lHOutput As Long
Dim lCmdID As Long
Public Sub StopShell()
If lHInput > 0 Then Call CloseHandle(lHInput)
If lHOutput > 0 Then Call CloseHandle(lHOutput)
If lCmdID > 0 Then Call TerminateProcess(lCmdID, ByVal 0&): Call CloseHandle(lCmdID)
End Sub
Public Function GetOutTextShell(sOut As String) As Boolean
Dim bBuffer() As Byte
Dim lLen As Long
Dim bRes As Boolean
Dim lLenBuff As Long
bRes = CBool(PeekNamedPipe(lHOutput, 0&, 0&, 0&, lLen, 0&))
If Not bRes Then Exit Function
If lLen <= 0 Then Exit Function
ReDim bBuffer(lLen)
If ReadFile(lHOutput, bBuffer(0), lLen, lLenBuff, ByVal 0&) = 0 Then Exit Function
sOut = Left(StrConv(bBuffer, vbUnicode), lLenBuff)
GetOutTextShell = True
End Function
Public Sub SendToShell(sCMD As String)
Dim sBytes() As Byte
Dim BytesWritten As Long
If lHInput = 0 Then Exit Sub
sCMD = sCMD & vbNewLine
sBytes = StrConv(sCMD, vbFromUnicode)
If WriteFile(lHInput, ByVal sCMD, Len(sCMD), BytesWritten, 0&) = 0 Then
Exit Sub
End If
End Sub
Public Function StartShell() As Boolean
On Error GoTo Error
Dim tSecurityAttributes As SECURITY_ATTRIBUTES
Dim tStartInfo As STARTUPINFO
Dim tProcessInfo As PROCESS_INFORMATION
Dim lCurrentID As Long
lCurrentID = GetCurrentProcess()
With tStartInfo
.cb = Len(tStartInfo)
.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
End With
With tSecurityAttributes
.nLength = Len(tSecurityAttributes)
.bInheritHandle = 1
End With
If CreatePipe(lHOutput, tStartInfo.hStdOutput, tSecurityAttributes, 0) = 0 Then
GoTo Error
End If
If CreatePipe(tStartInfo.hStdInput, lHInput, tSecurityAttributes, 0) = 0 Then
GoTo Error
End If
If DuplicateHandle(lCurrentID, tStartInfo.hStdOutput, lCurrentID, tStartInfo.hStdError, 0&, True, DUPLICATE_SAME_ACCESS) = 0 Then
GoTo Error
End If
If CreateProcess(vbNullString, "cmd", tSecurityAttributes, tSecurityAttributes, 1, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, tStartInfo, tProcessInfo) = 0 Then
GoTo Error
End If
With tProcessInfo
Call CloseHandle(.hThread)
lCmdID = .hProcess
If .dwProcessID > 0 And .hProcess > 0 Then
StartShell = True
Else
GoTo Error
End If
End With
Exit Function
Error:
Call StopShell
StartShell = False
End Function
”***************************************************************************************************************************************************************************************************************************************************************************************************************************************************
'
“---自动:Jhonjhon_123(Jhon Jairo专业开发者)
---版本:1.0
---描述:在窗户上打上一层薄壳
---法洛斯·梅乔拉斯:MSN;j、 j.g。p@hotmail.com
---许可证:GNU通用公共许可证
'
' ****************************************************************************************************************************** '
选项显式
私有声明函数PeekNamedPipe Lib“kernel32”(ByVal hNamedPipe尽可能长,lpBuffer尽可能长,ByVal nBufferSize尽可能长,lpBytesRead尽可能长,lpTotalBytesAvail尽可能长,lpBytesLeftThisMessage尽可能长)尽可能长
私有声明函数ReadFile Lib“kernel32”(ByVal hFile为长,lpBuffer为任意,ByVal nNumberOfBytesToRead为长,lpNumberOfBytesRead为长,lpOverlapped为任意)为长
私有声明函数WriteFile Lib“kernel32”(ByVal hFile尽可能长,lpBuffer尽可能长,ByVal nnumberofbytes尽可能长,lpnumberofbytes尽可能长,ByVal lpOverlapped尽可能长)尽可能长
私有声明函数CreatePipe Lib“kernel32”(phReadPipe为Long,phWritePipe为Long,lpPipeAttributes为Any,ByVal nSize为Long)为Long
私有声明函数CreateProcess Lib“kernel32”别名“CreateProcessA”(ByVal lpApplicationName为字符串,ByVal lpCommandLine为字符串,lpProcessAttributes为任意,lpThreadAttributes为任意,ByVal bInheritHandles为长,ByVal dwCreationFlags为长,lpEnvironment为任意,ByVal lpCurrentDriectory为字符串,lpStartupInfo为STARTUPINFO,lpProcessInformation为进程信息)为长
私有声明函数CloseHandle Lib“kernel32”(ByVal hObject As Long)为Long
私有声明函数TerminateProcess Lib“kernel32”(ByVal hProcess As Long,ByVal uExitCode As Long)作为Long
私有声明函数DuplicateHandle Lib“kernel32”(ByVal hSourceProcessHandle为Long,ByVal hSourceHandle为Long,ByVal hTargetProcessHandle为Long,lpTargetHandle为Long,ByVal dwDesiredAccess为Long,ByVal bInheritHandle为Long,ByVal dwOptions为Long)为Long
私有声明函数GetCurrentProcess Lib“kernel32”(长度为
私人Const STARTF_USESTDHANDLES的长度=&H100
Private Const STARTF_USESHOWWINDOW作为Long=&H1
Private Const DUPLICATE\u与Long=&H2访问相同
Private Const NORMAL_PRIORITY_类的长度=&H20
私有类型安全属性
长度等于
lpSecurityDescriptor的长度
长柄
端型
私有类型STARTUPINFO
只要
保留为字符串
lpDesktop作为字符串
lpTitle作为字符串
dwX尽可能长
只要
dwXSize尽可能长
尽可能长
dwXCountChars的长度为
德怀康查斯一样长
dwFillAttribute尽可能长
把旗子拖得一样长
wShowWindow作为整数
cbReserved2为整数
lpReserved2尽可能长
HST输入长度为
hst输出长度
hStdError尽可能长
端型
私有类型进程信息
hProcess尽可能长
hThread尽可能长
dwProcessID尽可能长
dwThreadId尽可能长
端型
长输入
输出尽可能长
将lCmdID设置为长
公共子StopShell()
如果lHInput>0,则调用CloseHandle(lHInput)
如果lHOutput>0,则调用CloseHandle(lHOutput)
如果lCmdID>0,则调用TerminateProcess(lCmdID,ByVal 0&):调用CloseHandle(lCmdID)
端接头
作为布尔值的公共函数GetOutTextShell(sOut作为字符串)
Dim bBuffer()作为字节
暗淡如长
作为布尔的Dim-bRes
昏暗的伦巴夫像长的一样
bRes=CBool(PeekNamedPipe(lHOutput,0&,0&,0&,lLen,0&))
如果不是bRes,则退出功能
如果lLen 0和.hProcess>0,则
StartShell=True
其他的
转到错误
如果结束
以
退出功能
错误:
叫停炮弹
StartShell=False
端函数
代码完整示例:你必须使用管道。看看这个助手类:@wqw非常感谢这个类将指导我!但肯定没有“管道”的替代品?管道有什么问题?你想实现什么?我的想法是使代码尽可能小,我看到过使用“袜子”的同样的东西,但需要客户,询问为什么有本地替代方案?