Writing in a ressource without having Exception handled ?

Posted on 1998-08-09
Last Modified: 2013-11-20
Hi ..

how cvould i write into a resource i loaded (a .raw image) in my .exe with the command LoadResource() without having a :

First-chance exception in Proggy.exe: 0xC0000005: Access Violation.

i tried to the LockResource Api and still have the Access Violation , there's no right to write in it , just read .. and i want to WRITE in IT .. please , help !
Question by:SkizoWalker

Expert Comment

ID: 1320457
Please detail your question and give me your code.

Accepted Solution

jmelia earned 100 total points
ID: 1320458
The exception error mentioned is oftentimes caused by an unalloicated pointer.  
See example:

CString theAlphabetString ("AAAAAAAAAAAAAAAAAA");
CString * pPtr;

pPtr->Format (....);

Fixing the Problem:
pPtr = &theAlphabetString;

pPtr = &theAlphabetString;
pPtr->Format (....);

A complete check to ensure all pointers are initialized generally fixes this class of errors.

Here is some code to perform the operation you described.  


      struct COLORMAP
            // use DWORD instead of RGBQUAD so we can compare two GBQUADs asily
            DWORD rgbqFrom;
            int iSysColorTo;
      static const COLORMAP sysColorMap[] =
            // mapping from color in DIB to system color
            { RGB_TO_RGBQUAD(0x00, 0x00, 0x00),  COLOR_BTNTEXT },       // black
            { RGB_TO_RGBQUAD(0x80, 0x80, 0x80),  COLOR_BTNSHADOW },     // dark grey
            { RGB_TO_RGBQUAD(0xC0, 0xC0, 0xC0),  COLOR_BTNFACE },       // bright grey
            { RGB_TO_RGBQUAD(0xFF, 0xFF, 0xFF),  COLOR_BTNHIGHLIGHT }   // white
      const int nMaps = 4;

      HGLOBAL hglb;
      if ((hglb = ::LoadResource(hInst, hRsrc)) == NULL)
            return NULL;

      if (lpBitmap == NULL)
            return NULL;

      // make copy of BITMAPINFOHEADER so we can modify the color table
      const int nColorTableSize = 16;
      UINT nSize = lpBitmap->biSize + nColorTableSize * sizeof(RGBQUAD);
      if (lpBitmapInfo == NULL)
            return NULL;
      memcpy(lpBitmapInfo, lpBitmap, nSize);

      // color table is in RGBQUAD DIB format
      DWORD* pColorTable =
            (DWORD*)(((LPBYTE)lpBitmapInfo) + (UINT)lpBitmapInfo->biSize);

      for (int iColor = 0; iColor < nColorTableSize; iColor++)
            // look for matching RGBQUAD color in original
            for (int i = 0; i < nMaps; i++)
                  if (pColorTable[iColor] == sysColorMap[i].rgbqFrom)
#ifdef _MAC
                        if (bMonochrome)
                              // all colors except text become white
                              if (sysColorMap[i].iSysColorTo != COLOR_BTNTEXT)
                                    pColorTable[iColor] = RGB_TO_RGBQUAD(255, 255, 255);
                        pColorTable[iColor] =

      int nWidth = (int)lpBitmapInfo->biWidth;
      int nHeight = (int)lpBitmapInfo->biHeight;
      HDC hDCScreen = ::GetDC(NULL);
      HBITMAP hbm = ::CreateCompatibleBitmap(hDCScreen, nWidth, nHeight);
      ::ReleaseDC(NULL, hDCScreen);

      if (hbm != NULL)
            HBITMAP hbmOld = (HBITMAP)::SelectObject(hDCGlyphs, hbm);

            LPBYTE lpBits;
            lpBits = (LPBYTE)(lpBitmap + 1);
            lpBits += (1 << (lpBitmapInfo->biBitCount)) * sizeof(RGBQUAD);

            StretchDIBits(hDCGlyphs, 0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight,
                  lpBits, (LPBITMAPINFO)lpBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
            SelectObject(hDCGlyphs, hbmOld);

#ifdef _MAC
            // We don't change this bitmap any more, so get rid of the big,
            // wasteful Macintosh port
            ::SetBitmapReadOnly(hbm, BRO_READONLY);

      // free copy of bitmap info struct and resource itself

      return hbm;


Author Comment

ID: 1320459
First off all jmelia : thanx but youy dind't explained why .. jsut some source code .. i have the regret to reject it ..

okay Tangchen , here the uestion more detailed :

i have a ressource : IDR_TGAPIC which is a custom resource (a targa as you can guess)

i do this to get back the memory block where my resource is located

HRSRC res = FindResource( NULL,str,"TGA" );
unsigned char * memTGAPtr = (unsigned char *) LoadResource(NULL,res);

and when i write in my memTGAPtr pattern it cause me an exception , i can only read in my mempattern , not writing in it ..
you know everything now ..
thx ..

LVL 23

Expert Comment

ID: 1320460
The memory allocated for resource is supposed to be read-only. If you want to modify the content, allocate a memory block and copy the resource into it so that you can modify it.

Author Comment

ID: 1320461
That's what i wanted to unread .. but the fact is that Windows doens't assume that programmers are not enuff good to make them trust in their code .. bah anywa , its okay . thx .. l8er


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now