samedi 18 avril 2015

How to capture _fopen invalid (NULL) file handle?

I have two asm files: parse.asm, fileops.asm parse.exe accepts a file name in the command line argument. The file name is passed to fileops's global open_file method.


fileops implements _fopen to get a file handle. The application seems to be working fine (so far) for files that exist. However, the code fails with a file that does not exist ...which is what I am now trying to prevent.


Here is the fileops.open_file function:



open_file:
; stash base stack pointer
push ebp
mov ebp, esp

mov eax, [ebp + 8] ; location of
mov [fn], eax ; file name argument
push DWORD [fn]
push DWORD mopf
call _printf ; displ: "[FILE] open: /file_name/"
add esp, 8

; open file
push DWORD fread ; mode
push DWORD [fn]
call _fopen ; how can I check for error here: e.g., file not found, etc.
add esp, 8

push DWORD [eax] ; push result to stack
xor eax, eax ; ret 0

.done:
; restore base stack pointer
mov esp, ebp
pop ebp

ret


My understanding is that in C, fopen(...) returns a NULL to the pointer. But then there is a GetLastError() that would give me a code or something that would tell me what the problem was. How can I implement this open_file?.


At the very least, I am okay with simply returning 1 in eax if the handle is invalid (0).


Aucun commentaire:

Enregistrer un commentaire