The MASM Forum

Miscellaneous => Irvine Book Questions. => Topic started by: p3tr0va on October 10, 2012, 09:58:01 AM

Title: FindFirstFile error
Post by: p3tr0va on October 10, 2012, 09:58:01 AM
Hi everyone!
I'm trying to write an app like the dir command, but when I use the FindFirstFile function
I get error:126 The specific module could not be found.
I wrote the code in c and is ok, no error.
Why? What's wrong? (the OS is windows xp sp3)

Here is in assembly (error)
.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\irvine32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\irvine32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

.data
fName               db "*.*",0
fData               WIN32_FIND_DATA <>

.data?
hFile               HANDLE ?

.code
start:
    push offset fData
    push offset fName
    call FindFirstFile
    mov  hFile,eax
    call WriteWindowsMsg     ; Irvine's proc to see error

    push 0
    call ExitProcess
end start


Here is in C (ok)
#include <stdio.h>
#include <windows.h>

int main(int argc, char *argv[])
{
char path[]="*.*";

HANDLE hdl;
WIN32_FIND_DATA fd;

hdl=FindFirstFile(path,&fd);

printf("Error: %d\n",GetLastError());
return 0;
}


Thanks.
Title: Re: FindFirstFile error
Post by: qWord on October 10, 2012, 10:10:50 AM
You must check the return value of FindFirstFile (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx):

.if eax == INVALID_HANDLE_VALUE
    call WriteWindowsMsg ; GetLastError is valid
.else
    ; success ; GetLastError has no meaning
.endif
Title: Re: FindFirstFile error
Post by: Gunther on October 10, 2012, 10:56:16 AM
Hi p3tr0va,

welcome to the forum.

Gunther
Title: Re: FindFirstFile error
Post by: jj2007 on October 10, 2012, 04:32:40 PM
Hi p3tr0va,

Welcome to the forum. qWord is right, as usual, but to demonstrate the effect, you can add this:
    invoke SetLastError, 0
    push offset fData
    push offset fName
    call FindFirstFile

Windows' logic is "if eax signals a good value, why should I bother clearing the last error?". That's why you have to check - by the way, also in C.

P.S.: You can use Kip Irvine's lib and Masm32 in parallel, see here for an example (http://masm32.com/board/index.php?topic=767.msg6585#msg6585).
Title: Re: FindFirstFile error
Post by: p3tr0va on October 11, 2012, 01:54:10 AM
That works  :t
Thanks a lot!