Get an HGLOBAL for a DIB.


My application loads a picture from file and converts it to a DDB and then to a DIB; however I am in need of using globalsize and globallock functions on the DIB to get the size and such. How can I get an HGLOBAL to a loaded bitmap(DDB) or DIB?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Depending on the way the data was read in and converted, your HBITMAP to the DIB is quite possibly an HGLOBAL already.  Have you tried using GlobalSize on the handle?

Also:  If you did the conversion, I'd think that you would already know the size, etc....
rushthewebAuthor Commented:
Here is a snippet of my code that is loading the image to bitmap and then getting the dib section for it.... I dont actually know if this is the correct way of doing it or not. When I do a GlobalSize on the DIB handle I always get 0; is there another way of getting the DIB and the size?

MultiByteToWideChar(CP_ACP, 0, szPath, -1, wpath, MAX_PATH);
IPicture *pic;
OleLoadPicturePath(wpath, NULL, NULL, NULL, IID_IPicture, (LPVOID *)&pic);
HBITMAP hPic      = NULL;
HBITMAP h            = NULL;
DIBSECTION            info;

pic->get_Handle((UINT *)&hPic);
GetObject(h, sizeof(DIBSECTION), &info);
void *pBits                  = info.dsBm.bmBits;

nSize = GlobalSize(hData);
pPtr = GlobalLock(hData);

if ( nSize != NULL )
      SetData(CF_DIB, pPtr, (UINT)nSize);

I'm not up to speed on  OleLoadPicturePath...
Verify that the OleLoadPicturePath call succeeded.  Also try:
     short nType;
     HRESULT hr= pic->get_Type( &nType );
as a sanity check to see if you have a real HBITMAP
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

rushthewebAuthor Commented:
I've verified that the picture is being loaded successfully by displaying the bitmap on a dialog. To double check I have done as you suggested and came up with the result: S_OK.

I just wish I knew why GlobalSize always returns 0; I imagine its something kinda simple that im overlooking, however I havent done C++ is 4 years and even then I didnt use MFC very often so its a given that im going to mess something up. :)
>>I just wish I knew why GlobalSize always returns 0;

How you are processing hData before ? is this handle returned by either the GlobalAlloc or GlobalReAlloc ??

rushthewebAuthor Commented:
Sorry that was a typo when I was editing the code, it really is:

nSize = GlobalSize(h);
pPtr = GlobalLock(h);

My mistake, I've also tried using pBits in GlobalSize with no luck, both cause GlobalLock to return nothing.
mahesh1402Commented: <===As per this documentation handle for GlobalSize() is returned by either the GlobalAlloc or GlobalReAlloc function....

rushthewebAuthor Commented:
Hmm, I wish I read that entire page through... lol.

However Im still back to the where I started in a sense because I dont know how to get the total size I need to allocate. Did I mention I havent coded in C++ in forever. ;p I appreciate the help.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Out of curiosity -- and to improve the value of this question in the PAQ -- what specifically did you learn from those links and how did you obtain the size information that you needed?  Thanks!
I think he was passing HBITMAP instead of HANDLE(hDIB) to GlobalSize()......also GLobalSize() needs handle returned by GlobalAlloc()/GlobalReAlloc().......So I suggested to refer above links...and to refer BitmapToDIB()function....


Probably true.  Nevertheless, it would be best to hear exactly what helped solve the problem, directly from rushtheweb.
rushthewebAuthor Commented:
Im currently very busy at work so I apoligize for the late reply. What mahesh said was right, I was simply confused and in the many examples he provided I was able to clearly see that I wasnt using the correct handle and on top of that the BitmapToDib function turned out to be fantastic!

Thanks for everyone for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.