I want the following assembly to write Hello World!
in the current console. Currently it is working but writes Hello World!
into a new console window. I have tried removing the invoke AllocConsole
statement but it does not write anything and exits with code -1073741819
. What is the simplest way to use the existing console window when the executable is called from the terminal?
include 'win64ax.inc'
.data
tex TCHAR 'Hello World!'
dummy rd 1
.code
start:
; Don't want to allocate new console. How to use existing??
invoke AllocConsole
invoke WriteConsole, <invoke GetStdHandle, STD_OUTPUT_HANDLE>, tex, 12, dummy, 0
invoke Sleep, 1000
.end start
Are you trying to make a console app instead of a GUI app? Then just define format PE64 console
at the top of the file (instead of the format PE64 GUI
which win64ax.inc
defaults to). With this, you can just remove the call to AllocConsole
, because a console app automatically gets allocated a console by the operating system, or the console from the parent process if that is a console process.
-1073741819
is NTSTATUS
0xC0000005 STATUS_ACCESS_VIOLATION
, which you get because your function does not return (ret
) or call ExitProcess
. Note that .code
inserts a sub rsp, 8
to align the stack, so you need to insert add rsp, 8
before the return of start
to balance this out. Lastly, if you use ret
, you may want to set the exit code in rax
to 0
.
Full code:
format PE64 console
include 'win64ax.inc'
.data
tex TCHAR 'Hello World!'
.code
start:
invoke WriteConsole, <invoke GetStdHandle, STD_OUTPUT_HANDLE>, tex, 12, NULL, NULL
xor eax,eax
add rsp, 8
ret
.end start
Result:
> .\print.exe
Hello World!