下面的程序完成向本机的dns服务器中添加新的IP地址和域名的对应关系。存在这样一个问题:就是程序运行到“oShell.run(...)”这一句时,总是新打开一个命令窗口,执行完命令后关闭窗口,整个循环下来,会发现大量跳动的cmd窗口,很不爽。上网查了一下,因为oShell.run这个方法就是“建立一个新的进程,并执行”,所以会产生上述的问题。请问有没有什么方法可以让添加dns的在这些命令在同一个命令窗口中执行,先谢谢大家的帮忙!指定oShell.run的第二个参数可以实现命令窗口的隐藏,但为了获得当前命令执行的结果信息,所以不要这种方发。ipRange  = "23.23.1."dim i, oShell, resultset oShell = wscript.CreateObject ("wscript.shell")dnscmdPath = "C:\dnscmd.exe"for i = 1 to 254
        ipAddress = ipRange & i
        result = oShell.run ("cmd /c " & dnscmdPath & " 127.0.0.1 /recordadd test.com local_" & i & " A " & ipAddress, 1, true)
nextset oShell = nothing

解决方案 »

  1.   

    主要是获得输出带来的问题,那么后台直接给出输出结果就不用烦恼界面的问题了。
    以下是老外写的一个类,用来获得dos命令的执行结果。Option Explicit
    'DOSOutputs.cls
    'The CreatePipe function creates an anonymous pipe,
    'and returns handles to the read and write ends of the pipe.
    Private Declare Function CreatePipe Lib "kernel32" ( _
        phReadPipe As Long, _
        phWritePipe As Long, _
        lpPipeAttributes As Any, _
        ByVal nSize As Long) As Long'Used to read the the pipe filled by the process create
    'with the CretaProcessA function
    Private Declare Function ReadFile Lib "kernel32" ( _
        ByVal hFile As Long, _
        ByVal lpBuffer As String, _
        ByVal nNumberOfBytesToRead As Long, _
        lpNumberOfBytesRead As Long, _
        ByVal lpOverlapped As Any) As Long'Structure used by the CreateProcessA function
    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type'Structure used by the CreateProcessA function
    Private 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 Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End Type'Structure used by the CreateProcessA function
    Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessID As Long
        dwThreadID As Long
    End Type'This function launch the the commend and return the relative process
    'into the PRECESS_INFORMATION structure
    Private Declare Function CreateProcessA Lib "kernel32" ( _
        ByVal lpApplicationName As Long, _
        ByVal lpCommandLine As String, _
        lpProcessAttributes As SECURITY_ATTRIBUTES, _
        lpThreadAttributes As SECURITY_ATTRIBUTES, _
        ByVal bInheritHandles As Long, _
        ByVal dwCreationFlags As Long, _
        ByVal lpEnvironment As Long, _
        ByVal lpCurrentDirectory As Long, _
        lpStartupInfo As STARTUPINFO, _
        lpProcessInformation As PROCESS_INFORMATION) As Long'Close opened handle
    Private Declare Function CloseHandle Lib "kernel32" ( _
        ByVal hHandle As Long) As Long'Consts for the above functions
    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const STARTF_USESTDHANDLES = &H100&
    Private Const STARTF_USESHOWWINDOW = &H1
    Private mCommand As String          'Private variable for the CommandLine property
    Private mOutputs As String          'Private variable for the ReadOnly Outputs property'Event that notify the temporary buffer to the object
    Public Event ReceiveOutputs(CommandOutputs As String)'This property set and get the DOS command line
    'It's possible to set this property directly from the
    'parameter of the ExecuteCommand method
    Public Property Let CommandLine(DOSCommand As String)
        mCommand = DOSCommand
    End PropertyPublic Property Get CommandLine() As String
        CommandLine = mCommand
    End Property'This property ReadOnly get the complete output after
    'a command execution
    Public Property Get Outputs()
        Outputs = mOutputs
    End PropertyPublic Function ExecuteCommand(Optional CommandLine As String) As String
        Dim proc As PROCESS_INFORMATION     'Process info filled by CreateProcessA
        Dim ret As Long                     'long variable for get the return value of the
                                            'API functions
        Dim start As STARTUPINFO            'StartUp Info passed to the CreateProceeeA
                                            'function
        Dim sa As SECURITY_ATTRIBUTES       'Security Attributes passeed to the
                                            'CreateProcessA function
        Dim hReadPipe As Long               'Read Pipe handle created by CreatePipe
        Dim hWritePipe As Long              'Write Pite handle created by CreatePipe
        Dim lngBytesread As Long            'Amount of byte read from the Read Pipe handle
        Dim strBuff As String * 256         'String buffer reading the Pipe    'if the parameter is not empty update the CommandLine property
        If Len(CommandLine) > 0 Then
            mCommand = CommandLine
        End If
        
        'if the command line is empty then exit whit a error message
        If Len(mCommand) = 0 Then
            MsgBox "Command Line empty", vbCritical
            Exit Function
        End If
        
        'Create the Pipe
        sa.nLength = Len(sa)
        sa.bInheritHandle = 1&
        sa.lpSecurityDescriptor = 0&
        ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
        
        If ret = 0 Then
            'If an error occur during the Pipe creation exit
            MsgBox "CreatePipe failed. Error: " & Err.LastDllError, vbCritical
            Exit Function
        End If
        
        'Launch the command line application
        start.cb = Len(start)
        start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
        'set the StdOutput and the StdError output to the same Write Pipe handle
        start.hStdOutput = hWritePipe
        start.hStdError = hWritePipe
        'Execute the command
        ret& = CreateProcessA(0&, mCommand, sa, sa, 1&, _
            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
            
        If ret <> 1 Then
            'if the command is not found ....
            MsgBox "File or command not found", vbCritical
            Exit Function
        End If
        
        'Now We can ... must close the hWritePipe
        ret = CloseHandle(hWritePipe)
        mOutputs = ""
        
        'Read the ReadPipe handle
        Do
            ret = ReadFile(hReadPipe, strBuff, 256, lngBytesread, 0&)
            mOutputs = mOutputs & Left(strBuff, lngBytesread)
            'Send data to the object via ReceiveOutputs event
            RaiseEvent ReceiveOutputs(Left(strBuff, lngBytesread))
        Loop While ret <> 0
        
        'Close the opened handles
        ret = CloseHandle(proc.hProcess)
        ret = CloseHandle(proc.hThread)
        ret = CloseHandle(hReadPipe)
        
        'Return the Outputs property with the entire DOS output
        ExecuteCommand = mOutputs
    End Function
      

  2.   

    以下测试的userform例子,在EXCEL VBE中使用。
    [code=VBA]]
    VERSION 5.00
    Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} UserForm1 
       Caption         =   "DOS Outputs"
       ClientHeight    =   6660
       ClientLeft      =   45
       ClientTop       =   330
       ClientWidth     =   10560
       OleObjectBlob   =   "UserForm1.frx":0000
       StartUpPosition =   1  '所有者中心
    End
    Attribute VB_Name = "UserForm1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option ExplicitPrivate WithEvents objDOS As DOSOutputs
    Attribute objDOS.VB_VarHelpID = -1Private Sub cmdExecute_Click()
        On Error GoTo errore
        objDOS.CommandLine = txtCommand.Text
        objDOS.ExecuteCommand
        Exit Sub
    errore:
        MsgBox (Err.Description & " - " & Err.Source & " - " & CStr(Err.Number))
    End SubPrivate Sub cmdExit_Click()
        Set objDOS = Nothing
        End
    End SubPrivate Sub objDOS_ReceiveOutputs(CommandOutputs As String)
        txtOutputs.Text = txtOutputs.Text & CommandOutputs
    End SubPrivate Sub txtOutputs_Change()
        txtOutputs.SelStart = Len(txtOutputs.Text)
    End SubPrivate Sub UserForm_Initialize()
    Set objDOS = New DOSOutputs
    End Sub
    [/code]
      

  3.   

    对你的实际应用来说,只要把userform1的cmdExecute的Click事件代码
    用以下代码替换掉就可以了,当然你也可以另外设计用户界面。
    [code=VB]
    Private Sub cmdExecute_Click()
    Dim i As Integer
    Dim ipRange As String
    Dim dnscmdPath As String
    Dim ipAddress As String    On Error GoTo errore
        ipRange = "23.23.1."
        dnscmdPath = "C:\dnscmd.exe"
        With objDos
        For i = 1 To 254
            ipAddress = ipRange & i
            .CommandLine = "cmd /c " & dnscmdPath & " 127.0.0.1 /recordadd test.com local_" & i & " A " & ipAddress
            .ExecuteCommand
        Next
        Exit Sub
    errore:
        MsgBox (Err.Description & " - " & Err.Source & " - " & CStr(Err.Number))
    End Sub
    [\CODE]