Hi,
I am writing masm32 console code using win32 api, the program prints an intro line and ask user name, then try to print the name, for intro I am using STD_OUTPUT_HANDLE and for getting user input I am using STD_INPUT_HANDLE. All goes well except the last greet output with username, the code is below:
=======================================================================
.Data
hInstOut HINSTANCE NULL
hInstIn HINSTANCE NULL
intro db "Hi, What is your name: ",0
greet db "Welcome to WinAssembly "
mname db 50 dup (0)
.Code
start:
Invoke GetStdHandle, STD_OUTPUT_HANDLE
Mov hInstOut, Eax
Invoke GetStdHandle, STD_INPUT_HANDLE
Mov hInstIn, Eax
Invoke WriteConsole, hInstOut, addr intro, sizeof intro, NULL, NULL
Invoke ReadConsole, hInstIn, addr mname, 50, NULL, NULL
Invoke WriteConsole, hInstOut, addr greet, 60, NULL, NULL
Invoke ExitProcess, 0
End start
========================================================
The expected last line "Welcome to WinAssembly USERNAME" is not printing.
The string greet is not zero terminated.
Oh, I see you want to concatenate the user name to it. At first glance I did not notice, but did notice after I posted.
I am not at my computer at the moment, but on my iPad. Else I would have looked at it further.... maybe later today.
Welcome back, Professor.
include \masm32\include\masm32rt.inc
.Data
hInstOut HINSTANCE NULL
hInstIn HINSTANCE NULL
intro db "Hi, What is your name: ",0
greet db "Welcome to WinAssembly "
mname db 50 dup (0)
bread dd 0
.Code
start:
Invoke GetStdHandle, STD_OUTPUT_HANDLE
Mov hInstOut, Eax
Invoke GetStdHandle, STD_INPUT_HANDLE
Mov hInstIn, Eax
Invoke WriteConsole, hInstOut, addr intro, sizeof intro, NULL, NULL
Invoke ReadConsole, hInstIn, addr mname, 50, addr bread, NULL
invoke lstrlen, addr greet
Invoke WriteConsole, hInstOut, addr greet, eax, NULL, NULL
inkey
Invoke ExitProcess, 0
end start
three things:
I used lstrlen to get the proper concatenated length of "greet", it is in eax after returning from lstrlen.
Also added variable bread for number of bytes read. mname was not getting copied into buffer.
And added macro call inkey to wait for user to press any key before exit, to pause before exiting. I am assuming that you have masm32 SDK installed. Link in upper right corner, if not.
:biggrin:
Free tip: Get yourself a stepping debugger such as ollydbg 1.10 (https://www.ollydbg.de/) or x32dbg or similar to assist in troubleshooting. Invaluable for debugging.
Hi themughal,
Here is another example, the StdOut function using GetStdHandle :
\masm32\m32lib\stdout.asm
StdOut proc lpszText:DWORD
LOCAL hOutPut :DWORD
LOCAL bWritten :DWORD
LOCAL sl :DWORD
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hOutPut, eax
invoke StrLen,lpszText
mov sl, eax
invoke WriteFile,hOutPut,lpszText,sl,ADDR bWritten,NULL
mov eax, bWritten
ret
StdOut endp
Instead of having to mess with getting all those handles and passing them to WriteFile(), I like to use the simplified functions that Steve provided for us in the MASM32 package:
INVOKE StdOut, OFFSET CompressedErrMsg
It's there if you include masm32rt.inc and takes care of getting the handles for you. All you need to provide is the offset of the string to display. (There's a complementary function that reads from the console as well, don't know it's exact name at the moment.)
I'm guessing it's StdIn().
Quote from: themughal on September 18, 2024, 12:34:13 AMInvoke ReadConsole, hInstIn, addr mname, 50, NULL, NULL
mov [edi], eax some where in ReadConsole, and edi==0
The "characters read" variable is not optional. Read the docs.