VBA 直接运行命令拿到返回值

Option Explicit
Public Type PROCESS_INFORMATION
   hProcess As Long    'スフセ莖・
   hThread As Long     'スフオトヨ゚ウフセ莖・
   dwProcessId As Long 'スフID
   dwThreadId As Long  'スフオトヨ゚ウフID
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 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 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&

Sub Test003()
    Dim a As String
    a = RunCommand("ipconfig")
End Sub

Public Function RunCommand(CommandLine As String) As String

    Dim si As STARTUPINFO
    Dim pi As PROCESS_INFORMATION
    Dim retval As Long
    Dim hRead As Long
    Dim hWrite As Long
    Dim sBuffer(0 To 63) As Byte
    Dim lgSize As Long
    Dim sa As SECURITY_ATTRIBUTES
    Dim strResult As String

    With sa
     .nLength = Len(sa)
     .bInheritHandle = 1&
     .lpSecurityDescriptor = 0&
    End With
    
    retval = CreatePipe(hRead, hWrite, sa, 0&)
    If retval = 0 Then
        Debug.Print "CreatePipe Failed"
        RunCommand = ""
        Exit Function
    End If
    
    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
    End With

    retval = CreateProcess(vbNullString, CommandLine & vbNullChar, sa, sa, 1&, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, si, pi)
    If retval Then
        WaitForSingleObject pi.hProcess, INFINITE
        Do While ReadFile(hRead, sBuffer(0), 64, lgSize, ByVal 0&)
            strResult = strResult & StrConv(sBuffer(), vbUnicode)
            Erase sBuffer()
            If lgSize <> 64 Then Exit Do
        Loop
        CloseHandle pi.hProcess
        CloseHandle pi.hThread
    Else
        Debug.Print "CreateProcess Failed" & vbCrLf
    End If
    CloseHandle hRead
    CloseHandle hWrite
    RunCommand = Replace(strResult, vbNullChar, "")
    
End Function

 

posted on 2014-04-02 22:07  鱼东鱼  阅读(1016)  评论(0编辑  收藏  举报

导航