invoke CreateFileMapping,hFileRead,NU CreateFileMapping,hFileRead,NULL,PAGE_READONLY, LL,PAGE_READONLY,0,0,NULL 0,0,NULL Then we call CreateFileMapping CreateFileMapping to create a memory mapped file from the opened file. CreateFileMapping has the following syntax:
CreateFileMapping proto hFile:DWORD,\ hFile:DWORD,\ lpFileMappingAttributes:DWORD,\ flProtect:DWORD,\ dwMaximumSizeHigh:DWORD,\ dwMaximumSizeLow:DWORD,\ lpName:DWORD You should know first that CreateFileMapping doesn't have to map the whole file to memory. You can use this function to map only a part of the actual file to memory. You specify the size of the memory mapped file in dwMaximumSizeHigh dwMaximumSizeHigh and dwMaximumSizeLow params. If you specify the size that 's larger than the actual file, the actual file will be expanded to the new size. If you want the memory mapped file to be the same size as the actual file, put zeroes in both params. You can use NULL in lpFileMappingAttributes lpFileMappingAttributes parameter to have Windows creates a memory mapped file with with default security attributes. attributes. flProtect defines the protection desired for the memory mapped file. In our example, we use PAGE_READONLY to allow only read operation on the memory mapped file. Note that this attribute must not contradict the attribute used used in CreateFile else CreateFileMapping CreateFileMapping will fail. lpName points to the name of the memory mapped file. If you want to share this file with other process, you must provide it a name. But in our example, our process is the only one that uses this file so we ignore this parameter.
mov eax,OFFSET buf buffer movzx edx,ofn.nF edx,ofn.nFileO ileOffset ffset a dd eax,edx invoke SetWindowText,hWnd, SetWindowText,hWnd,eax eax If CreateFileMapping is successful, we change the window caption to the name of the opened file. The filename with full path is stored in buffer, we want to display only the filename in the caption so we must add the value of nFileOffset member of the OPENFILENAME structure structure to the address of buffer.
invoke EnableMenuItem,hMenu,IDM_OPEN EnableMenuItem,hMenu,IDM_OPEN,MF_GRAYED ,MF_GRAYED invoke EnableMenuItem,hMenu,IDM_SAVE,MF_ EnableMenuItem,hMenu,IDM_SAVE,MF_ENABLED ENABLED As a precaution, we don't want the user user to open multiple multiple files at once, so so we gray out the Open Open menu item and and enable the Save menu item. EnableMenuItem is used to change the attribute of menu item. After this, we wait for the user user to select File/Save as menu menu item or close our program. If the user user chooses to close close our program, we must close the memory memory mapped file and the actual file like the code below: below:
.ELSEIF uMsg==WM_DESTRO uMsg==WM_DESTROY Y .if hMapFile!=0 call CloseMapFile .endif invoke PostQuitMessage,NULL PostQuitMessage,NULL In the above code snippet, when the window procedure receives the WM_DESTROY message, message, it checks the value of hMapFile first whether it is zero or not. If it's not zero, it calls CloseMapFile function which contains the following code:
CloseMapFile PROC invoke CloseHandle,hMapFile CloseHandle,hMapFile mov hMapFile,0 invoke CloseHandle,hFileRead CloseHandle,hFileRead ret CloseMapFile endp
72