hello everybody
I  made a call to GetDIBits to get the BITs of a BMP file(a CBitmap instance) in memory. So , i need to allocate a block of memory when calling the function.
My problem is that if the memory is allocated by using GlobalAlloc() , the GetDIBits will success. if i use LocalAlloc() ,the function will fail and the GetLastError() return 0.
I could not see what the difference between GlobalAlloc() and LocalAlloc()  in MSDN. It's said that both the two will allocate memory in the heap.
Does GetDIBits do something else inside?
Sorry for my English. I am waiting for your replay.
Best regards!!
Posted on 2005-09-04 21:58:32 by Rey
AFAIK the only real difference between the two is that LocalAlloc is not guaranteed to align to an 8 Byte boundary, at least according to the docs. But tests show that it does anyway, at least a few hundred allocs/deallocs were all aligned to 8 bytes. At any rate, there are no longer separate local and global heaps as Win32 is a protected mode OS using virtual memory, so why not just use GlobalAlloc if the local version is giving problems ?

Next, are you sure that you must use GetDIBIts to get the results that you need ? Using GetObject can give you a pointer to the existing memory buffer where the bits are stored, allowing you to manipulate it directly. GetDIBits is great for converting V3 to DIB bitmaps or adjusting the color table of an image but for simple direct image manipulation GetObject works fine.

DIBstruct DIBSECTION <?>
invoke GetObject, , SIZEOF DIBSECTION, offset DIBstruct

or if it is an older V3 bitmap (compatible bmp)

BMPStruct BITMAP <?>
invoke GetObject, , SIZEOF BITMAP, offset BMPStruct

The bits are returned in the bmBits or dsOffset member of the structure.
Posted on 2005-09-04 22:39:02 by donkey
Thank you for your reply.
I can not post the code here according to  my company's rule . I will discuss with you about this
problem later.
Thank you
Posted on 2005-09-07 03:17:47 by Rey
Rey, your company will probably allow you to show just the line of code doing the Local/GlobalAlloc allocation :)

Which flags are you passing to it? Why are you using Local/GlobalAlloc instead of HeapAlloc? Have you filled the BITMAPINFO header properly? etc.
Posted on 2005-09-07 07:20:35 by f0dder
Maybe , i will disscuss your suggestions with my BOSS :lol:

Here's the code segment i used to allocate memory:

//Before calling the function , i have filled the stBitmapInfoHeader structor with proper value.
LPSTR lpBits = (LPSTR)::GlobalAlloc( GPTR , stBitmapInfoHeader.biSizeImage );
nRet = ::GetDIBits( dcScreen.GetSafeHdc() , (HBITMAP)&bitmap.GetSafeHandle() , 0
                          stBitmap.bmHeight , lpBits , (PBITMAPINFO)&stBitmapInfoHeader,
                          DIB_RGB_COLORS);
//GlobalAlloc worked fine and GetDIBits() return the data i need, but if i call LocalAlloc like code below
LPSTR lpBits = (LPSTR)::LocalAlloc(  LPTR , stBitmapInfoHeader.biSizeImage ) ;
nRet = ::GetDIBits( dcScreen.GetSafeHdc() , (HBITMAP)&bitmap.GetSafeHandle() , 0
                          stBitmap.bmHeight , lpBits , (PBITMAPINFO)&stBitmapInfoHeader,
                          DIB_RGB_COLORS);
GetDIBits will failed.

As to why i dont use HeapAlloc , because i find an example in MSDM that use GlobalAlloc().
By the way , the GetLastError()  returns 0 when callingl the GetDIBits with the memory allocated by
LocalAlloc() . but 0 means ERROR_SUCCESS that means no error occured!!  I dont know why too.
Any new idea?



Posted on 2005-09-08 21:31:16 by Rey