The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: asm4all on February 10, 2012, 10:56:27 PM

Title: Win32 invoke call problem
Post by: asm4all on February 10, 2012, 10:56:27 PM
I've just started using Hutch's QuickEditor 4.0g, and here's my specific problem:

In the .data section, I've defined:

FileName db "e:\temp2\TestFileFromMASM",0
hPosFile HANDLE 0

In a PROC later in the program, I have:

invoke CreateFile,ADDR FileName,GENERIC_WRITE,0,\
      NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,NULL
mov hPosFile,eax

The program assembles without any problems, but these two lines cause the program to crash, and I can't debug to find out why. 

Any ideas would be appreciated. 

Title: Re: Win32 invoke call problem
Post by: jj2007 on February 10, 2012, 11:04:12 PM
Post the complete code, and we may be able to help you.

You can also replace everything, from headers (.model.... includelib) to ... end start with a simple

include \masm32\MasmBasic\MasmBasic.inc   ; download (http://www.masm32.com/board/index.php?topic=12460)
.data
FileName   db "e:\temp2\TestFileFromMASM",0
hPosFile   HANDLE 0

   Init

   mov edx, offset FileName
   deb 1, "before CreateFile:", eax, $edx

   invoke CreateFile,ADDR FileName,GENERIC_WRITE,0,\
   NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,NULL
   mov hPosFile,eax

   deb 1, "CreateFile result:", $Err$(), hPosFile

   Exit
end start

For most apps, this is much easier to use than Olly. It works with qEditor, too.
Title: Re: Win32 invoke call problem
Post by: hutch-- on February 11, 2012, 02:21:33 AM
Its a bit hard to track the problem when there is not enough source code to do it but its a pretty basic API call and it will depend on the validity of the input data.

Here is a quick test result.

      FileName db "e:\temp2\TestFileFromMASM",0

      ; FileName db "TestFileFromMASM",0


One my dev box the path does not exist and the function fails, remove the path and the function succeeds.

If the directory does not exist, you most probably need to create it first.
Title: Re: Win32 invoke call problem
Post by: dedndave on February 11, 2012, 04:09:04 AM
QuoteCREATE_NEW Creates a new file, only if it does not already exist.
If the specified file exists, the function fails...

i generally use CREATE_ALWAYS - however, CREATE_NEW may be what you want

at any rate, if CreateFile returns INVALID_HANDLE_VALUE, you can use GetLastError to help you troubleshoot the code
Title: Re: Win32 invoke call problem
Post by: dedndave on February 11, 2012, 04:20:23 AM
for console mode...
invoke CreateFile,ADDR FileName,GENERIC_WRITE,0,\
      NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,NULL
cmp eax,INVALID_HANDLE_VALUE
jnz @F

print LastError$(),13,10
jmp ExitProgram

@@: mov hPosFile,eax


for GUI mode...
invoke CreateFile,ADDR FileName,GENERIC_WRITE,0,\
      NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,NULL
cmp eax,INVALID_HANDLE_VALUE
jnz @F

invoke MessageBox,NULL,LastError$(),offset FileName,MB_OK
jmp ExitProgram

@@: mov hPosFile,eax
Title: Re: Win32 invoke call problem
Post by: Tedd on February 11, 2012, 05:49:00 PM
Using the given code works fine for me in isloation - the file is created and there's no crash..
My first guess would be that you're not checking whether the file was created successfully and then immediately trying to use the returned handle - which will be invalid if the call failed.

invoke CreateFile, ADDR FileName,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,NULL
.IF (eax != INVALID_HANDLE_VALUE)
    mov hPosFile,eax

    ;...do other stuff...

    invoke CloseHandle, hPosFile
.ELSE
    ;error "Unable to create file."
.ENDIF



If the crash is stopping on the CreateFile call, we need to see your code (or at least a cut-down version that still crashes.)