News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION

Started by wh173l13, December 06, 2021, 09:45:50 PM

Previous topic - Next topic

wh173l13

Hello, i want write to file, but can't open file:


OPTION DOTNAME
option casemap:none

include temphls.inc
include win64.inc
include kernel32.inc
include user32.inc
includelib kernel32.lib
includelib user32.lib

.data?
hFile HANDLE ?

.data

szFilePath db "file.txt",0

.code
WinMain proc

sub rsp, 5*8


invoke CreateFile, &szFilePath, FILE_APPEND_DATA, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
mov hFile, rax

invoke CloseHandle, hFile

invoke ExitProcess, NULL

WinMain endp
end



EXCEPTION_DEBUG_INFO:
           dwFirstChance: 1
           ExceptionCode: C0000005 (EXCEPTION_ACCESS_VIOLATION)
          ExceptionFlags: 00000000
        ExceptionAddress: 0000000000D61016 hello1.0000000000D61016
        NumberParameters: 2
ExceptionInformation[00]: 0000000000000001 Write
ExceptionInformation[01]: 0000000000000030 Inaccessible Address


Screen from debugger: https://ibb.co/sjCr07N

I try many paths, temp, low, no luck. In MASM32 all work good.

Anyone know whats wrong? Thank you.

Biterider

Hi wh173l13
Check the CreateFile documentation for the correct parameter constants.
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea

The FileName must be passes as a pointer to the null terminated string, ANSI string in this case.

invoke CreateFile, offset szFilePath, GENERIC_READ or GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL

You can also use "addr szFilePath", but "offset szFilePath" is the correct way for a static string.

Biterider

wh173l13

Hi Biterider, thank you for your reply, but your code generate the same error :sad:

I was try many variants, no luck with CreateFile in MASM64 on Windows10 x64 (i was try in two computers)

compile bat:

cls
set masm64_path=C:\masm64\
set filename=Hello1

%masm64_path%bin64\ml64 /Cp /c /I"%masm64_path%Include64" %filename%.asm
%masm64_path%bin64\link /SUBSYSTEM:WINDOWS /LIBPATH:"%masm64_path%Lib64" /entry:WinMain %filename%.obj
del %filename%.obj

pause

HSE

Hi wh !

Just yesterday I was working in that problem. Package invoke macro don't know that.

See a minute ago topic Alternative macros64 : OFFSET and &.

You can modify what macros64 file you use in \masm32\include64\masm64rt.inc

And you can be the first to test the ampersand :biggrin:. I really don't use that. I have problems with OFFSET.

Regards, HSE.
Equations in Assembly: SmplMath

wh173l13

hello HSE, yes I seen your topic, cool, but "&" works for me without your macros, i get it from Hutch tutorials i think

maybe "&" already i have in temphls.inc or its in ml64


OPTION DOTNAME
option casemap:none

include temphls.inc
include win64.inc
include kernel32.inc
include user32.inc
includelib kernel32.lib
includelib user32.lib

.data?
hFile HANDLE ?

.data

szFilePath db "file.txt",0

.code
WinMain proc

sub rsp, 5*8

invoke MessageBox, NULL, &szFilePath, &szFilePath, MB_OK

invoke ExitProcess, NULL

WinMain endp
end


CreateFile not work =(

HSE

Quote from: wh173l13 on December 06, 2021, 11:53:13 PM
maybe "&" already i have in temphls.inc

:biggrin: I don't pay attention: your using Mikl__ package, not masm64 SDK.

You are not using win64a.inc, perhaps some include is missing.
Equations in Assembly: SmplMath

mineiro

hello sir wh173l13;
From your debug screen I can see that invoke is using a stack frame (rbp register). But in your code you don't create/initialize stack frame.
CreateFile have 7 parameters, 4 being passed in registers and 3 by stack. That 3 means odd number of parameters in stack. This means that stack will be unaligned again.
That works in MessageBox because this function need only 4 parameters, so, no stack use.

In most cases, when you receive this error message: "C0000005" means, ops, I need check memory or pointers.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

wh173l13

Hello sir mineiro, your answer is very helpful for me, i will learn more about stack and come back to it more powerfull :biggrin: thank you =)

Mikl__

Hi, wh173l13!
; GUI
include win64a.inc
.data
szFilePath db "file.txt",0
.code
WinMain proc
local hFile:qword

invoke CreateFile,&szFilePath,GENERIC_READ or GENERIC_WRITE,0,0,OPEN_EXISTING,\
FILE_ATTRIBUTE_NORMAL,NULL
mov hFile, rax

invoke CloseHandle,hFile
invoke ExitProcess,0
WinMain endp
end
bat-filecls
set masm64_path=\masm41\
set filename=%~n1
if exist errors.txt del errors.txt

call :read_settings %filename%
@echo %kind_of_file%
goto %kind_of_file%
:CONSOLE
if exist %filename%.exe del %filename%.exe
if exist %filename%.obj del %filename%.obj
if exist errors.txt del errors.txt
%masm64_path%bin\ml64 /Cp /c /I"%masm64_path%Include" %filename%.asm >> errors.txt
if errorlevel 1 exit
if exist %1.rc (
%masm64_path%bin\RC /r /i"%masm64_path%\Include" %filename%.rc >> errors.txt
%masm64_path%bin\link /LIBPATH:"%masm64_path%Lib" ^
/LARGEADDRESSAWARE:NO /BASE:0x400000 /STUB:%masm64_path%bin\stubby.exe ^
/SECTION:.text,W /ALIGN:16 /entry:WinMain /MERGE:.rdata=.text ^
/fixed /nocoffgrpinfo %filename%.obj %filename%.res >> errors.txt
if exist %1.res del %1.res
) else (
%masm64_path%bin\link /SUBSYSTEM:CONSOLE /LIBPATH:"%masm64_path%Lib" ^
/entry:WinMain %filename%.obj /LARGEADDRESSAWARE:NO ^
/ALIGN:16 /SECTION:.text,W /BASE:0x400000 /STUB:%masm64_path%\bin\stubby.exe >> errors.txt
)
if errorlevel 1 exit
del %filename%.obj

goto exit1
:GUI
if exist %filename%.exe del %filename%.exe
if exist %filename%.obj del %filename%.obj
if exist errors.txt del errors.txt
%masm64_path%bin\ml64 /Cp /c /I"%masm64_path%Include" %filename%.asm > errors.txt
if errorlevel 1 exit
if exist %1.rc (
%masm64_path%bin\RC /r /i"%masm64_path%\Include" %filename%.rc > errors.txt
%masm64_path%bin\link /LIBPATH:"%masm64_path%Lib" ^
/LARGEADDRESSAWARE:NO /BASE:0x400000 /STUB:%masm64_path%bin\stubby.exe ^
/SECTION:.text,W /ALIGN:16 /entry:WinMain  ^
/fixed /nocoffgrpinfo %filename%.obj %filename%.res >> errors.txt
if exist %1.res del %1.res
) else (
%masm64_path%bin\link /SUBSYSTEM:WINDOWS /LIBPATH:"%masm64_path%Lib" ^
/LARGEADDRESSAWARE:NO /BASE:0x400000 /STUB:%masm64_path%bin\stubby.exe ^
/ALIGN:16 /entry:WinMain  ^
/fixed /nocoffgrpinfo %filename%.obj >> errors.txt
)
if errorlevel 1 exit
goto exit1
:DLL
if exist %filename%.dll del %filename%.dll
%masm64_path%bin\ml64 /c /Cp /I %masm64_path%include %filename%.asm >> errors.txt
if errorlevel 1 exit
if exist %1.rc (
%masm64_path%bin\RC /r  %filename%.rc >> errors.txt
if errorlevel 1 exit
%masm64_path%bin\link /SUBSYSTEM:WINDOWS /LIBPATH:%masm64_path%lib ^
/ENTRY:DllMain /DLL /DLL /section:.bss,S /stub:%masm64_path%bin\stubby.exe  ^
%filename%.obj %filename%.res /DEF:%filename%.def >> errors.txt
if exist %1.res del %1.res
) else (
%masm64_path%bin\link /SUBSYSTEM:WINDOWS /LIBPATH:%masm64_path%lib ^
/ENTRY:DllMain /DLL /DLL /section:.bss,S /stub:%masm64_path%bin\stubby.exe  ^
%filename%.obj /DEF:%filename%.def >> errors.txt
)
if errorlevel 1 exit
del %filename%.exp
:exit1
del %filename%.obj
del errors.txt
:: %filename%.exe
exit
:read_settings
for /f "eol=# tokens=2-3" %%A in (%filename%.asm) do (
set kind_of_file=%%A
if %%B == # exit /b )
exit /b
win64a.incOPTION DOTNAME
OPTION PROLOGUE:rbpFramePrologue
OPTION EPILOGUE:none
include win64.inc
include temphls.inc
include glu32.inc
includelib glu32.lib
include glut32.inc
includelib glut32.lib
include kernel32.inc
includelib kernel32.lib
include msvcrt.inc
includelib msvcrt.lib
include ntdll.inc
includelib ntdll.lib
include oleaut32.inc
includelib oleaut32.lib
include ole32.inc
includelib ole32.lib
include opengl32.inc
includelib opengl32.lib
include user32.inc
includelib user32.lib
include advapi32.inc
includelib advapi32.lib
include avifil32.inc
includelib avifil32.lib
include msvfw32.inc
includelib msvfw32.lib
include winmm.inc
includelib winmm.lib
include dinput8.inc
includelib dinput8.lib
include freetype.inc
includelib freetype.lib
include dxva2.inc
includelib dxva2.lib
include setupapi.inc
includelib setupapi.lib

Mikl__


wh173l13

#10
Quote from: Mikl__ on January 01, 2022, 12:47:19 AM
wh173l13,
did my example help you?

Hello Mikl__ , I saw your reply only now. Your example work! It's like a magic, thank you very match =)

but its not working if hFile global, same error C0000005, you know why? i need it global, thanks again


; GUI
include win64a.inc

.data?
hFile dq ?

.data
szFilePath db "file.txt",0

.code
WinMain proc
;local hFile:qword

invoke CreateFile, &szFilePath, FILE_APPEND_DATA, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
mov hFile, rax

invoke CloseHandle,hFile
invoke ExitProcess,0
WinMain endp
end