winapi - Write access violation when trying to modify array -


the file resources passed function change should xor each byte value, write access violation error.

int callback winmain(hinstance hinstance, hinstance hprevinstance, pstr lpcmdline, int ncmdshow) {     hrsrc hres = findresource(null, l"file", rt_rcdata);      if (hres == null)     {         // print error     }      dword ressize = sizeofresource(null, hres);     hglobal resdata = loadresource(null, hres);     byte *file = reinterpret_cast<byte*>(lockresource(resdata));      change(file, ressize);      return 0; }  void change(byte *data, int size) {     (int = 0; < size; ++i)     {         data[i] ^= 2;     } } 

resources exist in read-only memory, cannot write them directly.

the way modify contents of resource use updateresource() (unless writefile() directly executable file on disk), can't use either of on resource belongs active running process, executable locked os.

so, way attempting allocate separate copy of resource data in writable memory, , can whatever want copy, eg:

int callback winmain(hinstance hinstance, hinstance hprevinstance, pstr lpcmdline, int ncmdshow) {     hrsrc hres = findresource(null, l"file", rt_rcdata);     if (hres == null)     {         // print error     }     else     {         dword ressize = sizeofresource(null, hres);         hglobal resdata = loadresource(null, hres);         lpvoid resdataptr = lockresource(resdata);          byte *copy = new byte[ressize];         memcpy(copy, resdataptr, ressize);         change(copy, ressize);         delete[] copy;     }      return 0; } 

Comments