this is my mini I/O MASM 'Class'. It has the basic functions, if you think i should change/add something please tell me what
(I really enjoy creating such files because they make everything easier)
Source:
io_file.asm:
Code:
;Mini I/O 'Class' in MASM by Ten$ion
IFNDEF __IO__FILE
__IO__FILE equ <1>
;== This should be normaly included in your main header ==;
;include windows.inc
;include kernel32.inc
;includelib user32.lib
;includelib kernel32.lib
;includelib user32.lib
.const
io_open proto :DWORD, :DWORD, :DWORD
io_close proto
io_read proto :DWORD, :DWORD
io_write proto :DWORD, :DWORD
io_getsize proto
io_seek proto :DWORD, :DWORD
io_tell proto
access_read equ 0 ; READONLY
access_write equ 1 ; WRITEONLY
access_readwrite equ 2 ; Read and write
seek_cur equ 0 ; Current file position
seek_set equ 1 ; File begin
seek_end equ 2 ; File end
_str macro x:VARARG
LOCAL txt
.data
txt db x,0
align 4
.code
exitm <addr txt>
endm
.data
io_handle dd 0 ; Handle to the file
io_access dd 0 ; File access
.code
;Opens a File
; > lpFileName - Name of file to open
; > lpAccess - File access
; > dwAppend - 1 => Append to the file, 0 => overwrite
io_open proc lpFileName:DWORD, lpAccess:DWORD, dwAppend:DWORD
LOCAL dwFlag:DWORD
cmp lpAccess, access_read
je @AccessRead
cmp lpAccess, access_write
je @AccessWrite
cmp lpAccess, access_readwrite
je @AccessReadWrite
xor eax, eax
ret
@AccessRead:
mov dwFlag, GENERIC_READ
jmp @Next
@AccessWrite:
mov dwFlag, GENERIC_WRITE
jmp @Next
@AccessReadWrite:
mov dwFlag, GENERIC_READ
or dwFlag, GENERIC_WRITE
jmp @Next
@Next:
invoke CreateFile, lpFileName, dwFlag, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
cmp eax, INVALID_HANDLE_VALUE
je @InvalidHandle
mov io_handle, eax
push dwFlag
pop io_access
cmp dwAppend, 0
je @NoAppend
invoke io_getsize
invoke io_seek, eax, 1
@NoAppend:
mov eax, 1
ret
@InvalidHandle:
invoke MessageBox, 0, _str('Could not open file.'), _str('Error'), MB_ICONERROR
xor eax, eax
ret
io_open endp
; Closes the file
io_close proc
cmp io_handle, 0
je @IsZero
invoke CloseHandle, io_handle
mov io_handle, 0
mov eax, 1
ret
@IsZero:
invoke MessageBox, 0, _str('Invalid handle.'), _str('Error'), MB_ICONERROR
xor eax, eax
ret
io_close endp
; Reads from the file
; > lpBuffer - Buffer to save the data
; > dwSize - Size to read
io_read proc lpBuffer:DWORD, dwSize:DWORD
LOCAL dwRead:DWORD
mov dwRead, 0
cmp io_handle, 0
je @IsZero
cmp io_access, access_write
je @InvalidAccess
invoke ReadFile, io_handle, lpBuffer, dwSize, addr dwRead, 0
cmp eax, 1
je @Done
invoke MessageBox, 0, _str('Read failed.'), _str('Error'), MB_ICONERROR
xor eax, eax
ret
@InvalidAccess:
invoke MessageBox, 0, _str('Invalid access!'), _str('Error'), MB_ICONERROR
xor eax, eax
ret
@IsZero:
invoke MessageBox,0, _str('Invalid handle.'), _str('Error'), MB_ICONERROR
xor eax, eax
@Done:
ret
io_read endp
; Writes to the file
; > lpBuffer - Buffer which holds the data you want to write into the file
; > dwSize - Size to write
io_write proc lpBuffer:DWORD, dwSize:DWORD
LOCAL dwWrite:DWORD
mov dwWrite, 0
cmp io_handle, 0
je @IsZero
cmp io_access, access_read
je @InvalidAccess
invoke WriteFile, io_handle, lpBuffer, dwSize, addr dwWrite, 0
cmp eax, 1
je @Done
invoke MessageBox, 0, _str('Write failed.'), _str('Error'), MB_ICONERROR
xor eax, eax
ret
@InvalidAccess:
invoke MessageBox, 0, _str('Invalid access!'), _str('Error'), MB_ICONERROR
xor eax, eax
ret
@IsZero:
invoke MessageBox,0, _str('Invalid handle.'), _str('Error'), MB_ICONERROR
xor eax, eax
@Done:
ret
io_write endp
; Returns the filesize
io_getsize proc
cmp io_handle, 0
je @IsZero
invoke GetFileSize, io_handle, 0
ret
@IsZero:
invoke MessageBox,0, _str('Invalid handle.'), _str('Error'), MB_ICONERROR
xor eax, eax
ret
io_getsize endp
; Sets the fileposition
; > dwPos - Position in the File
; > lpStart - From where to start ( current, begin, end )
io_seek proc dwPos:DWORD, lpStart:DWORD
LOCAL dwStart:DWORD
cmp io_handle, 0
je @IsZero
cmp lpStart, seek_cur
je @SeekCur
cmp lpStart, seek_set
je @SeekSet
cmp lpStart, seek_end
je @SeekEnd
xor eax, eax
ret
@SeekCur:
mov dwStart, FILE_CURRENT
jmp @Next
@SeekSet:
mov dwStart, FILE_BEGIN
jmp @Next
@SeekEnd:
mov dwStart, FILE_END
jmp @Next
@Next:
invoke SetFilePointer, io_handle, dwPos, 0, dwStart
mov eax, 1
ret
@IsZero:
invoke MessageBox,0, _str('Invalid handle.'), _str('Error'), MB_ICONERROR
xor eax, eax
@Done:
ret
io_seek endp
; Returns the current file position
io_tell proc
cmp io_handle, 0
je @IsZero
invoke SetFilePointer, io_handle, 0, 0, FILE_CURRENT
ret
@IsZero:
invoke MessageBox,0, _str('Invalid handle.'), _str('Error'), MB_ICONERROR
xor eax, eax
ret
io_tell endp
ENDIF
Usage:
Code:
.386
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
include user32.inc
include masm32.inc
includelib kernel32.lib
includelib user32.lib
includelib masm32.lib
include io_file.asm
.const
.data
szBuffer db MAX_PATH dup(0)
.code
main:
invoke io_open, _str('TestFile.txt'), access_write, 0
invoke io_write, _str('This is a test.'), 15
invoke io_close
invoke StdIn, addr szBuffer, MAX_PATH
invoke ExitProcess, 0
ret
end main
Regards!






