VB实现CreateProcess重定向输出到一个指定文件
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
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 lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId 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 SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const STARTF_USESTDHANDLES = &H4
Private Const STARTF_USESHOWWINDOW = &H1
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_WRITE = &H2
Private Const FILE_SHARE_READ = &H1
Private Const OPEN_ALWAYS = 4
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Public Enum enSW
SW_HIDE = 0
SW_NORMAL = 1
SW_MAXIMIZE = 3
SW_MINIMIZE = 6
End Enum
Public Enum enPriority_Class
NORMAL_PRIORITY_CLASS = &H20
IDLE_PRIORITY_CLASS = &H40
HIGH_PRIORITY_CLASS = &H80
End Enum
Private Sub Command1_Click()
Dim pInfo As PROCESS_INFORMATION
Dim sInfo As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim sNull As String
Dim lSuccess As Long
Dim lRetValue As Long
Dim k As String
k = "ping.exe 192.168.5.110"
Dim hfile As Long
sa.nLength = Len(SECURITY_ATTRIBUTES)
sa.lpSecurityDescriptor = 0
sa.bInheritHandle = 1
hfile = CreateFile("test.txt", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, sa, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
MsgBox hfile
sInfo.cb = Len(STARTUPINFO)
sInfo.dwFlags = STARTF_USESTDHANDLES 'Or STARTF_USESHOWWINDOW
'sInfo.wShowWindow = SW_HIDE
sInfo.hStdOutput = hfile
lSuccess = CreateProcess(sNull, _
k, _
sa, _
sa, _
True, _
NORMAL_PRIORITY_CLASS, _
0, _
sNull, _
sInfo, _
pInfo)
MsgBox lSuccess
lRetValue = TerminateProcess(pInfo.hProcess, 0&)
lRetValue = CloseHandle(pInfo.hThread)
lRetValue = CloseHandle(pInfo.hProcess)
lRetValue = CloseHandle(hfile)
End Sub
程序可以运行成功,但是重定向的文件里没有内容啊???
[解决办法]
我记得hStdOutput与hStdInput都是指向管道的句柄,而不是直接的文件句柄.
找到了:
http://bbs.csdn.net/topics/210031042
5楼和6楼我发的代码.
把CMD.EXE换成PING试试.
另外,也有自己发ICMP包实现PING功能的模块:
PING源码
[解决办法]
管道通信。老马这个是匿名管道,是单向通信。还可用命名管道,2个exe文件可以双向通信。