retval = CreatePipe(hRead, hWrite, sa, 0&)
With si
.cb = Len(si)
.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW ''tell it to use (not ignore) the values below
.wShowWindow = SW_HIDE
.hStdOutput = hWrite ''pass the write end of the pipe as the processes standard output
End With
retval = CreateProcess(vbNullString, CommandLine & vbNullChar, sa, sa, 1&, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, si, pi)
CloseHandle hWrite
''WaitForSingleObject pi.hProcess, INFINITE
Do While 0 <> ReadFile(hRead, sBuffer(0), 4096, lgSize, ByVal 0&)
strResult = strResult & Left$(sBuffer(), lgSize)
Erase sBuffer()
DoEvents
Loop
CloseHandle pi.hProcess
CloseHandle pi.hThread
CloseHandle hRead
GetStrFromCommand = StrConv(strResult, vbUnicode)
End Function
Public Function RunCommand()Function RunCommand(ByVal CommandLine As String, ByVal WaitForIt As Boolean, Optional ByVal ShowWindow As Boolean = False) As Long
On Error Resume Next
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 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
With sa
.nLength = Len(sa)
.bInheritHandle = 1& ''inherit, needed for this to work
.lpSecurityDescriptor = 0&
End With
If ShowWindow Then
With si
.cb = Len(si)
.dwFlags = STARTF_USESHOWWINDOW ''tell it to use (not ignore) the values below
.wShowWindow = SW_NORMAL
''.hStdOutput = hWrite ''pass the write end of the pipe as the processes standard output
End With
Else
With si
.cb = Len(si)
.dwFlags = STARTF_USESHOWWINDOW ''tell it to use (not ignore) the values below
.wShowWindow = SW_HIDE
''.hStdOutput = hWrite ''pass the write end of the pipe as the processes standard output
End With
End If
retval = CreateProcess(vbNullString, CommandLine & vbNullChar, sa, sa, 1&, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, si, pi)
If WaitForIt Then
WaitForSingleObject pi.hProcess, INFINITE
End If
RunCommand = pi.dwProcessId
CloseHandle pi.hProcess
CloseHandle pi.hThread
End Function
上一页 [1] [2]
责任编辑:小草