Author Topic: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION  (Read 609 times)

wh173l13

  • Regular Member
  • *
  • Posts: 5
MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« on: December 06, 2021, 09:45:50 PM »
Hello, i want write to file, but can't open file:

Code: [Select]
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

Code: [Select]
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

  • Member
  • ****
  • Posts: 817
  • ObjAsm Developer
    • ObjAsm
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #1 on: December 06, 2021, 10:37:23 PM »
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.

Code: [Select]
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

  • Regular Member
  • *
  • Posts: 5
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #2 on: December 06, 2021, 11:21:59 PM »
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:
Code: [Select]
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

  • Member
  • *****
  • Posts: 1858
  • AMD 7-32 / i3 10-64
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #3 on: December 06, 2021, 11:40:51 PM »
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

  • Regular Member
  • *
  • Posts: 5
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #4 on: December 06, 2021, 11:53:13 PM »
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

Code: [Select]
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

  • Member
  • *****
  • Posts: 1858
  • AMD 7-32 / i3 10-64
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #5 on: December 07, 2021, 12:16:42 AM »
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

  • Member
  • ****
  • Posts: 829
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #6 on: December 07, 2021, 12:34:14 AM »
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

  • Regular Member
  • *
  • Posts: 5
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #7 on: December 07, 2021, 05:08:58 AM »
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__

  • Member
  • *****
  • Posts: 1152
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #8 on: December 30, 2021, 10:26:40 AM »
Hi, wh173l13!
Code: [Select]
; 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-file
Code: [Select]
cls
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.inc
Code: [Select]
OPTION 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__

  • Member
  • *****
  • Posts: 1152
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #9 on: January 01, 2022, 12:47:19 AM »
wh173l13,
did my example help you?

wh173l13

  • Regular Member
  • *
  • Posts: 5
Re: MASM64 CreateFile EXCEPTION_ACCESS_VIOLATION
« Reply #10 on: January 05, 2022, 03:42:20 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

Code: [Select]
; 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
« Last Edit: January 07, 2022, 08:36:36 AM by wh173l13 »