This repository was archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEEexConsole.asm
233 lines (182 loc) · 6.1 KB
/
EEexConsole.asm
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
ConsoleInit PROTO
ConsoleExit PROTO
ConsoleClearScreen PROTO
ConsoleText PROTO :DWORD
ConsoleStarted PROTO
ConsoleAttach PROTO
ConsoleSendEnterKey PROTO
ReadFromPipe PROTO
.DATA
szBackslash DB "\",0
gConsoleStartedMode DD 0
dwBytesRead DD 0
TotalBytesAvail DD 0
BytesLeftThisMessage DD 0
PIPEBUFFER DB 4096 DUP (0) ;4096 DUP (0) - modified to 1 char as console output was cutting off/lagging until it 'filled' buffer size
.DATA?
SecuAttr SECURITY_ATTRIBUTES <>
hChildStd_OUT_Rd DD ?
hChildStd_OUT_Wr DD ?
hChildStd_IN_Rd DD ?
hChildStd_IN_Wr DD ?
.CODE
EEEX_ALIGN
;------------------------------------------------------------------------------
; ConsoleInit
;------------------------------------------------------------------------------
ConsoleInit PROC
Invoke ConsoleAttach
Invoke ConsoleStarted
mov gConsoleStartedMode, eax
ret
ConsoleInit ENDP
EEEX_ALIGN
;------------------------------------------------------------------------------
; ConsoleExit
;------------------------------------------------------------------------------
ConsoleExit PROC
.IF gConsoleStartedMode == TRUE
Invoke ConsoleSendEnterKey
Invoke FreeConsole
.ENDIF
ret
ConsoleExit ENDP
EEEX_ALIGN
;------------------------------------------------------------------------------
; ConsoleText
;------------------------------------------------------------------------------
ConsoleText PROC lpszConText:DWORD
LOCAL dwBytesWritten:DWORD
LOCAL dwBytesToWrite:DWORD
.IF hConOutput != 0 && lpszConText != 0
Invoke lstrlen, lpszConText
mov dwBytesToWrite, eax
Invoke WriteFile, hConOutput, lpszConText, dwBytesToWrite, Addr dwBytesWritten, NULL
mov eax, dwBytesWritten
.ELSE
xor eax, eax
.ENDIF
ret
ConsoleText ENDP
EEEX_ALIGN
;------------------------------------------------------------------------------
; ClearConsoleScreen
;------------------------------------------------------------------------------
ConsoleClearScreen PROC USES EBX
LOCAL noc:DWORD
LOCAL cnt:DWORD
LOCAL sbi:CONSOLE_SCREEN_BUFFER_INFO
.IF hConOutput != 0
Invoke GetConsoleScreenBufferInfo, hConOutput, Addr sbi
mov eax, sbi.dwSize ; 2 word values returned for screen size
; extract the 2 values and multiply them together
mov ebx, eax
shr eax, 16
mul bx
mov cnt, eax
Invoke FillConsoleOutputCharacter, hConOutput, 32, cnt, NULL, Addr noc
movzx ebx, sbi.wAttributes
Invoke FillConsoleOutputAttribute, hConOutput, ebx, cnt, NULL, Addr noc
Invoke SetConsoleCursorPosition, hConOutput, NULL
.ENDIF
ret
ConsoleClearScreen ENDP
EEEX_ALIGN
;------------------------------------------------------------------------------
; ConsoleStarted - For GUI Apps - Return TRUE if started from console or FALSE
; if started via GUI (explorer)
;------------------------------------------------------------------------------
ConsoleStarted PROC
LOCAL pidbuffer[8]:DWORD
Invoke GetConsoleProcessList, Addr pidbuffer, 4
.IF eax == 2
mov eax, TRUE
.ELSE
mov eax, FALSE
.ENDIF
ret
ConsoleStarted ENDP
EEEX_ALIGN
;------------------------------------------------------------------------------
; ConsoleSendEnterKey
;------------------------------------------------------------------------------
ConsoleSendEnterKey PROC
Invoke GetConsoleWindow
.IF eax != 0
Invoke SendMessage, eax, WM_CHAR, VK_RETURN, 0
.ENDIF
ret
ConsoleSendEnterKey ENDP
EEEX_ALIGN
;------------------------------------------------------------------------------
; ConsoleAttach
;------------------------------------------------------------------------------
ConsoleAttach PROC
Invoke AttachConsole, ATTACH_PARENT_PROCESS
ret
ConsoleAttach ENDP
EEEX_ALIGN
;------------------------------------------------------------------------------
; Read output from the child process's pipe for STDOUT
; and write to the parent process's pipe for STDOUT.
; Stop when there is no more data.
;------------------------------------------------------------------------------
ReadFromPipe PROC
LOCAL dwTotalBytesToRead:DWORD
LOCAL dwRead:DWORD
LOCAL dwWritten:DWORD
LOCAL bSuccess:DWORD
LOCAL nCount:DWORD
IFDEF DEBUG32
PrintText 'ReadFromPipe'
ENDIF
mov nCount, 0
mov bSuccess, FALSE
.WHILE TRUE
Invoke GetExitCodeProcess, pi.hProcess, Addr ProcessExitCode
.IF eax == 0
IFDEF DEBUG32
PrintText 'GetExitCodeProcess error'
Invoke GetLastError
PrintDec eax
ENDIF
.ENDIF
.IF ProcessExitCode != STILL_ACTIVE
IFDEF DEBUG32
PrintText 'Exit from ReadFromPipe::GetExitCodeProcess'
ENDIF
ret
.ENDIF
Invoke PeekNamedPipe, hChildStd_OUT_Rd, NULL, NULL, NULL, Addr dwTotalBytesToRead, NULL
.IF eax == 0
IFDEF DEBUG32
PrintText 'PeekNamedPipe Error'
Invoke GetLastError
PrintDec eax
ENDIF
.ENDIF
.IF dwTotalBytesToRead != 0
IFDEF DEBUG32
PrintDec dwTotalBytesToRead
ENDIF
Invoke ReadFile, hChildStd_OUT_Rd, Addr PIPEBUFFER, SIZEOF PIPEBUFFER, Addr dwRead, NULL
mov bSuccess, eax
.IF bSuccess == FALSE || dwRead == 0
IFDEF DEBUG32
PrintText 'Exit from ReadFromPipe::ReadFile'
ENDIF
ret
.ENDIF
Invoke WriteFile, hParentStdOut, Addr PIPEBUFFER, dwRead, Addr dwWritten, NULL
mov bSuccess, eax
.IF bSuccess == FALSE
IFDEF DEBUG32
PrintText 'Exit from ReadFromPipe::WriteFile'
ENDIF
ret
.ENDIF
.ENDIF
Invoke Sleep, 100
.ENDW
ret
ReadFromPipe ENDP