News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

confused about the WriteFile function

Started by gelatine1, November 24, 2014, 05:56:13 AM

Previous topic - Next topic

gelatine1

I wrote some code which was supposed to write some number to a file


invoke CreateFile,addr file,GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
mov hfile,eax
invoke SetFilePointer,hfile,0,0,FILE_END

mov eax,age
mov edi,[pmem]
mov [edi],eax

invoke WriteFile,hfile,edi,1,0,0

invoke CloseHandle,hfile


It crashed. I tried to debug it and i couldn't find anything that made it crash. I remember I had done this before though so I digged into my older projects and I found out that it should've been done like this:


invoke CreateFile,addr file,GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
mov hfile,eax
invoke SetFilePointer,hfile,0,0,FILE_END

mov eax,age
mov edi,[pmem]
mov [edi],eax

xor     ecx,ecx
push    ecx
mov     edx,esp

invoke WriteFile,hfile,edi,1,edx,ecx

pop ecx

invoke CloseHandle,hfile


This code did work, but now I am just very confused about what is happening in this last code with esp and ecx and stuff and I still don't understand why my initial code did not work.
I am confused too about the meaning of the last 2 parameters of the WriteFile function.
Could anyone make this clear to me ?

Thanks in advance
Jannes

jj2007

Comments are a fantastic invention, Jannes, they help you to understand your own code ;-)

Quote from: gelatine1 on November 24, 2014, 05:56:13 AM
   push    ecx   ; create a dword on the stack
   mov     edx,esp  ; get its address
   invoke WriteFile,hfile,edi,1,edx,ecx
   pop ecx  ; NumberOfBytesWritten
   invoke CloseHandle,hfile

WriteFile(
    HANDLE hFile,   // handle to file to write to
    LPCVOID lpBuffer,   // pointer to data to write to file
    DWORD nNumberOfBytesToWrite,   // number of bytes to write
    LPDWORD lpNumberOfBytesWritten,   // pointer to number of bytes written
    LPOVERLAPPED lpOverlapped    // pointer to structure needed for overlapped I/O

gelatine1

Alright thank you :) I get it now. Just one more question, What's the point of the lpNumberOfBytesWritten that gets returned ? won't it be the same as nNumberOfBytesToWrite? Or is it possible it get's interrupted or anything similar ?

Mark44

Quote from: gelatine1 on November 24, 2014, 08:15:43 AM
Alright thank you :) I get it now. Just one more question, What's the point of the lpNumberOfBytesWritten that gets returned ? won't it be the same as nNumberOfBytesToWrite?
The two should be the same, but we don't live in a perfect world. lpNumberOfBytesWritten lets you know that all of the bytes actually got written.
Quote from: gelatine1Or is it possible it get's interrupted or anything similar ?

Gunther

Hi Mark,

Quote from: Mark44 on December 07, 2014, 03:55:19 AM
The two should be the same, but we don't live in a perfect world. lpNumberOfBytesWritten lets you know that all of the bytes actually got written.

that's right. Fine to see you again here after a long break.  :t

Gunther
You have to know the facts before you can distort them.