News:

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

filemapping question

Started by ragdog, February 01, 2008, 08:14:17 PM

Previous topic - Next topic

ragdog

hi

i have a question to  MapViewOfFile use this fileoffsets or virtual offsets ?


example:

   invoke MapViewOfFile,hMap,FILE_MAP_READ,04Eh,0,0
   mov   lpView, eax
   
thanks
ragdog

MichaelW

According to the documentation for MapViewOfFile:

dwFileOffsetHigh is the high-order DWORD of the file offset were the view begins.

dwFileOffsetLow is the low-order DWORD of the file offset were the view begins, and the offset must also match the memory allocation granularity of the system.

As your call is coded, with 04Eh in the high-order DWORD, in my tests using the ~~1MB MASM2 windows.inc, the function fails and GetLastError returns "Access is denied", I assume because the address in nowhere in the file. If I move the 04Eh to the low-order DWORD, the function fails and GetLastError returns "The base address or the file offset specified does not have the proper alignment", because the offset does not match the memory allocation granularity of the system (10000h on my system). If I set the low-order DWORD to zero or an integral multiple of 10000h, then the function succeeds.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      hFile   dd 0
      hFMO    dd 0
      lpView  dd 0
      sysinfo SYSTEM_INFO <>
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke GetSystemInfo, ADDR sysinfo
    print "Allocation Granularity = "
    print uhex$(sysinfo.dwAllocationGranularity)," bytes",13,10,13,10

    invoke CreateFile,chr$("\masm32\include\windows.inc"),GENERIC_READ,
              NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
    mov hFile, eax
    print "hFile = "
    print uhex$(hFile),"h",13,10

    invoke CreateFileMapping,hFile,NULL,PAGE_READONLY,0,0,NULL
    mov hFMO, eax
    print "hFMO = "
    print uhex$(hFMO),"h",13,10

    invoke MapViewOfFile,hFMO,FILE_MAP_READ,0,4Eh,0
    mov lpView, eax
    print "lpView = "
    print uhex$(lpView),"h",13,10,13,10
    .IF lpView == 0
      print LastError$()
    .ENDIF

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

eschew obfuscation

ragdog

thanks for your help

by me works that not :'(


LOCAL hMap:DWORD
LOCAL lpView:DWORD
       invoke GetSystemInfo, ADDR sysinfo
              invoke CreateFile,addr sztest,GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL     
         mov hFile,eax
       
      invoke CreateFileMapping,hFile,NULL,PAGE_READONLY,0,0,NULL
         mov hMap,eax
 
      invoke MapViewOfFile,hMap,FILE_MAP_READ,0,4Eh,0
         mov lpView, eax

       invoke MessageBox,hWnd,eax,0,MB_OK
       invoke UnmapViewOfFile,lpView
       invoke CloseHandle,hFile



ragdog

MichaelW

It should not work, because the offset 4Eh does not match the memory allocation granularity of the system. I left it that way so you could build the code and then run it to see the error. You can change the code to:

invoke MapViewOfFile,hMap,FILE_MAP_READ,0,0,0

And it should succeed in mapping the entire file.
eschew obfuscation

ragdog

hmm ok

Was just a question then I work with continue setfilepointer

thanks for your replys and work

greets
ragdog