[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


how to Get Rid GetObject Error !!

Posted on 2000-03-07
Medium Priority
Last Modified: 2010-04-10
hi !!

I am using the below function described
BOOL CDib::CreateDibIndirect( int width, int height, int planes, int bytesPerPixel , CDC& dc, RGBQUAD* colors, int numCols, void** frameData )
      CDC memdc;
      char* bmi = new char[sizeof(BITMAPINFOHEADER) + numCols*sizeof(RGBQUAD)];
      RGBQUAD* newcolors = (RGBQUAD*)(&bmih+1);
      bmih.biSize = sizeof( BITMAPINFOHEADER );
      bmih.biWidth = width;
      bmih.biHeight = height;
      bmih.biPlanes = planes;
      bmih.biBitCount = bytesPerPixel * 8;
      bmih.biCompression = BI_RGB;
      bmih.biSizeImage = 0;
      bmih.biXPelsPerMeter = 4000;
      bmih.biYPelsPerMeter = 4000;
      bmih.biClrUsed = numCols;
      bmih.biClrImportant = numCols;
      memcpy(newcolors, colors, numCols*sizeof(RGBQUAD));
      // Create a DIB section for our image
      HBITMAP ds = ::CreateDIBSection( memdc, (BITMAPINFO*)bmi, DIB_RGB_COLORS, frameData, NULL, NULL );
      delete[] bmi;
      return Attach( ds );

In which i am using one parameter of CreateDIBSection hOffset as NULL , due to which i get error when i latter use in as it gives (GetObject) NULL value , How should i use the value by using CreateFileMapping function to get rid of the error ??

Please answer !!
Thanks in advance
Question by:Govil
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
  • 3
LVL 10

Expert Comment

ID: 2594102
What does "to which i get error when i latter use in as it gives (GetObject) NULL value" mean?

What does this have to do with CreateFileMapping?

Perhaps if you show us the code in which you call the CDib::CreateDibIndirect that may help us understand.

Author Comment

ID: 2595012
thanks to look at my problem !! yeah I am sending u the code where i get the error !! When i call my save fuction from CView class it gives error at

      CFileDialog *pSave;
      char szFilter[]="BMP Files(*.BMP)|*.BMP||";
      pSave = new CFileDialog(FALSE,"BMP","",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter,this);

      if(pSave->DoModal () == IDOK)
                        CFile file;
                  CFileException fe;
            if(!file.Open(pSave->GetPathName(),CFile::modeCreate | CFile::modeWrite,&fe))
                  #ifdef _DEBUG
                        afxDump << "File could not be opened " << fe.m_cause << "\n";
            LPBYTE lpDib;
                  AfxGetMainWnd()->SetWindowText (pSave->GetPathName () );
                  //seq.Release ();


my save function is in CDib class code ia given below ,Sorry for lot of comments as just for testing !!

DWORD CDib::Save(CFile& file,HANDLE hDib,LPBYTE lpDib) //,HDC* hdc)//CDC* dc)
      //handle of the CGdiObject
      if (m_hObject != NULL)
            //AfxMessageBox("First selct the frame");
            return FALSE;
      //CString str;
      //CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
      BOOL bSave = TRUE;
//      HDIB hDib;
      LPBITMAPINFOHEADER lpBI;   // Pointer to DIB info structure
      DWORD dwDIBSize;

      DIBSECTION ds;
      //Attach( ds );
       VERIFY(GetObject(sizeof(ds), &ds)==sizeof(ds));
      char* buf = new char[sizeof(BITMAPINFOHEADER) + MAXPALCOLORS*sizeof(RGBQUAD)];
      RGBQUAD* bmicolors = (RGBQUAD*)(&bmih+1);
      memcpy(&bmih, &ds.dsBmih, sizeof(bmih));

      //Get a pointer to the DIB memory,first of which contains the Bitmapinfo structure
      //lpBI = (LPBITMAPINFOHEADER)GlobalLock(hDib);
      //if (!m_hObject)
      //      return FALSE;

      if(hDib == NULL)
            return 0;

      if(&bmih == NULL)
            return 0;

//      if (lpBI == NULL)
//            return 0;
      if(bmicolors == NULL)
            return 0;
      //fill the fields of file header
      // fill the file type( first two will be "BM")

            hdr.bfType = BFT_BITMAP;//0x4d42;//DIB_HEADER_MARKER;

      //Calculating the size of DIB

      // First find the size of header plus size of color table.
      //the size of the structure
      //Now Calculate the size of the Palette
      //dwDIBSize = *(LPDWORD)&lpBI + PaletteSize();

      if((bmih.biCompression == BI_RLE8) || (bmih.biCompression  == BI_RLE4))

            dwDIBSize +=bmih.biSizeImage;
            DWORD dwBmBitsSize;                  //Size of the Bitmap Bits only

            //It's not RLE,so size is width (DWORD aligned) * Height

            dwBmBitsSize = WIDTHBYTES((bmih.biWidth) * ((DWORD)bmih.biBitCount)) * bmih.biHeight;
            dwDIBSize +=dwBmBitsSize;
            bmih.biSizeImage  = dwBmBitsSize;


      //Calculating the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)
      hdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
      hdr.bfReserved1 = 0;
      hdr.bfReserved2 = 0;

       //now calculate the offset
      hdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + bmih.biSize  + PaletteSize();

      //Write the file header

      file.Write ((LPSTR)&hdr,sizeof(BITMAPFILEHEADER));
      DWORD dwBytesSaved = sizeof(BITMAPFILEHEADER);

      // Write the DIB header
      UINT nCount = sizeof(BITMAPINFO) +(GetColorTable(bmicolors,MAXPALCOLORS)-1)*sizeof(RGBQUAD);
      dwBytesSaved += nCount;
      file.Write (&bmih,nCount);
      // Write the DIB bits

      DWORD dwBytes = bmih.biBitCount * bmih.biWidth ;      //Width();
      // Calculate the number of bytes per line

      if (dwBytes%32 == 0)
            dwBytes /= 8;
      dwBytes = dwBytes/8 + (32-dwBytes%32)/8 + (((32-dwBytes%32)%8 > 0) ? 1 : 0);
      nCount = dwBytes * bmih.biHeight;      
      dwBytesSaved += nCount;
      file.WriteHuge (m_bm.bmBits ,nCount);
      return dwBytesSaved;


so i get degub error at line
VERIFY(GetObject(sizeof(ds), &ds)==sizeof(ds));

If u require any other information on code do let me know ASAP !!

By CreatFileMapping(...) i mean when i read the about CreateDIBSection in (msdn)help !!there it is mentioned like that !!

hope u will get me out of this error soon !! thanks in advance
Piyush Govil
LVL 10

Expert Comment

ID: 2598229
Try using (or looking at) the CSimpleDIB from the MSDN article "OpenGL VI: Rendering on DIBs with PFD_DRAW_TO_BITMAP".

Here is the class


#ifndef __CSimpleDIB_H__
#define __CSimpleDIB_H__

// CSimpleDIB -
// Implements a simple encapsulation of a DIB section and a DC.
class CSimpleDIB
            // Construction
            CSimpleDIB() ;
            virtual ~CSimpleDIB() ;

      // Create a CSimpleDIB object. Create can be called multiple times.
            void Create(int cx, int cy, int ibitcount ) ;

      // Draw the DIB onto the destination DC.
            void Draw(CDC* pdcDest, int x, int y) ;

      // Set the palette used in the DIB.
            void SetPalette(CPalette* pPal) ;

            CDC* GetDC() {return m_pdc;}
            HBITMAP GetHandle() {return m_hbmp;}

            HBITMAP m_hbmp ;
            HBITMAP m_hbmOld ;
            void* m_pBits ;            // Pointer to the bits.
            CSize m_sizeDIB ;
            CDC* m_pdc ;
            void destroy() ;       // Destruction helper.


#include "stdafx.h"
#include "CSimpleDIB.h"

// Construction
      : m_sizeDIB(0,0)
      m_hbmp = NULL ;
      m_pBits = NULL ;
      m_pdc = NULL ;

// Destruction.
      destroy() ;

//      Destruction helper.
void CSimpleDIB::destroy()
    // Select the old bitmap back into the buffer DC.
    if (m_hbmOld)
        ::SelectObject(m_pdc->GetSafeHdc(), m_hbmOld);

      // Delete bitmap.
      if (m_hbmp) DeleteObject(m_hbmp);

      // Delete DC.
      if (m_pdc) delete m_pdc ;

// Create a DIB section cx X cy X ibitcount.
void CSimpleDIB::Create(int cx, int cy, int ibitcount)
   ASSERT((ibitcount == 8) || (ibitcount == 16) || (ibitcount = 24))      ;
   ASSERT(cx > 0);
   ASSERT(cy > 0);

   // Destroy parts of objects if we are recreating it.
   if ((m_pdc != NULL) || (m_hbmp != NULL)) destroy() ;

   // Save size for drawing later.
   m_sizeDIB.cx = cx ;
   m_sizeDIB.cy = cy ;

   // Create a BITMAPINFOHEADER structure to describe the DIB
   int iSize = sizeof(BITMAPINFOHEADER) ;
   memset(&BIH, 0, iSize);

   // Fill in the header info.
   BIH.biSize = iSize;
   BIH.biWidth = cx;
   BIH.biHeight = cy;
   BIH.biPlanes = 1;
   BIH.biBitCount = ibitcount;
   BIH.biCompression = BI_RGB;

   // Create a new DC.
   m_pdc = new CDC ;

   // Create the DIB section.
   m_hbmp = CreateDIBSection( m_pdc->GetSafeHdc(),


    // Select the new bitmap into the buffer DC
    if (m_hbmp)
        m_hbmOld = (HBITMAP)::SelectObject(m_pdc->GetSafeHdc(),

// BitBlt DIB section to the screen. Does not mess with the palette.
void CSimpleDIB::Draw(CDC* pdcDest, int x, int y)
    // Note: you do not need to select the palette into
    // the memory DC because the DIB section is using palette
    // index values not colors
    pdcDest->BitBlt( x, y,
                     m_sizeDIB.cx, m_sizeDIB.cy,
                     0, 0,


// Set the color table in the DIB section.
void CSimpleDIB::SetPalette(CPalette* pPal)

    // get the colors from the palette
    int iColors = 0;
    pPal->GetObject(sizeof(iColors), &iColors);
    ASSERT(iColors > 0);
    pPal->GetPaletteEntries(0, iColors, pPE);

    // Build a table of RGBQUADS
    RGBQUAD* pRGB = new RGBQUAD[iColors];
    for (int i = 0; i < iColors; i++) {
        pRGB[i].rgbRed = pPE[i].peRed;
        pRGB[i].rgbGreen = pPE[i].peGreen;
        pRGB[i].rgbBlue = pPE[i].peBlue;
        pRGB[i].rgbReserved = 0;
                       0, iColors,

    delete [] pRGB;
    delete [] pPE;

LVL 10

Accepted Solution

RONSLOW earned 150 total points
ID: 2775681
i have code for saving a DIB to file that does work.

Your code seems to be doing strange things.

For example, you are doing a GetObject on a DIBSECTION that hasn't been created yet .. that's why it asserts

Here is the code I use to write out a 24-bit dib...

bool Q24bitDIB::WriteToFile(LPCTSTR filename) const {
    bool ok = false;
    TRY {
        CFile file(filename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
        ok = WriteToFile(file);
    } CATCH_ALL(e) {
    return ok;

bool Q24bitDIB::WriteToFile(CFile& file) const {
    QReentrancyReader lock(&m_cs,"Q24bitDIB::WriteToFile");

    if (! m_pBits) return false;

    // setup file header
    hdr.bfType = 0x4d42;    // 0x42 = "B", 0x4d = "M"
    hdr.bfReserved1 = 0;
    hdr.bfReserved2 = 0;

    BITMAPINFOHEADER bitmapinfoheader;

    // Compute the offset to the array of color indices.
    hdr.bfOffBits = (DWORD) (
        sizeof(BITMAPFILEHEADER) +
        bitmapinfoheader.biSize +
        bitmapinfoheader.biClrUsed * sizeof (RGBQUAD)

    // Compute the size of the entire file.
    hdr.bfSize = hdr.bfOffBits + bitmapinfoheader.biSizeImage;

    bool ok = false;
    TRY {
        // Copy the BITMAPFILEHEADER into the .BMP file.
        // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
        file.WriteHuge(&bitmapinfoheader,sizeof(bitmapinfoheader) + bitmapinfoheader.biClrUsed * sizeof (RGBQUAD));
        // Copy the array of color indices into the .BMP file.

        ok = true;
    } CATCH_ALL (e) {
    return ok;

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

649 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