0%

MASM : MS-Windows Programming (2)

- File Manipulation

Win32 API Functions that create, read, and write to files:
CreateFileReadFileWriteFileSetFilePointer

CreateFile

CreateFile either creates a new file or opens an existing file. If successful, it returns a handle to the open file; otherwise, it returns a special constant named INVALID_HANDLE_VALUE.

  • Prototype:
    1
    2
    3
    4
    5
    6
    7
    8
    CreateFile PROTO,
    pFilename:PTR BYTE, ; ptr to filename
    desiredAccess:DWORD, ; access mode
    shareMode:DWORD, ; share mode
    lpSecurity:DWORD, ; ptr to security attribs
    creationDisposition:DWORD, ; file creation options
    flagsAndAttributes:DWORD, ; file attributes
    htemplate:DWORD ; handle to template file

desiredAccess

  • 0 (check for the existence of a file)
  • GENERIC_READ
  • GENERIC_WRITE

creationDisposition

  • CREATE_NEW (the function fails if the file already exists)
  • CREATE_ALWAYS
  • OPEN_EXISTING
  • OPEN_ALWAYS
  • TRUNCATE_EXISTING

## CreateFile Examples

Opens an existing file for reading: 讀取並打開已經存在的檔案

1
2
3
4
5
6
7
8
INVOKE CreateFile,
ADDR filename, ; ptr to filename
GENERIC_READ, ; !!!!! access mode
DO_NOT_SHARE, ; share mode
NULL, ; ptr to security attributes
OPEN_EXISTING, ; !!!!! file creation options
FILE_ATTRIBUTE_NORMAL, ; file attributes
0 ; handle to template file

Opens an existing file for writing: 寫入並打開已經存在的檔案

1
2
3
4
5
6
7
8
INVOKE CreateFile,
ADDR filename,
GENERIC_WRITE, ; !!!!! access mode
DO_NOT_SHARE,
NULL,
OPEN_EXISTING, ; !!!!!
FILE_ATTRIBUTE_NORMAL,
0

Creates a new file with normal attributes, erasing any existing file by the same name: 製造一個新檔案並刪除一樣名字的檔案

1
2
3
4
5
6
7
8
INVOKE CreateFile,
ADDR filename,
GENERIC_WRITE, ; !!!!!
DO_NOT_SHARE,
NULL,
CREATE_ALWAYS, ; !!!!! overwrite existing file
FILE_ATTRIBUTE_NORMAL,
0

ReadFile

  • ReadFile reads text from an input file
  • Prototype:
    1
    2
    3
    4
    5
    6
    ReadFile PROTO,
    handle:DWORD, ; handle to file
    pBuffer:PTR BYTE, ; ptr to buffer
    nBufsize:DWORD, ; num bytes to read
    pBytesRead:PTR DWORD, ; bytes actually read
    pOverlapped:PTR DWORD ; NULL (0) for syn mode

WriteFile

  • WriteFile writes data to a file, using an output handle. The handle can be the screen buffer handle, or it can be one assigned to a text file.
  • Prototype:
    1
    2
    3
    4
    5
    6
    WriteFile PROTO,
    fileHandle:DWORD, ; output handle
    pBuffer:PTR BYTE, ; pointer to buffer
    nBufsize:DWORD, ; size of buffer
    pBytesWritten:PTR DWORD, ; num bytes written
    pOverlapped:PTR DWORD ; NULL (0) for syn mo

SetFilePointer

  • SetFilePointer moves the position pointer of an open file. You can use it to append data to a file, and to perform random-access record processing:
  • Prototype:

    1
    2
    3
    4
    5
    SetFilePointer PROTO,
    handle:DWORD, ; file handle
    nDistanceLo:SDWORD, ; bytes to move pointer
    pDistanceHi:PTR SDWORD, ; ptr to bytes to move
    moveMethod:DWORD ; starting point
  • Example:

    1
    2
    3
    ; Move to end of file:
    INVOKE SetFilePointer,
    fileHandle,0,0,FILE_END

- Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
.data
buffer BYTE "This text is written to an output file.",0dh,0ah
bufSize DWORD ($-buffer)
errMsg BYTE "Cannot create file",0dh,0ah,0
filename BYTE "output.txt",0
fileHandle DWORD ? ; handle to output file
bytesWritten DWORD ? ; number of bytes written
.code
main PROC
INVOKE CreateFile,
ADDR filename, GENERIC_WRITE, DO_NOT_SHARE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
mov fileHandle,eax ; save file handle
.IF eax == INVALID_HANDLE_VALUE
mov edx,OFFSET errMsg ; Display error message
call WriteString
jmp QuitNow
.ENDIF
INVOKE WriteFile, ; write text to file
fileHandle, ; file handle
ADDR buffer, ; buffer pointer
bufSize, ; number of bytes to write
ADDR bytesWritten, ; number of bytes written
0 ; overlapped execution flag
INVOKE CloseHandle, fileHandle