News:

Download Pelles C here: http://www.pellesc.se

Main Menu

WriteFileToDisc function

Started by Vortex, Yesterday at 08:16:05 PM

Previous topic - Next topic

Vortex

Simple function to write binary data to disc, 64-bit version :

include fileIO.inc

; Exit codes returned by WriteFileToDisc

; INVALID_HANDLE_VALUE equ -1 ; CreateFile API error
  WRITEFILE_ERROR      equ 0  ; WriteFile API error
  WRITEFILEDISC_OK     equ 1  ; Operation successfull

.code

WriteFileToDisc PROC pFileName:QWORD,pMemory:QWORD,nSize:QWORD PARMAREA=7*SIZEOF QWORD

;       pFileName       :   pointer to the file name
;       pMemory         :   address of the data block to write to disc
;       nSize           :   number of bytes to write

LOCAL hFile:QWORD
LOCAL BytesWritten:QWORD
LOCAL ExitCode:QWORD
LOCAL _pMemory:QWORD
LOCAL _nSize:QWORD


    mov     _pMemory,rdx
    mov     _nSize,r8
 
    invoke  CreateFile,pFileName,GENERIC_WRITE,0,0,\
            CREATE_ALWAYS,0,0

    cmp     eax,INVALID_HANDLE_VALUE
    je      _exitB
   
    mov     hFile,rax
    mov     rcx,rax
    mov     ExitCode,WRITEFILE_ERROR

    lea     r9,BytesWritten
    invoke  WriteFile,rcx,_pMemory,_nSize,r9,0
    test    rax,rax
    jz     _exitA
   
    mov     ExitCode,WRITEFILEDISC_OK

_exitA:
 
    invoke  CloseHandle,hFile
    mov     rax,ExitCode

_exitB:

    ret

WriteFileToDisc ENDP

END
Code it... That's all...