各位仁兄,在VB中如何得到命令行的返回值?谢谢!
比如说调用用VB调用NET SEND
Shell ("command.com /c net send " & Text1.Text & " " & Text2.Text), vbHide如何才能得到NET SEND的返回值呢?那如果使用PING呢?如何才可以在VB的窗体中显示下面这些呢?Pinging starrib [127.0.0.1] with 32 bytes of data:Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
Reply from 127.0.0.1: bytes=32 time<10ms TTL=128Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  0ms, Average =  0ms

解决方案 »

  1.   

    Command 函数用于获得命令行参数示例
    本示例在某个函数中用 Command 函数获得命令行参数,并将命令行参数以 Variant 类型之数组返回。
    Function GetCommandLine(Optional MaxArgs)
       '声明变量。
       Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
       '检查是否提供了 MaxArgs 参数。
       If IsMissing(MaxArgs) Then MaxArgs = 10
       ' 使数组的大小合适。
       ReDim ArgArray(MaxArgs)
       NumArgs = 0: InArg = False
       '取得命令行参数。
       CmdLine = Command()
       CmdLnLen = Len(CmdLine)
       '以一次一个字符的方式取出命令行参数。
       For I = 1 To CmdLnLen
          C = Mid(CmdLine, I, 1)
          '检测是否为 space 或 tab。
          If (C <> " " And C <> vbTab) Then
             '若既不是 space 键,也不是 tab 键,
             '则检测是否为参数内含之字符。
             If Not InArg Then
             '新的参数。
             '检测参数是否过多。
                If NumArgs = MaxArgs Then Exit For
                   NumArgs = NumArgs + 1
    InArg = True
                End If
             '将字符连接到当前参数中。
             ArgArray(NumArgs) = ArgArray(NumArgs) & C
          Else
             '找到 space 或 tab。
             '将 InArg 标志设置成 False。
             InArg = False
          End If
       Next I
       '调整数组大小使其刚好符合参数个数。
       ReDim Preserve ArgArray(NumArgs)
       '将数组返回。
       GetCommandLine = ArgArray()
    End Function
      

  2.   

    哦,谢谢,这是MSDN里面的那个,
    我试过了,总调试不成功。
      

  3.   

    我想他指的是如何取得命令行启动的应用程序的Dos屏幕输出值,记得好像Win下的Dos输出分两个“屏幕”,一个是错误输出,一个是过程输出,只要取得相应的“屏幕”地址,就可获得输出,但很复杂:(
    你可以试试重定向符">",让屏幕输出先输入一个文件,再解读此文件,试试看
      

  4.   

    加入以下代码Private Sub Form_Load()
        If Command() <> "" Then MsgBox Command()
    End Sub需要简单设置,在工程属性属性页中有个make 项,就是通用边上那项
    里面有个命令行参数设置,输入哈哈即可运行后即可弹出对话框,显示哈哈
      

  5.   

    对,我是想问如何来取得命令行启动的应用程序的Dos屏幕输出值。TO:SoHo_Andy(冰)你帮我贴的那个代码是这个的吗?我不知道该怎么用。请具体说一下。小弟在此先谢谢了。
      

  6.   

    Shell ("command.com /c net send " & Text1.Text & " " & Text2.Text & ">C:\temp.text"), vbHide
      

  7.   

    TO: lxcc(虫子)好,谢谢,但是有没有什么API可以直接截获的?
      

  8.   

    你要的是在DOS控制台上执行命令后,再获得返回值吗?
    Option ExplicitPrivate Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
    Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
    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 SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Type SECURITY_ATTRIBUTES
      nLength As Long
      lpSecurityDescriptor As Long
      bInheritHandle As Long
    End TypePrivate Type PROCESS_INFORMATION
      hProcess As Long
      hThread As Long
      dwProcessId As Long
      dwThreadId As Long
    End TypePrivate Type STARTUPINFO
      cb As Long
      lpReserved As Long
      lpDesktop As Long
      lpTitle As Long
      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 Byte
      hStdInput As Long
      hStdOutput As Long
      hStdError As Long
    End TypePrivate Type OVERLAPPED
        ternal As Long
        ternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
    End TypePrivate Const STARTF_USESHOWWINDOW = &H1
    Private Const STARTF_USESTDHANDLES = &H100
    Private Const SW_HIDE = 0
    Private Const EM_SETSEL = &HB1
    Private Const EM_REPLACESEL = &HC2Public Function ExcuComm(ByVal strCommand As String, ByRef objBuffer As Object) As Boolean
        '* ------------------------------------------------------------------------------
        '* 目的 :       用于执行Saprouter的命令
        '* 传值参数 :   要执行的命令(变量中包括路经、命令、参数)
        '* 传地址参数 : 将命令的执行结果传回
        '* 返回值 :     布尔型值,表明函数是否成功处理
        '* 输出 :
        '* 注解 :
        '* 用法 :
        '* 修订版 :     2003-09-16 崔迪明 - 原始
        '* ------------------------------------------------------------------------------
        On Error GoTo ErrorHandler
        Dim i%, t$
        Dim pa As SECURITY_ATTRIBUTES
        Dim pra As SECURITY_ATTRIBUTES
        Dim tra As SECURITY_ATTRIBUTES
        Dim pi As PROCESS_INFORMATION
        Dim sui As STARTUPINFO
        Dim hRead As Long
        Dim hWrite As Long
        Dim bRead As Long
        Dim lpBuffer(1024) As Byte
        
        ExcuComm = False
        
        pa.nLength = Len(pa)
        pa.lpSecurityDescriptor = 0
        pa.bInheritHandle = True
        
        pra.nLength = Len(pra)
        tra.nLength = Len(tra)
        
        If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
            sui.cb = Len(sui)
            GetStartupInfo sui
            sui.hStdOutput = hWrite
            sui.hStdError = hWrite
            sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
            sui.wShowWindow = SW_HIDE
            
            '* 执行命令,并将结果放入文件中
            If CreateProcess(vbNullString, strCommand, pra, tra, True, 0, Null, vbNullString, sui, pi) <> 0 Then            '* 读取文件,并写入缓存对象中
                SetWindowText objBuffer.hwnd, ""
                Do
                  Erase lpBuffer()
                  If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
                    SendMessage objBuffer.hwnd, EM_SETSEL, -1, 0
                    SendMessage objBuffer.hwnd, EM_REPLACESEL, False, lpBuffer(0)
                    DoEvents
                  Else
                    CloseHandle pi.hThread
                    CloseHandle pi.hProcess
                    Exit Do
                  End If
                  CloseHandle hWrite
                Loop
                CloseHandle hRead
            End If
        End If
        ExcuComm = True
    CleanExit:
        Exit Function
    ErrorHandler:
        GoTo CleanExit
    End FunctionPrivate Sub Command1_Click()
        Text2.Text = ""
        Call ExcuComm(Text1.Text, Text2)
    End Sub
    Private Sub Form_Load()
    Text1.Text = "ping 181.9.201.10"End Sub
      

  9.   

    请问你的代码是做什么用的,我试了,可是text2里一点反应也没有
      

  10.   

    好象也是把执行结果写入一个文件,然后读取,
    那用shell "ping 127.0.0.1 >1.txt"不是一样
      

  11.   

    学习ing 谢谢 sacredwarrior(sacredwarrior)的代码。
      

  12.   


    谢谢sacredwarrior(sacredwarrior)我要的就是这个。同样感谢 SoHo_Andy(冰)我把帖子结了。