News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Win32 invoke call problem

Started by asm4all, February 10, 2012, 10:56:27 PM

Previous topic - Next topic

asm4all

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. 


jj2007

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
.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.

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

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

dedndave

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

Tedd

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.)
No snowflake in an avalanche feels responsible.