Solved

Writing in a ressource without having Exception handled ?

Posted on 1998-08-09
5
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

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

Accepted Solution

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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