CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2010
    Posts
    94

    Thumbs up Simple Checksum question

    I created a mapped file, and mapview of a file as follows

    hMap=CreateFileMapping(hFile, 0, PAGE_READWRITE, 0,0,0);
    lpBaseAddr=(LPBYTE)MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,0);
    pImgHdr=Checksummapfile(lpBaseAddr,nFileSize,&dwOldSum,&dwNewSum);


    The third line in debuging produces error of Access Violation.
    Could someone who knows about this offers me some guidance please ?

    Thank you.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Simple Checksum question

    Either something fail so lpBaseAddr is NULL or you do something bad in Checksummapfile. Single-step your code in the debugger and check the result from every single line.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Simple Checksum question

    Access violation will typically mean that you are trying to access a relative offset in the filemapping that exceeds the mapped file.
    As in you have a file of 10 bytes, and you are trying to read/write the byte at lpBaseAddr+10

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Simple Checksum question

    Access violation is pointer error. The code snippet you posted doesn't make any check on nFileSize, hFile, hMap, lpBaseAddress though any of these handles, sizes, pointers could be invalid. I would bet that either lpBaseAddress is NULL or nFileSize is invalid and that the crash is because of that.

  5. #5
    Join Date
    Jul 2010
    Posts
    94

    Re: Simple Checksum question

    Thank you everyone for your ideas, I changed pointers into references and the problem went away.
    However, I am still stuck on the NULL return of the function, the dwOldSum always becomes zero. MSDN doesn't provide information why that is so. GetLastError after the CheckSumMappedFile also offers a value of 0 which states nothing about the extended error itself. Somebody helps me please?

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Simple Checksum question

    I did this in VS2005 and it worked alright as far as I understand it
    Code:
    DWORD hdrsum, chksum;
    DWORD retval = MapFileAndCheckSum( "../debug/console.exe", &hdrsum, &chksum );
    // retval = 0, hdrsum = 0, chksum = 0xdf68
    HANDLE hFile = CreateFile( "../debug/console.exe", GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
    DWORD size = GetFileSize( hFile, 0 ); // Don't need high-order DWORD
    HANDLE hMap  = CreateFileMapping( hFile, 0, PAGE_READONLY, 0, 0, 0 );
    LPVOID lpBaseAddr = MapViewOfFile( hMap, FILE_MAP_COPY, 0, 0, 0 );
    IMAGE_NT_HEADERS* pHdrs = CheckSumMappedFile( lpBaseAddr, size, &hdrsum, &chksum );
    // pHdrs = 0x3c00e8, hdrsum = 0, chksum = 0xdf68
    UnmapViewOfFile( lpBaseAddr ); // Unchecked return...
    CloseHandle( hMap );
    CloseHandle( hFile );
    Since all functions return success I can only presume that the original checksum is 0. In http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx it's stated
    The linker computes the original checksum at link time, if you use the appropriate linker switch. For more details, see your linker documentation.
    and, yes they were right... in my project the Set Checksum option was No so I changed it to Yes and voila!
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured