VBscript code to capture stdout, without showing console window

前端 未结 7 617
生来不讨喜
生来不讨喜 2020-11-28 11:48

This is a VBScript code example that shows how to catch whatever a command line program sends to standard output. It executes the command xcopy /? and shows the

7条回答
  •  [愿得一人]
    2020-11-28 12:35

    If you don't mind having the taskbar button appear, you can just move the console window offscreen before launching it.

    If the HKCU\Console\WindowPosition key exists, Windows will use its value to position the console window. If the key doesn't exist, you'll get a system-positioned window.

    So, save the original value of this key, set your own value to position it offscreen, call Exec() and capture its output, then restore the key's original value.

    The WindowPosition key expects a 32-bit value. The high word is the X coordinate and the low word is the Y coordinate (XXXXYYYY).

    With CreateObject("WScript.Shell")
    
        ' Save the original window position. If system-positioned, this key will not exist.
        On Error Resume Next
        intWindowPos = .RegRead("HKCU\Console\WindowPosition")
        On Error GoTo 0
    
        ' Set Y coordinate to something crazy...
        .RegWrite "HKCU\Console\WindowPosition", &H1000, "REG_DWORD"
    
        ' Run Exec() and capture output (already demonstrated by others)...
        .Exec(...)
    
        ' Restore window position, if previously set. Otherwise, remove key...
        If Len(intWindowPos) > 0 Then
            .RegWrite "HKCU\Console\WindowPosition", intWindowPos, "REG_DWORD"
        Else
            .RegDelete "HKCU\Console\WindowPosition"
        End If
    
    End With
    

    If you really want to make sure the coordinates are offscreen, you can get the screen dimensions via VBScript by using IE or other tools.

提交回复
热议问题