Code : Tout sélectionner
opt("RunErrorsFatal",0)
opt("MustDeclareVars",0)
#include<WinApi.au3>
#include <NamedPipes.au3>
;For 'CreateProcessWithLogonW' -> dwLogonFlags
Global Const $LOGON_WITH_PROFILE = 0x1
Global Const $LOGON_NETCREDENTIALS_ONLY = 0x2
;For'CreateProcessWithLogonW' -> dwCreationFlags
Global Const $CREATE_DEFAULT_ERROR_MODE = 0x4000000
;Les initialisations
Dim $STARTUPINFO = "",$si=""
Dim $PROCESS_INFORMATION ="",$pi=""
;
Func Msg($letexte)
ConsoleWrite($letexte)
EndFunc
msg(RunWaitStdOut())
;test1()
func test1()
$STARTUPINFO &= "dword cb;long lpReserved;long lpDesktop;long lpTitle;dword dwX;dword dwY;"
$STARTUPINFO &= "dword dwXSize;dword dwYSize;dword dwXCountChars;dword dwYCountChars;"
$STARTUPINFO &= "dword dwFillAttribute;dword dwFlags;short wShowWindow;short cbReserved2;long lpReserved2;"
$STARTUPINFO &= "long hStdInput;long hStdOutput;long hStdError"
$si = DllStructCreate($STARTUPINFO)
If $si = 0 Then
Msg(@error)
Exit
EndIf
DllStructSetData($si,"cb",DllStructGetSize($si))
$ptrSI = DllStructGetPtr($si)
;~ Msg($ptrSI)
$PROCESS_INFORMATION &= "long hProcess;long hThread;long dwProcessId;long dwThreadId"
$pi = DllStructCreate($PROCESS_INFORMATION)
If $pi = 0 Then
Msg(@error)
Exit
EndIf
$ptrPI = DllStructGetPtr($pi)
Msg($pi)
$tCommandLine = DllStructCreate("char[256]")
$ProcessName = "c:\WINDOWS\system32\notepad.exe"
DllStructSetData($tCommandLine,1,$ProcessName)
$arrRet = DllCall("advapi32.dll","long","CreateProcessWithLogonW", _
"wstr","toto", _
"wstr","", _
"wstr","tutu", _
"dword",$LOGON_WITH_PROFILE, _
"wstr",$ProcessName, _
"ptr",DllStructGetPtr($tCommandLine), _
"dword",$CREATE_DEFAULT_ERROR_MODE, _
"ptr",0, _
"ptr",0, _
"ptr",$ptrSI, _
"ptr",$ptrPI)
if $arrRet[0]=0 then
Msg("Error in Dllcall " & _WinAPI_GetLastErrorMessage())
Exit
EndIf
If Not @error and $arrRet[0]<>0 Then
$hProcess = _WinAPI_OpenProcess(0x00100000, False,
DllStructGetPtr($pi,"dwProcessId")) ; SYNCHRONIZE (better than PROCESS_ALL_ACCESS for this purpose)
_WinAPI_WaitForSingleObject($hProcess, -1)
_WinAPI_CloseHandle($hProcess)
;$arrRet = DllCall("kernel32.dll", "int", "CloseHandle",
"ptr",DllStructGetPtr($pi,"dwProcessId"))
;If Not @error and $arrRet[0]<>0 Then
; Msg("Return of 'CloseHandle' : "& $arrRet[0])
;Else
; Msg("Error (CloseHandle): "& @error)
; Msg("Return of 'CloseHandle' : "& $arrRet[0])
; Exit 1
;EndIf
Else
Msg("Error (CreateProcessWithLogonW) : "& @error)
Msg("Return of 'CreateProcessWithLogonW' : "& $arrRet[0])
Exit 2
EndIf
endfunc
;
====================================================================================================
; Execute a command and display the results
;
====================================================================================================
; Paul Campbell (PaulIA), ProgAndy, modified by KaFu
; http://www.autoitscript.com/forum/index....p?showtopic=76607&view=findpos
Func RunWaitStdOut()
Local $iBytes, $sData, $hReadPipe, $hWritePipe, $tBuffer, $tProcess,$tSecurity, $tStartup
Local $STILL_ACTIVE = 0x103
Local Const $STARTF_USESHOWWINDOW = 0x1
Local Const $STARTF_USESTDHANDLES = 0x100
local $sWorkingDir = "", $state = @SW_SHOW
; Set up security attributes
;~ $tSecurity = DllStructCreate($tagSECURITY_ATTRIBUTES)
;~ DllStructSetData($tSecurity, "Length", DllStructGetSize($tSecurity))
;~ DllStructSetData($tSecurity, "InheritHandle", True)
; Create a pipe for the child process's STDOUT
_NamedPipes_CreatePipe($hReadPipe, $hWritePipe);, $tSecurity)
;**************
_WinAPI_SetHandleInformation($hReadPipe, 1, 0) ; redundant in this new situation
_WinAPI_SetHandleInformation($hWritePipe, 1, 1)
;**************
; Create child process
$tProcess = DllStructCreate($tagPROCESS_INFORMATION)
$tStartup = DllStructCreate($tagSTARTUPINFO)
DllStructSetData($tStartup, "Size", DllStructGetSize($tStartup))
DllStructSetData($tStartup, "Flags", BitOR($STARTF_USESTDHANDLES,$STARTF_USESHOWWINDOW))
DllStructSetData($tStartup, "StdOutput", $hWritePipe)
DllStructSetData($tStartup, "StdError", $hWritePipe)
DllStructSetData($tStartup, "ShowWindow", $state)
;_WinAPI_CreateProcess("", $sCmd, 0, 0, True, 0, 0, $sWorkingDir,DllStructGetPtr($tStartup), DllStructGetPtr($tProcess))
local $tCommandLine = @comspec & " /c ping 127.0.0.1";DllStructCreate("char[256]")
local $ProcessName = @comspec
DllStructSetData($tCommandLine,1,$ProcessName)
$arrRet = DllCall("advapi32.dll","long","CreateProcessWithLogonW", _
"wstr","toto", _
"wstr","", _
"wstr","tutu", _
"dword",$LOGON_WITH_PROFILE, _
"wstr","", _
"ptr",@comspec & " /c ping 127.0.0.1", _
"dword",$CREATE_DEFAULT_ERROR_MODE, _
"ptr",0, _
"ptr",0, _
"ptr",DllStructGetPtr($tStartup), _
"ptr",DllStructGetPtr($tProcess))
;if $arrRet[0]=0 then
; Msg("Error in Dllcall " & _WinAPI_GetLastErrorMessage())
; Exit
;EndIf
msg("ok")
local $exitCode
Local $handle = DllStructGetData($tProcess, "hProcess"), $exitCode
_WinAPI_CloseHandle(DllStructGetData($tProcess, "hThread"))
Do
$exitCode = DllCall("kernel32.dll", "long","GetExitCodeProcess", "hwnd", $handle, "dword*", 0)
Until $exitCode[0] <> $STILL_ACTIVE
$exitCode = $exitCode[2]
; Close the write end of the pipe before reading from the read end of the pipe
_WinAPI_CloseHandle($handle)
_WinAPI_CloseHandle($hWritePipe)
; Read data from the child process
$tBuffer = DllStructCreate("wchar Text[4096]")
$pBuffer = DllStructGetPtr($tBuffer)
While 1
_WinAPI_ReadFile($hReadPipe, $pBuffer, 4096, $iBytes)
If $iBytes = 0 Then ExitLoop
$sData &= StringLeft(DllStructGetData($tBuffer, "Text"), $iBytes / 2)
WEnd
_WinAPI_CloseHandle($hReadPipe)
SetExtended($exitCode)
Return $sData
EndFunc ;==>_RunWaitStdOut