Author Topic: WriteFile access violation writing 0x0000000  (Read 312 times)

jmw457

  • Regular Member
  • *
  • Posts: 8
WriteFile access violation writing 0x0000000
« on: July 10, 2017, 10:06:55 AM »
I have two nearly identical code blocks to write a single DWord value into a file.  Both blocks successfully write the information into the
file, but after the call to the second block, I get "access violation writing 0x0000000" even though the write was successful.  The final write
is the last statement in the program, so no code after that point could have caused the error message. 

Here are the two code blocks:

mov AuditWrite,12
mov ebp,OFFSET AuditWrite
mov SizeReadWrite,4
invoke WriteFile,hFile,ebp,SizeReadWrite,ebx,NULL

mov AuditWrite,1
mov ebp,OFFSET AuditWrite
mov SizeReadWrite,4
invoke WriteFile,hFile,ebp,SizeReadWrite,edx,NULL

I don't understand why I would get an access violation after the code successfully performed the write operation. 

Thanks for any ideas on this problem. 


jmw457

  • Regular Member
  • *
  • Posts: 8
Re: WriteFile access violation writing 0x0000000
« Reply #1 on: July 10, 2017, 10:18:49 AM »
Moments later I solved the problem, and I wanted to post the solution so others will know in the future. 

For the second write (where the crash occurs), the number of bytes written is returned in edx, so I pushed edx on the stack before the call to WriteFile and popped it after the call:

mov AuditWrite,1
push edx
mov ebp,OFFSET AuditWrite
mov SizeReadWrite,4
invoke WriteFile,hFile,ebp,SizeReadWrite,edx,NULL
pop edx

and it worked. 


hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4935
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: WriteFile access violation writing 0x0000000
« Reply #2 on: July 10, 2017, 11:14:25 AM »
Unless you have set up a no stack frame procedure and have preserved EBP, using EBP is your problem. Use another register AFTER you write the PUSH / POP code to do so.

push esi

; your code

pop esi
ret
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jmw457

  • Regular Member
  • *
  • Posts: 8
Re: WriteFile access violation writing 0x0000000
« Reply #3 on: July 11, 2017, 08:46:54 AM »
Hi, Hutch,

It's a no stack frame procedure.  It works as it stands now, but I can try replacing ebp with another register anyway. 

RuiLoureiro

  • Member
  • ****
  • Posts: 671
Re: WriteFile access violation writing 0x0000000
« Reply #4 on: July 11, 2017, 08:59:12 AM »
Moments later I solved the problem, and I wanted to post the solution so others will know in the future. 

For the second write (where the crash occurs), the number of bytes written is returned in edx, so I pushed edx on the stack before the call to WriteFile and popped it after the call:

mov AuditWrite,1
push edx
;  mov ebp,OFFSET AuditWrite                                                   <<<<< removed
mov SizeReadWrite,4
invoke WriteFile,hFile, addr AuditWrite,SizeReadWrite,edx,NULL     ; <<<< no EBP
pop edx

and it worked.
If you need to use EBP do something like this:
ThisProcA       proc   ; without any parameters. If there is parameters DONT USE EBP
                     push  ebp           ; <<<< -- preserve ebp

                     ; use ebp here, there is no problem, your EBP is preserved by all other procedures

                     pop    ebp
                     ret
ThisProcA       endp
« Last Edit: July 12, 2017, 01:21:07 AM by RuiLoureiro »

jmw457

  • Regular Member
  • *
  • Posts: 8
Re: WriteFile access violation writing 0x0000000
« Reply #5 on: July 11, 2017, 09:03:32 AM »
Thank you, RuiLoureiro.  I think it's a good idea.