Search code examples
filesearchassemblyfasm

FindFirstFile and FindNextFile crash my program in assembly. Why?


I am writing a program that looking for files with the extension ".fly". It displays the names of the founded files, but when I try to use FindFirstFile and FindNextFile, my program crashes. I have tried with FindFirstFileA (ANSI Version of the function) , but my code crashes too. Please, give me a example of code if it's possible.

Please, thanks for your answers. Here's my source code written in FASM Assembly

format pe console 4.0
include 'C:\fasm\INCLUDE\WIN32AX.INC'

; Declare a macro to make buffers
macro dup label,lcount
 { forward
   label#:
   common
    repeat lcount
   forward
    db      0
   common
    end repeat }


.code

main:

explore_directoy:
    invoke      FindFirstFile,file_extension, FIND_STRUCT   ; find the first *.fly
    mov dword [find_handle], eax

find_more_files:
    cmp eax, 0
    je exit
    call show_file

findnextfile:
    invoke FindNextFile, dword [find_handle], FIND_STRUCT
    jmp find_more_files

show_file:
    invoke MessageBox, NULL, addr msg, addr msg_caption, MB_OK ; Easy way of outputing               the text
    ;invoke MessageBox, NULL, addr cFileName, addr msg_caption, MB_OK
    ret

exit:
    invoke ExitProcess, 0

datazone:
    end_msg     db      'End of program', 0
    msg         db      'File founded', 0
    msg_caption db      'Assembly program...', 0
    file_extension db '*.fly', 0
    find_handle     dd 0              ; handles/other stuff..
    FIND_STRUCT:                      ; find structure used with searching
        dwFileAttributes    dd 0
        ftCreationTime      dd 0,0
        ftLastAccessTime    dd 0,0
        ftLastWriteTime     dd 0,0
        nFileSizeHigh       dd 0
        nFileSizeLow        dd 0
        dwReserved0         dd 0
        dwReserved1         dd 0
        dup         cFileName, 256        ; found file buffer
        dup         cAlternate, 14

.end main

Solution

  • Your .text section isn't writable. Change

    .code
    

    to

    section '.text' readable writable executable