Hi everyone,

I am converting a DOS program to MASM32, and need a routine that loads a data file into a buffer when the program is first started.
I have come up with the following code and would like an opinion on whether it is O.K. or is there a better way?
The code is executed when Windows posts an WM_ONCREATE message.

sz Text sz Cload, "C:\Folder\ File.dat",0

invoke CreateFile, ADDR Cload, GENERIC_READ or GENERIC_WRITE,0, NULL,OPEN_EXISTING_FILE,FILE_ATTRIBUTE_NORMAL,NULL
mov Handle,eax

invoke GetFileSize,Handle,NULL
mov Filesize,eax

invoke ReadFile,Handle,ADDR buffer,Filesize, bytes_read,NULL

invoke CloseHandle,Handle

The file path is just an example & all variables & the buffer will be in the data segment.
Posted on 2001-07-21 09:39:04 by Mel
Here is a somewhat corrected and improved version:


sz Text sz Cload, "C:\Folder\ File.dat",0

invoke CreateFile, ADDR Cload, GENERIC_READ,FILE_SHARE_READ, NULL,OPEN_EXISTING_FILE,FILE_ATTRIBUTE_NORMAL,NULL

mov Handle,eax

invoke GetFileSize,Handle,NULL
mov Filesize,eax

invoke ReadFile,Handle,ADDR buffer,Filesize, ADDR bytes_read,NULL

invoke CloseHandle,Handle
Posted on 2001-07-21 13:43:00 by comrade
Afternoon, Mel.

I hope you realize that you'll have problems if 'Filesize' is larger than the 'buffer'.:)

You may wish to use 'GlobalAlloc' (or some other memory function) to dynamically create your buffer.

e.g.


.data
szTitle db 'File.dat stuff',0
Cload db 'C:\Folder\File.dat',0

.data?

buffer DWORD ? ; pointer to the read info
Handle DWORD ? ; pointer to name of the file
Filesize DWORD ? ; number of bytes to read
bytes_read DWORD ? ; pointer to number of bytes written

.code

invoke CreateFile, ADDR Cload, GENERIC_READ, FILE_SHARE_READ, \
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
mov Handle,eax

invoke GetFileSize,Handle,NULL ; get the size of the file
mov Filesize,eax ;... and save it
add eax,1 ; add room for 'NULL'
invoke GlobalAlloc, GPTR,eax ; allocate memory for the buffer
mov buffer, eax ; ...and save it
add eax, Filesize ; shift to the end of the buffer
mov eax,0 ; ... and add a 'NULL'

invoke ReadFile,Handle,[buffer],Filesize, ADDR bytes_read,NULL

invoke CloseHandle,Handle

invoke MessageBox,hWnd,[buffer],offset szTitle,MB_OK
:alright:

Sheesh! It looks like I'm coding in 'C'!:grin:

Cheers,
Scronty
Posted on 2001-07-21 21:12:44 by Scronty
;) I have been trying out your code Scronty and it works just fine, the only mod required was the Message box, in your code this displays the contents of the buffer which is not what I wanted, I shall probably mod it to say Database loaded, open, or not as the case might be. The reason I used GENERIC_READ or GENERIC_WRITE was that the file will be added to and needs to be saved later in the program, Can I do that if I don't release the file handle or do I release the handle and get a new one when required?
Posted on 2001-07-22 09:31:01 by Mel
I've changed the message box to come up with a message that the data file has loaded O.K. trouble is now that the message box appears before the main window.
I,m packing up for today, see you tomorrow.
Posted on 2001-07-22 12:38:31 by Mel
Afternoon, Mel.

I put the 'MessageBox' there so that you could visually see that the code is working correctly, even when you adjust the info in the '*.dat' file.;)

In practice, you may wish to put the message 'Database Loaded' (or 'Not Loaded!') in a StatusBar at the bottom of your Proggy.:alright:

Cheers,
Scronty
Posted on 2001-07-22 18:50:36 by Scronty
:) Thanks scronty, it was a long day yesterday and I wasn't thinking straight by the end of it. Looks like I've got a bit more work to do, and talking about work thats where I've got to go now(unfortunately),

Mel
Posted on 2001-07-23 01:35:32 by Mel