Easily write lines to a text file

Started by Vortex, Today at 06:38:33 PM

Previous topic - Next topic

Vortex

Hello,

Simple functions to write lines to a text file :

.386
.model flat,stdcall
option casemap:none

GENERIC_WRITE equ 40000000h
CREATE_ALWAYS equ 2

CreateFileA PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
CreateFile equ <CreateFileA>

.code

OpenFileToWrite PROC fname:DWORD

    invoke  CreateFile,fname,GENERIC_WRITE,\
            0,0,CREATE_ALWAYS,0,0
    ret

OpenFileToWrite ENDP

END

.386
.model flat,stdcall
option casemap:none

lstrlenA PROTO :DWORD
lstrlen EQU <lstrlenA>

WriteFile PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD

.code

WriteLine PROC hFile:DWORD,LineAddr:DWORD

LOCAL BytesWritten:DWORD

    invoke  lstrlen,LineAddr
    lea     edx,BytesWritten
    invoke  WriteFile,hFile,LineAddr,eax,edx,0
    ret

WriteLine ENDP

END

A quick example :

.386
.model flat,stdcall
option casemap:none

CloseHandle PROTO :DWORD
ExitProcess PROTO :DWORD

OpenFileToWrite PROTO :DWORD
WriteLine PROTO :DWORD,:DWORD

.data

fname   db 'Test.txt',0
line1   db 'This is a test.',13,10,0
line2   db 'Another test.',0

.data?

hFile   dd ?

.code

start:

    invoke  OpenFileToWrite,ADDR fname
    mov     hFile,eax

    invoke  WriteLine,eax,ADDR line1
    invoke  WriteLine,hFile,ADDR line2

    invoke  CloseHandle,hFile

    invoke  ExitProcess,0

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

TimoVJL

Those support functions are also useful for x64, when parameter count is less that five.
May the source be with you

Vortex

Hi Timo,

Attached is the 64-bit version of the functions.
Code it... That's all...