[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Writing in a ressource without having Exception handled ?

Posted on 1998-08-09
5
Medium Priority
?
245 Views
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 !
0
Comment
Question by:SkizoWalker
5 Comments
 

Expert Comment

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

Accepted Solution

by:
jmelia earned 300 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.  
 

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
 

Author Comment

by:SkizoWalker
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

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
 
LVL 23

Expert Comment

by:chensu
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.
0
 

Author Comment

by:SkizoWalker
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

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Loops Section Overview
Suggested Courses

831 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