Stealthily get cmd output without temp files or clipboard Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 08:42

But now try it with msgbox and try to make it human-readable.
MsgBox can not do that. And that's the end of that story, unless you hook into Windows code, I guess.
But hang on for my version of human-readable output in parsed form, line by line.

code will follow shortly
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 08:56

Code: Select all

#SingleInstance, Force
pid := attach_HiddenConsole()
OnExit, close_HiddenConsole

OutPut := read_StdOut("ping google.com -n 1")

Loop, Parse, output, `n, `r
{
    Gui, Font, s14, Terminal
    Gui, Add, Text, w800, % A_Index ": " A_LoopField
    Gui, Show,, Test - Line%A_Index%
    WinWaitClose, Test - Line%A_Index%
}
ExitApp


GuiClose:
GuiEscape:
    Gui, Destroy
Return


;-------------------------------------------------------------------------------
close_HiddenConsole: ; clean up
;-------------------------------------------------------------------------------
    DetectHiddenWindows, On
    DllCall("FreeConsole")
    Process, Close, %pid%
ExitApp


;-------------------------------------------------------------------------------
read_StdOut(cmdl) { ; read StdOut from command line
;-------------------------------------------------------------------------------
    oExec := ComObjCreate("WScript.Shell").Exec(cmdl)
    While, !oExec.StdOut.AtEndOfStream
        result := oExec.StdOut.ReadAll()
    Return, result
}


;-------------------------------------------------------------------------------
attach_HiddenConsole() { ; attach a hidden console and return pid
;-------------------------------------------------------------------------------
    Detection := A_DetectHiddenWindows
    DetectHiddenWindows, On

    Run, %ComSpec% /k,, Hide UseErrorLevel, pid
    WinWait, ahk_pid %pid%
    DllCall("AttachConsole", "UInt", pid)

    DetectHiddenWindows, %Detection%
    Return, pid
}
garry
Posts: 3852
Joined: 22 Dec 2013, 12:50

Re: Stealthily get cmd output without temp files or clipboar

12 Feb 2015, 16:18

thank you all for help

Code: Select all

MODIFIED=20150212

DetectHiddenWindows On
Run, %ComSpec%,, Hide, pid
WinWait, ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
Wsh := ComObjCreate("Wscript.Shell")

gosub,List1

;GUI,2:+AlwaysOnTop
Gui,2: Color, ControlColor, Black
;Gui,2:font,,Fixedsys
;Gui,2:font,s10,Terminal
Gui,2:font,s10,Lucida Console

Gui,2:add,Listbox,x5   y10  w200  h800 vLB1 cWhite gExec1,%e1x%
Gui,2:Show       ,x10  y10  w1080 H850
Gui,2:Add,Edit   ,x210 y10  W780  H800 cYellow vE1,
return
;-----------------------------------------------------

exec1:
Gui,2:submit,nohide
GuiControl,2:,E1,
if lb1 contains ping
   goto,exec2
exec  := Wsh.Exec("cmd /c " . LB1 )
output:= exec.StdOut.ReadAll()
GuiControl,2:,E1,%output%
return
;------------------------------

exec2:
Gui,2:submit,nohide
GuiControl,2:,E1,
exec  := Wsh.Exec("cmd /c " . LB1 )
while,!Exec.StdOut.AtEndOfStream
{
GuiControlGet,E1
output:=Exec.StdOut.readline()
GuiControl,2:,E1,%e1%`n%output%
}
return
;------------------------------

2Guiclose:
DllCall("FreeConsole")
Process Exist, %pid%
if (ErrorLevel == pid)
    Process, Close, %pid%
exitapp
;------------------------------

list1:
e1x =
(Ltrim Join|
help
ver
date /t
time /t
ping -n 3 -w 1000 www.google.com
at /?
)
return
;====================================================
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Stealthily get cmd output without temp files or clipboar

08 Mar 2015, 14:59

I'm using SKAN's version of StdOutToVar() from here: http://www.autohotkey.com/board/topic/9 ... douttovar/

I am also having trouble with special characters. But it's not because the GUI can't display them. I know this because when I send the output of my query to a file and load the data from that file into my script and display it, I see all the special UTF-8 characters. When I redirect the console output using StdOutToVar() instead, each UTF-8 character shows up as 2 characters of gibberish (while everything else looks fine). It is mimicking exactly what happens if I let the output of a query go right to the console -- it also show two-character gibberish for each UTF-8 character. I know the query output is correct because it is correct when sent to a file. It's going through StdOut that is causing the issue.

It seems like there needs to be a setting for StdOut to be able to handle UTF-8 correctly. If the answer is in this thread, I can't find it. Does anyone know of a way to do this? If I can't solve this, I will have to fall back to directing the output of my queries to a file and reading from that, but I'd really like to read it straight into a variable. Not only is the hard drive access for writing and reading that file eliminated, but my script doesn't have to try to sense when the query is finished writing to the file, which I'm worried may be unreliable.

Perhaps after reading it into a variable, I can have my script do a transformation on the string to UTF-8. Is there a relatively easy way to do that?
garry
Posts: 3852
Joined: 22 Dec 2013, 12:50

Re: Stealthily get cmd output without temp files or clipboar

09 Mar 2015, 03:10

Perhaps after reading it into a variable, I can have my script do a transformation on the string to UTF-8. Is there a relatively easy way to do that?
sorry , I have no answer
http://www.autohotkey.com/board/topic/9 ... douttovar/
thank you for the link, I'll try them
just another example

Code: Select all

;---------- FUNCTION StdOutToVar SKAN -----------
e4x:="
(ltrim join&
echo off
ver
vol
date /t
time /t
netstat -an
path"
)

aaf:=% StdOutToVar(comspec " /k " e4x)
;aaf:=% StdOutToVar("ping -n 4 -w 1000 http://www.google.com")
msgbox,%aaf%
return
;---------------------------------------------------

;=============== function SKAN ============================================================
;- http://www.autohotkey.com/board/topic/96903-simplified-versions-of-seans-stdouttovar/
StdOutToVar( sCmd ) { ;  GAHK32 ; Modified Version : SKAN 05-Jul-2013  http://goo.gl/j8XJXY
  Static StrGet := "StrGet"     ; Original Author  : Sean 20-Feb-2007  http://goo.gl/mxCdn

  DllCall( "CreatePipe", UIntP,hPipeRead, UIntP,hPipeWrite, UInt,0, UInt,0 )
  DllCall( "SetHandleInformation", UInt,hPipeWrite, UInt,1, UInt,1 )

  VarSetCapacity( STARTUPINFO, 68, 0  )      ; STARTUPINFO          ;  http://goo.gl/fZf24
  NumPut( 68,         STARTUPINFO,  0 )      ; cbSize
  NumPut( 0x100,      STARTUPINFO, 44 )      ; dwFlags    =>  STARTF_USESTDHANDLES = 0x100
  NumPut( hPipeWrite, STARTUPINFO, 60 )      ; hStdOutput
  NumPut( hPipeWrite, STARTUPINFO, 64 )      ; hStdError

  VarSetCapacity( PROCESS_INFORMATION, 16 )  ; PROCESS_INFORMATION  ;  http://goo.gl/b9BaI

  If ! DllCall( "CreateProcess", UInt,0, UInt,&sCmd, UInt,0, UInt,0 ;  http://goo.gl/USC5a
              , UInt,1, UInt,0x08000000, UInt,0, UInt,0
              , UInt,&STARTUPINFO, UInt,&PROCESS_INFORMATION )
   Return ""
   , DllCall( "CloseHandle", UInt,hPipeWrite )
   , DllCall( "CloseHandle", UInt,hPipeRead )
   , DllCall( "SetLastError", Int,-1 )

  hProcess := NumGet( PROCESS_INFORMATION, 0 )
  hThread  := NumGet( PROCESS_INFORMATION, 4 )

  DllCall( "CloseHandle", UInt,hPipeWrite )

  AIC := ( SubStr( A_AhkVersion, 1, 3 ) = "1.0" )                   ;  A_IsClassic
  VarSetCapacity( Buffer, 4096, 0 ), nSz := 0

  While DllCall( "ReadFile", UInt,hPipeRead, UInt,&Buffer, UInt,4094, UIntP,nSz, UInt,0 )
   sOutput .= ( AIC && NumPut( 0, Buffer, nSz, "UChar" ) && VarSetCapacity( Buffer,-1 ) )
              ? Buffer : %StrGet%( &Buffer, nSz, "CP850" )

  DllCall( "GetExitCodeProcess", UInt,hProcess, UIntP,ExitCode )
  DllCall( "CloseHandle", UInt,hProcess  )
  DllCall( "CloseHandle", UInt,hThread   )
  DllCall( "CloseHandle", UInt,hPipeRead )

Return sOutput,  DllCall( "SetLastError", UInt,ExitCode )
}
;==========================================================================================
garry
Posts: 3852
Joined: 22 Dec 2013, 12:50

Re: Stealthily get cmd output without temp files or clipboar

09 Mar 2015, 15:45

I tried StdOutStream example , works fine
only had no success with (example) xcopy /? , I don't see help file

Code: Select all

;-------- http://www.autohotkey.com/board/topic/96903-simplified-versions-of-seans-stdouttovar/ ---
;---------- FUNCTION StdOutStream SKAN  / HotKeyIt -----------

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
edcol1=gray
cmd1:="ping -n 4 -w 1000 www.google.com"

GUI,2:+AlwaysOnTop
Gui,2: Color, ControlColor, Black
Gui,2: Font,   ,Fixedsys
;Gui,2: Font,s15,Terminal


Gui,2:Add, Edit  , x5     y10   w800 h510  c%edcol1% vF1,
Gui,2:add, button, x5     y540  w100 h25 gDos1  ,DOS
Gui,2:add, button, x120   y540  w100 h25 gPing1 ,Ping
Gui,2:add, button, x240   y540  w100 h25 gDate1 ,DATE
Gui,2:add, button, x360   y540  w140 h25 gXcopy1,Xcopy-Help
GUI,2:show, NA W820 H580 X20 Y0, StdOutStream
return
;----------------------------------------------------------
2GuiClose:
ExitApp

;------------------------
dos1:
Gui,2:submit,nohide
Guicontrol,2:,F1,
e4x:="
(ltrim join&
echo off
ver
vol
date /t
time /t
netstat -an
path
xcopy /?"
)
aaf:=% StdOutStream(comspec " /k " e4x)
Guicontrol,2:,F1,%aaf%
e4x=
aaf=
return
;------------------------


;------------------------
ping1:
Guicontrol,2:,F1,
aaf:=% StdOutStream(cmd1 , "StdOutStream_Callback")
StdOutStream_Callback( data, n ) {
  Static D
  D .= data
  Guicontrol,2:,F1,%d%
  if ! ( n ) {
    Tooltip % D := ""
    Return
  }
}
return
;---------------------------------------------------

date1:
Gui,2:submit,nohide
Guicontrol,2:,F1,
e5x:="
(ltrim join&
ver
date"
)
aaf:=% StdOutStream(comspec " /k " e5x)
Guicontrol,2:,F1,%aaf%
e5x=
aaf=
return
;------------------------

xcopy1:
Gui,2:submit,nohide
Guicontrol,2:,F1,
e6x:="
(ltrim join&
ver
echo off
echo xcopy /? HELP=
xcopy /?"
)
aaf:=% StdOutStream(comspec " /k " e6x)
Guicontrol,2:,F1,%aaf%
e6x=
aaf=
return
;------------------------


;========================================================================================
StdOutStream( sCmd, Callback = "" ) { ; Modified  :  SKAN 31-Aug-2013 http://goo.gl/j8XJXY
  Static StrGet := "StrGet"           ; Thanks to :  HotKeyIt         http://goo.gl/IsH1zs
                                      ; Original  :  Sean 20-Feb-2007 http://goo.gl/mxCdn

  DllCall( "CreatePipe", UIntP,hPipeRead, UIntP,hPipeWrite, UInt,0, UInt,0 )
  DllCall( "SetHandleInformation", UInt,hPipeWrite, UInt,1, UInt,1 )

  VarSetCapacity( STARTUPINFO, 68, 0  )      ; STARTUPINFO          ;  http://goo.gl/fZf24
  NumPut( 68,         STARTUPINFO,  0 )      ; cbSize
  NumPut( 0x100,      STARTUPINFO, 44 )      ; dwFlags    =>  STARTF_USESTDHANDLES = 0x100
  NumPut( hPipeWrite, STARTUPINFO, 60 )      ; hStdOutput
  NumPut( hPipeWrite, STARTUPINFO, 64 )      ; hStdError

  VarSetCapacity( PROCESS_INFORMATION, 16 )  ; PROCESS_INFORMATION  ;  http://goo.gl/b9BaI

  If ! DllCall( "CreateProcess", UInt,0, UInt,&sCmd, UInt,0, UInt,0 ;  http://goo.gl/USC5a
              , UInt,1, UInt,0x08000000, UInt,0, UInt,0
              , UInt,&STARTUPINFO, UInt,&PROCESS_INFORMATION )
   Return ""
   , DllCall( "CloseHandle", UInt,hPipeWrite )
   , DllCall( "CloseHandle", UInt,hPipeRead )
   , DllCall( "SetLastError", Int,-1 )

  hProcess := NumGet( PROCESS_INFORMATION, 0 )
  hThread  := NumGet( PROCESS_INFORMATION, 4 )

  DllCall( "CloseHandle", UInt,hPipeWrite )

  AIC := ( SubStr( A_AhkVersion, 1, 3 ) = "1.0" )                   ;  A_IsClassic
  VarSetCapacity( Buffer, 4096, 0 ), nSz := 0

  While DllCall( "ReadFile", UInt,hPipeRead, UInt,&Buffer, UInt,4094, UIntP,nSz, Int,0 ) {

   tOutput := ( AIC && NumPut( 0, Buffer, nSz, "Char" ) && VarSetCapacity( Buffer,-1 ) )
              ? Buffer : %StrGet%( &Buffer, nSz, "CP850" )

   Isfunc( Callback ) ? %Callback%( tOutput, A_Index ) : sOutput .= tOutput

  }

  DllCall( "GetExitCodeProcess", UInt,hProcess, UIntP,ExitCode )
  DllCall( "CloseHandle",  UInt,hProcess  )
  DllCall( "CloseHandle",  UInt,hThread   )
  DllCall( "CloseHandle",  UInt,hPipeRead )
  DllCall( "SetLastError", UInt,ExitCode  )

Return Isfunc( Callback ) ? %Callback%( "", 0 ) : sOutput
}
;======================== end script ======================================

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 109 guests