News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

A program in trouble about memory-mapped files

Started by mathewzhao, September 18, 2008, 02:25:44 AM

Previous topic - Next topic

mathewzhao

I recently learned  memory-mapped files,so I programmed a program in order to deeply understand it,but some question always puzzle me.

Intention:
  I create a null text file,then the program writes the string "Hello,World!" into the text file.

Step:
  1)create a null text file at C:\MappingFile.txt
  2)input the following source code(the code is really easy),and executes it.

Q:
1: After the program run,the null text file is still a null file.The string doesn't be written into the file.
2: If I input 2 letters into the file before the program run ,I will see 'He' in the file after the program run.

.386
.Model Flat, StdCall
Option Casemap :None

include    windows.inc
include    kernel32.inc
includeLib kernel32.lib

ModifyFile PROTO  :DWORD

.data
MyFile     db 'C:\MappingFile.txt',0     
Msg        db 'Hello,World!',0         

.data?
hFile      dd  ?
hMap       dd  ?
pMapAddr   dd  ?

.code

ModifyFile proc uses ebx esi edi,lpBufferAddress:DWORD
    mov edi,lpBufferAddress     
    invoke lstrcpy,edi,addr Msg 
    ret
ModifyFile endp

START:
invoke CreateFile,ADDR MyFile,GENERIC_READ or GENERIC_WRITE,NULL, NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL                 
   
.if eax!=INVALID_HANDLE_VALUE         
mov hFile, eax               
invoke CreateFileMapping, hFile,NULL,PAGE_READWRITE,0,0,NULL
.if eax!=NULL   
                mov hMap,eax             
invoke MapViewOfFile,hMap,FILE_MAP_WRITE,0,0,NULL
.if eax!=NULL
mov pMapAddr,eax                 
invoke ModifyFile,pMapAddr     
invoke UnmapViewOfFile,pMapAddr
.endif
invoke CloseHandle,hMap       
.endif
invoke CloseHandle, hFile         
.endif
    invoke ExitProcess,0                   
END START     


bozo

i haven't run the code, but since on error no message will be displayed, how are you sure anything is written to the file?
are you stepping through the code with a debugger?

Try using FlushViewOfFile which is same as fflush() in C

zooba

If you don't specify the size of the file in CreateFileMapping, it will default to the current size.

Try this:

invoke CreateFileMapping, hFile,NULL,PAGE_READWRITE,0,256,NULL

(Keeping in mind that the extended space may contain garbage.)

Cheers,

Zooba :U

BogdanOntanu

And why mapping the file when you could simply WRITE the message text to the file?

Is this some kind of homework?
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro