Solved

Writing in a ressource without having Exception handled ?

Posted on 1998-08-09
5
220 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 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

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 http://bit.ly/XDcourse.

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