I've been learning asm for quite some time now
And I've been trying to open, and read the contents of a file for a while now
But something's wrong, and I cannot check the error codes to see what
How can I check (print) the error codes my program gives me
I've tried printing them to a console, and through a message box, the first prints a character on my screen and the second method fails
Here's the code I'm using:
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib
.data
txt db "There was an error with reading the file requested",0
capt db "SCHiM"
buffer db 1000 dup(0)
fname db "cmd.txt",0
err dd 0
.data?
hFile HANDLE <?>
BsRead dd <?>
.code
start:
invoke CreateFile, addr fname, GENERIC_ALL, 0, 0, 4, FILE_ATTRIBUTE_NORMAL, 0
mov hFile, eax
invoke ReadFile, addr hFile, addr buffer, 150, addr BsRead, 0
cmp eax, 0
jne nerr
invoke GetLastError
mov err, eax
invoke AllocConsole
invoke StdOut, addr err
;invoke MessageBox,0, er,ADDR capt,MB_OK
nerr:
invoke MessageBox, 0, addr buffer, addr capt, MB_OK
invoke ExitProcess, 0
end start
Thanks in advance
-SCHiM
Hi,
GetLastError retrurns a value, nothing more, i.e. not a pointer !
That is the reason that you can see a character on the screen.
The MessageBox needs a pointer to a buffer not a value.
Try this (for win32 errors):
BufferError db MAX_PATH dup (?)
invoke GetLastError ;returns error value in EAX
invoke FormatMessage,FORMAT_MESSAGE_FROM_SYSTEM,0,eax,0,ADDR BufferError,MAX_PATH,0 ;returns error message in BufferError
invoke MessageBox,0,ADDR BufferError,ADDR capt,MB_OK ;display error message
Thank you for your fast reply!
I'll use this from now on insead of using a debugger
But I've another guestion
Create file succeeds, and as far as I can see returns a vailid handle to my file however When I'm arriving at ReadFile I get an "ERROR_INVAILID_HANDLE"
Why is that? Nothing is wrong is there, here's the code I'm currently using:
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib
.data
txt db "There was an error with opening the file requested",0
capt db "SCHiM"
buffer db 1000 dup(0)
fname db "cmd.txt",0
err dd 0
.data?
hFile HANDLE <?>
BsRead dd <?>
.code
start:
invoke CreateFile, addr fname, GENERIC_ALL, 0, 0, 4, FILE_ATTRIBUTE_NORMAL, 0
cmp eax,INVALID_HANDLE_VALUE
jne nerr
invoke MessageBox, 0, addr txt, addr capt, MB_OK
nerr:
mov hFile, eax
invoke ReadFile, addr hFile, addr buffer, 11, addr BsRead, 0
invoke MessageBox, 0, addr buffer, addr capt, MB_OK
invoke ExitProcess, 0
end start
Solved by myself
I was having the ussual problems with reading my own code, and understanding what I was doing...
Instead of passing a handle to ReadFile() I passed a pointer to a handle: addr hFile, it should be: hFile
-SCHiM
Hi,
in the ReadFile function do not use ADDR hFile, use hFile.
When you are finished with a file operation you must always close the handle.
invoke CloseHandle,hObject
The handle (hObject) is returned by the CreateFile function in EAX.
Example:
invoke CreateFile,ADDR BufferFile,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
cmp eax,INVALID_HANDLE_VALUE
je ErrorHandling
mov hFile,eax
invoke ReadFile,hFile,pMem,FileBytesTotal,ADDR FileBytesRead,0
cmp eax,0
je ErrorHandling
cmp FileBytesRead,0
je ErrorHandling
invoke CloseHandle,hFile