各位仁兄,在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
比如说调用用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
解决方案 »
- 资源文件能不能动态修改啊
- 带视频输出的显卡(video out)如何编程实现显示器和电视显示不一样的画面?
- VB开发网络版数据库管理软件用SQL Server做后台,我想问下数据并发的问题!
- 开发程序一定要数学学得好吗?算法学得差就不能做程序员吗?
- 怎么样才能使窗口一直最大化,不能还原。但可以缩小。
- 有谁知道货币数字转换成大写的标准!
- 用VB如何生成随机数的序列?
- acptvb请帮忙,如何在程序中访问需要密码的网络共享资源
- 怎么改变工具栏和状态栏的颜色和字体?(API函数或vb函数或其他)
- 精通VB6.0+SQL的大侠快来救我
- VB程序员留下MSN 让我们一同探讨问题!
- VB中使用PowerTcp VT220的问题. (如何能识别中文字符)
本示例在某个函数中用 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
我试过了,总调试不成功。
你可以试试重定向符">",让屏幕输出先输入一个文件,再解读此文件,试试看
If Command() <> "" Then MsgBox Command()
End Sub需要简单设置,在工程属性属性页中有个make 项,就是通用边上那项
里面有个命令行参数设置,输入哈哈即可运行后即可弹出对话框,显示哈哈
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
那用shell "ping 127.0.0.1 >1.txt"不是一样
谢谢sacredwarrior(sacredwarrior)我要的就是这个。同样感谢 SoHo_Andy(冰)我把帖子结了。