The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Guenther on January 24, 2012, 12:19:16 AM

Title: Opening and saving files
Post by: Guenther on January 24, 2012, 12:19:16 AM
Hello!

In Izcelions Tutorial Number 12 is a way to open and save files. The edit window is the buffer, that stores the content of the file. Only bytes that are also printable ASCII characters will be saved, the file is maybe smaller.

In Iczelions Tutrial Number 13 is another way. File Mapping is used here. But i have tried to do this without file mapping. So i took the source code of Tutorial number 12 and made the following changes:

DataInMemory db 0
FileSize dd 0
YesNo dd 0
Full db "Overwrite data in memory?",0
Empty db "There is no data in memory.",0
Free db "Use data again?",0


                        .if DataInMemory==1
                              invoke MessageBox,NULL,ADDR Full,ADDR AppName,MB_YESNO
                              mov YesNo,eax
                              .if (YesNo==IDNO)
                                    ret
                              .endif
                            invoke CloseHandle,hFile
invoke GlobalUnlock,pMemory
invoke GlobalFree,hMemory
                         .endif


                              invoke GetFileSize,hFile,NULL
                              mov FileSize,eax                             


;invoke CloseHandle,hFile
;invoke GlobalUnlock,pMemory
;invoke GlobalFree,hMemory


                              mov DataInMemory,1



                        .if DataInMemory==0
                            invoke MessageBox,NULL,ADDR Empty,ADDR AppName,MB_OK
                            ret
                        .endif   



;invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE
;mov  hMemory,eax
;invoke GlobalLock,hMemory
;mov  pMemory,eax


;invoke SendMessage,hwndEdit,WM_GETTEXT,MEMSIZE-1,pMemory



invoke WriteFile,hFile,pMemory,FileSize,ADDR SizeReadWrite,NULL
                              invoke MessageBox,NULL,ADDR Free,ADDR AppName,MB_YESNO
                              mov YesNo,eax
                              .if (YesNo==IDYES)
                                    ret
                              .endif

invoke CloseHandle,hFile
invoke GlobalUnlock,pMemory
invoke GlobalFree,hMemory
                              mov DataInMemory,0


This program works. I have it attached in a zip-folder. Maybe you know something to do better:

Best Regards,

Guenther
Title: Re: Opening and saving files
Post by: jj2007 on January 24, 2012, 01:15:40 AM
Guenther,

What is your actual question? what do you want to achieve?
You can use the same buffer for reading and writing, but what happens if the user adds some text? Then your buffer is too small...
You cannot save your file right now:
;invoke SendMessage,hwndEdit,WM_GETTEXT,MEMSIZE-1,pMemory
invoke WriteFile,hFile,pMemory,FileSize,ADDR SizeReadWrite,NULL

::)

By the way, "it works" is a dangerous strategy with Global/HeapAlloc. The snippet below writes 3350 bytes to the allocated 10 bytes of memory... and then it crashes. This is with Win Xp, the value may differ with other versions and setups.

include \masm32\include\masm32rt.inc

.code
AppName db "Masm32....", 0

start: invoke GlobalAlloc, 0, 10
mov edi, eax
push eax
mov ebx, 400-65 ; try other values
.Repeat
print str$(ebx), " "
mov ecx, 10
mov esi, offset AppName
.Repeat
movsb
dec ecx
.Until Zero?
dec ebx
.Until Zero?
print chr$(13, 10, 10)
pop eax
print eax
inkey "ok?"
exit

end start
Title: Re: Opening and saving files
Post by: Guenther on January 24, 2012, 10:40:22 AM
Hallo,

no, the User cannot add any text, i made the Line with SendMessage (...,WM_GETTEXT,...) to a comment. The saved data is the same as the read data. I want to store binary data, not only text files, and i have tried Izcelions Tutorial number 13 with Visual C++ 2008. But the Application quits.

So i began to think. Why does this happen? Maybe it is because there is no allocation of memory. But Izcelions Tutorial works with MASM32. The allocation is maybe done by the APIs, i think. But why does the application written in C and compiled with Visual C++ quit? I have no idea. So i want to do it without file mapping. And this i have done with MASM32 because it is my new hobby.

If i would uncommand the line with SendMessage (...,WM_GETTEXT,...) and i would allow the user to add some text in the edit window, i have to get the size of the string and i have to realloc the Memory. But then the not printable ASCII characters will be deleted.

But maybe there is a way to look at the files ending. If it is ".txt", then i can allow the user to add something. Then it is like tutorial number 12. And if the ending is not ".txt" then it is done like in my code. I have to think about it.

Best Regards,

Guenther