flat assembler
Message board for the users of flat assembler.

Index > Windows > working with files in Fasm

Author
Thread Post new topic Reply to topic
xlinuks



Joined: 25 Sep 2006
Posts: 2
xlinuks 25 Sep 2006, 19:19
Hi anyone
Im pretty new to assembly and I just got to do some simple coding, and the next thing I'd like to learn is working with files, but.. I found out that there's actually not much info about that. I tried using the Iczelion's tutorial on that subject but.. I really dunno how to convert that code into fasm, cause there are a lotta '.while'-like instructions that make it hard for me to translate it into fasm code. I'd appreciate a lot any link related to working with (windows) files in Fasm..
Post 25 Sep 2006, 19:19
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 25 Sep 2006, 21:48
Well, there are 2 main methods of working with files:

1) GetPrivateProfileString/WritePrivateProfileString or other similar/related APIs - this does the work for you in INI format files

2) CreateFile, ReadFile, WriteFile, CloseHandle

So to read or write a file you can do something like:
Code:
; data
FilePath db 'C:\Test.txt',0
hFile dd 0
BytesRead dd 0
BytesWritten dd 0
Buffer rb 256
Buffer.size equ ($ - Buffer) ; Calculates the size at compile time
NULL equ 0

; code

;read
; Read-only; other apps can read too; file must exist
invoke CreateFile,FilePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
mov [hFile],eax
; fill buffer with Buffer.size bytes of data; actual bytes read put into BytesRead
invoke ReadFile,[hFile],Buffer,Buffer.size,BytesRead,NULL 
invoke CloseHandle,[hFile] ; done

;write
; write only; if the file doesn't exist, create it
invoke CreateFile,FilePath,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL mov [hFile],eax
; write Buffer.size byte from Buffer to the file; actual bytes written put into BytesWritten
invoke WriteFile,[hFile],Buffer,Buffer.size,BytesRead,NULL 
invoke CloseHandle,[hFile] ; done
    

A simple google of these API will get you the MSDN link for all the flags and stuff you will want to know later on. BytesRead and BytesWritten must point to a variable, otherwise you'll get a memory access violation.

HTH
Post 25 Sep 2006, 21:48
View user's profile Send private message Reply with quote
Garthower



Joined: 21 Apr 2006
Posts: 158
Location: Ukraine
Garthower 26 Sep 2006, 08:19
Also, you can use API _lcreat, ,_lopen, _llseek, _lclose, _lread and _lwrite. These API send from Win16, but till now are supported by all versions of OS Windows, down to Windows Vista. Their advantage - to work much easier, since they have less than parameters. Here their short descriptions:

HFILE _lcreat(

LPCSTR lpPathName, // pointer to name of file to open
int iAttribute // file attribute
);
Values of iAttribute:
0 Normal - (can be read from or written to without restriction).
1 Read only - (cannot be opened for write)
2 Hidden - (not found by directory search)
4 System - (not found by directory search)



HFILE _lopen(

LPCSTR lpPathName, // pointer to name of file to open
int iReadWrite // file access mode
);

OF_READ - Opens the file for reading only.
OF_READWRITE - Opens the file for reading and writing.
OF_WRITE - Opens the file for writing only.

The share mode can be one of the following values:

OF_SHARE_COMPAT - Opens the file in compatibility mode, enabling any process on a given computer to open the file any number of times. If the file has been opened by using any of the other share modes, _lopen fails.
OF_SHARE_DENY_NONE - Opens the file without denying other processes read or write access to the file. If the file has been opened in compatibility mode by any other process, _lopen fails.
OF_SHARE_DENY_READ - Opens the file and denies other processes read access to the file. If the file has been opened in compatibility mode or for read access by any other process, _lopen fails.
OF_SHARE_DENY_WRITE - Opens the file and denies other processes write access to the file. If the file has been opened in compatibility mode or for write access by any other process, _lopen fails.
OF_SHARE_EXCLUSIVE - Opens the file in exclusive mode, denying other processes both read and write access to the file. If the file has been opened in any other mode for read or write access, even by the current process, _lopen fails.



LONG _llseek(

HFILE hFile, // handle to file
LONG lOffset, // number of bytes to move
int iOrigin // position to move from
);

Values if iOrigin:
FILE_BEGIN - Moves the file pointer lOffset bytes from the beginning of the file.
FILE_CURRENT - Moves the file pointer lOffset bytes from its current position.
FILE_END - Moves the file pointer lOffset bytes from the end of the file.



HFILE _lclose(

HFILE hFile // handle to file to close
);

UINT _lread(

HFILE hFile, // handle to file
LPVOID lpBuffer, // pointer to buffer for read data
UINT uBytes // length, in bytes, of data buffer
);



UINT _lwrite(

HFILE hFile, // handle to file
LPCSTR lpBuffer, // pointer to buffer for data to be written
UINT uBytes // number of bytes to write
);



At an error all these functions return HFILE_ERROR, differently the value suitable on logic of function. _lcreat and _lopen return a handle of a file, _lread and _lwrite how many the bytes have been written down/is read through, _llseek returns new displacement in a file from its beginning, _lclose returns a zero if closing has passed successfully. For the greater information about these API you can read through in MSDN. Certainly, this API don't have that big set of functions, that in more modern analogues, but for the majority of the necessary manipulations with their files it's enough.
Post 26 Sep 2006, 08:19
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
xlinuks



Joined: 25 Sep 2006
Posts: 2
xlinuks 26 Sep 2006, 17:07
Thanks both for taking the time and answering that well! That's all the info I needed Smile
Post 26 Sep 2006, 17:07
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.