Search code examples
assemblymasmmasm32

Masm Program Not Working.(Using Win32 API - CreateFile)


This simple MASM program keeps crashing.

I'm using the Win32 API

The CreateFile function is not working and is making my app crash.

.386 
.model flat,stdcall 
option casemap:none 
include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
includelib \masm32\lib\kernel32.lib 
include \masm32\include\user32.inc 
includelib \masm32\lib\user32.lib 

.data 
filename byte "dummy.txt", 0

.code 
main PROC

    INVOKE CreateFile,
                filename,
                GENERIC_WRITE,
                0,
                NULL,
                CREATE_NEW,
                FILE_ATTRIBUTE_NORMAL,
                NULL

    invoke ExitProcess,NULL
main ENDP
END main

Can anyone help?


Solution

  • The first argument to CreateFile is the address of the filename, not the first couple of characters of the filename. So you need to place an ADDR operator before filename in your invoke:

    INVOKE CreateFile,
                ADDR filename,  
                GENERIC_WRITE,
                0,
                NULL,
                CREATE_NEW,
                FILE_ATTRIBUTE_NORMAL,
                NULL