Thread: memcpy access violation reading

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    2

    memcpy access violation reading

    Recently, i learn how to C by making a program by peeking to another public source code and i got stopped working error whenever i run it.

    I got "Access violation writing location" from debuger.

    Code:
    VOID LoadSection(HMODULE hModule, PUINT8 oBinary, IMAGE_SECTION_HEADER *section)
    {
        LPVOID targetAddress = (uint8_t*)((PUINT8)hModule + section->VirtualAddress);
        LPVOID sourceAddress = oBinary + section->PointerToRawData;
        
        if ((uintptr_t)targetAddress >= 0x140000000 + 0x60000000)
        {
            return;
        }
        
        if (section->SizeOfRawData > 0)
        {
            size_t sizeOfData = min(section->SizeOfRawData, section->Misc.VirtualSize);
            memcpy(targetAddress, sourceAddress, sizeOfData);
                // When i removed this memcpy line, i got no error on runtime.
            DWORD oldProtect;
            VirtualProtect(targetAddress, sizeOfData, PAGE_EXECUTE_READWRITE, &oldProtect);
        }
    }

  2. #2
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Try
    Code:
     #include <mem.h>
    at the beginning. Might be far fetch but worth a shot.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    2
    i already include it, still ain't work.

    Quote Originally Posted by Tien Nguyen View Post
    Try
    Code:
     #include <mem.h>
    at the beginning. Might be far fetch but worth a shot.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    What are you trying to do?
    Devoted my life to programming...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by Tien Nguyen View Post
    Try
    Code:
     #include <mem.h>
    at the beginning. Might be far fetch but worth a shot.
    Please go back to your Turbo C toys.
    No post you've made so far has any relevance to the 21st century.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation reading location
    By pelgro in forum C++ Programming
    Replies: 10
    Last Post: 02-01-2014, 08:52 AM
  2. Access violation reading location
    By erasm in forum C Programming
    Replies: 2
    Last Post: 08-24-2010, 02:50 AM
  3. Access violation reading location (Dll and Lib)
    By sarah22 in forum C++ Programming
    Replies: 13
    Last Post: 05-26-2010, 08:09 PM
  4. Access violation when reading [00000000]?
    By phal in forum Windows Programming
    Replies: 12
    Last Post: 08-19-2008, 06:03 AM
  5. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 10:25 AM

Tags for this Thread