[ Home | Optimize | Current | Project | Tools | Made in France | F.A.Q | Forums | Links |Sources Codes ]

2 - WRITING INTO A FILE

For writing into a file, that supposes that the file has been opend in GENERIC_WRITE mode.

What kind of problem can you meet when writing :

The file handle is not valid,
The file has not been open with the GENERIC_WRITE mode,
Part of the file is locked,
The disk is full.

Here is an example of what such a function could be :

WriteIntoThisFile PROC  USES EBX EDI ESI,__hFile:HANDLE,__lpBuffer:LPBYTE,__dwDataSize:DWord
                  LOCAL  _dwNbOfBytesWritten:DWord

                  mov    edi,__hFile
                  mov    esi,__lpBuffer
                  mov    ebx,__dwDataSize

                  test   edi,edi                  ; The file handle is NULL ?
                  jz     @Error

                  test   esi,esi                  ; The file buffer is NULL ?
                  jz     @Error

                  test   ebx,ebx                  ; Data size to write is 0 ?
                  jz     @Error

                  INVOKE WriteFile,edi,esi,__dwDataSize,ADDR _dwNbOfBytesWritten,NULL

                  test   eax,eax                  ; Function failed
                  jz     @Exit

                  cmp    ebx,_dwNbOfBytesWritten  ; Datas written to disk ?
                  je     @Exit                    ; Yes

@Error :          xor    eax,eax

@Exit :

                  test   eax,eax
                  ret
WriteIntoThisFile ENDP

This function returns TRUE if the write operation returns successfully.
The function return FALSE if an error occured or if the file handle is NULL or the file buffer is NULL or the datas size to write is 0.

At the end of the function the ZERO flag is set if an error occured.

INVOKE WriteIntoThisFile,_FileHandle,ADDR _szBuffer,SIZEOF _szBuffer
jz     @Error

<Return>

 

[ Home | Optimize | Current | Project | Tools | Made in France | F.A.Q | Forums | Links | Sources Codes ]