• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Writing in a ressource without having Exception handled ?

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 !
0
SkizoWalker
Asked:
SkizoWalker
1 Solution
 
TangChengCommented:
Please detail your question and give me your code.
0
 
jmeliaCommented:
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.  
 

HBITMAP AFXAPI LoadSysColorBitmap(HINSTANCE hInst, HRSRC hRsrc)

{
      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;

      LPBITMAPINFOHEADER lpBitmap = (LPBITMAPINFOHEADER)LockResource(hglb);
      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);
      LPBITMAPINFOHEADER lpBitmapInfo = (LPBITMAPINFOHEADER)::malloc(nSize);
      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);
                        }
                        else
#endif
                        pColorTable[iColor] =
                              CLR_TO_RGBQUAD(::GetSysColor(sysColorMap[i].iSysColorTo));
                        break;
                  }
            }
      }

      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);
#endif
      }

      // free copy of bitmap info struct and resource itself
      ::free(lpBitmapInfo);
      ::FreeResource(hglb);

      return hbm;
}



0
 
SkizoWalkerAuthor Commented:
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

LPCTSTR str;
str = MAKEINTRESOURCE(IDR_TGAPIC);
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 ..

0
 
chensuCommented:
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.
0
 
SkizoWalkerAuthor Commented:
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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now