forked from Tebayaki/AutoHotkeyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadProcessStdOut.ahk
More file actions
49 lines (42 loc) · 2 KB
/
ReadProcessStdOut.ahk
File metadata and controls
49 lines (42 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
ReadProcessStdOut(cmd, stdin := "", encoding := "cp0") {
sa := Buffer(24)
NumPut("uint", sa.Size, sa)
NumPut("ptr", 0, "uint", 1, sa, 8)
if !DllCall("CreatePipe", "ptr*", &hReadPipeOut := 0, "ptr*", &hWritePipeOut := 0, "ptr", sa, "uint", 0)
throw OSError()
DllCall("SetHandleInformation", "ptr", hReadPipeOut, "uint", 1, "uint", 0)
si := Buffer(104, 0)
NumPut("uint", si.Size, si)
NumPut("uint", 0x101, si, 60)
; NumPut("ushort", 5, si, 64) ; show window
NumPut("ptr", hWritePipeOut, si, 88)
if stdin !== "" {
if !DllCall("CreatePipe", "ptr*", &hReadPipeIn := 0, "ptr*", &hWritePipeIn := 0, "ptr", sa, "uint", 0)
throw OSError()
DllCall("SetHandleInformation", "ptr", hWritePipeIn, "uint", 1, "uint", 0)
NumPut("ptr", hReadPipeIn, si, 80)
}
if !DllCall("CreateProcessW", "ptr", 0, "str", cmd, "ptr", 0, "ptr", 0, "int", true, "uint", 0, "ptr", 0, "ptr", 0, "ptr", si, "ptr", pi := Buffer(24)) {
DllCall("CloseHandle", "ptr", hWritePipeOut)
DllCall("CloseHandle", "ptr", hReadPipeOut)
throw OSError()
}
DllCall("CloseHandle", "ptr", NumGet(pi, "ptr"))
DllCall("CloseHandle", "ptr", NumGet(pi, 8, "ptr"))
DllCall("CloseHandle", "ptr", hWritePipeOut)
if stdin !== "" {
DllCall("CloseHandle", "ptr", hReadPipeIn)
buf := Buffer(StrPut(stdin, encoding))
StrPut(stdin, buf, encoding)
if !DllCall("WriteFile", "ptr", hWritePipeIn, "ptr", buf, "uint", buf.Size, "uint*", &lpNumberOfBytesWritten := 0, "ptr", 0){
DllCall("CloseHandle", "ptr", hWritePipeIn)
throw OSError()
}
DllCall("CloseHandle", "ptr", hWritePipeIn)
}
stdout := ""
while DllCall("ReadFile", "ptr", hReadPipeOut, "ptr", buf := Buffer(4096), "uint", buf.Size, "uint*", &lpNumberOfBytesRead := 0, "ptr", 0) && lpNumberOfBytesRead
stdout .= StrGet(buf, lpNumberOfBytesRead, encoding)
DllCall("CloseHandle", "ptr", hReadPipeOut)
return stdout
}