Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Created March 10, 2015 13:21
Show Gist options
  • Save tmplinshi/0df28b8b32134cf2c63d to your computer and use it in GitHub Desktop.
Save tmplinshi/0df28b8b32134cf2c63d to your computer and use it in GitHub Desktop.
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, "CP936" )
; ? 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 )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment