[comp.windows.ms.programmer] Problem Reading Bitmaps from Clipboard

garyp@csg.uwaterloo.ca (Gary Pianosi) (05/28/91)

I am trying to write a program that will read a bitmap from the
clipboard and save it out in another format.  However, the handle
returned from GetClipboardData() is sometimes invalid in the sense
that a GlobalLock() of that handle fails.  Also, the handle returned
is often not listed by Heap-Walker, or is labelled as being a handle
to an existing resource (menu, string, etc).

Following the example in the "Guide to Programming" (section 13.1.3),
I modified the CLIPTEXT sample to paste bitmaps instead of text from
the clipboard.  The pertinent code is as follows:

> case IDM_PASTE:
>   if (OpenClipboard(hWnd)) {
>       if (!(hClipData = GetClipboardData(CF_BITMAP))) {
>           CloseClipboard();
>           break;
>       }
>       if (!(lpClipData = GlobalLock(hClipData))) {
>           OutOfMemory();              /* <--- usually fails here */
>           CloseClipboard();
>           break;
>       }
>       /*
>        * ... do something with data at lpClipData ...
>        */
>       GlobalUnlock(hClipData);
>       CloseClipboard();
>   }

Sometimes the GlobalLock() works but usually it fails.  With a given
content in the clipboard it either always succeeds or always fails.

Miscellaneous info:
    - I'm using a 386 clone with 8 Meg of memory (also occurs using
        a PS/2 Model 80 with 2 Meg of memory).
    - I have tried both Microsoft C 6.00 and Borland Turbo C 2.0
    - occurs in 386-enhanced, Standard, and Real modes
    - occurs with both Windows 3.0 and Windows 3.00a

In an attempt to solve my problem by side-stepping it, I tried using
the GetBitmapBits() function to copy the bitmap data to a buffer.
It seems to work (even when the bitmap handle appears to be invalid!).
I guess I'm a little worried about leaving my problem unsolved since
the SDK suggests using GlobalLock() to look at the bitmap data.

Thanks in advance for any advice.

- Gary Pianosi.
-- 
--
E-mail:  garyp@csg.UWaterloo.CA                     Phone: (519) 888-4678
Computer Systems Group, University of Waterloo, Waterloo, Ontario, Canada

Gary.Pianosi@sunbrk.FidoNet.Org (Gary Pianosi) (05/28/91)

I am trying to write a program that will read a bitmap from the
clipboard and save it out in another format.  However, the handle
returned from GetClipboardData() is sometimes invalid in the sense
that a GlobalLock() of that handle fails.  Also, the handle returned
is often not listed by Heap-Walker, or is labelled as being a handle
to an existing resource (menu, string, etc).

Following the example in the "Guide to Programming" (section 13.1.3),
I modified the CLIPTEXT sample to paste bitmaps instead of text from
the clipboard.  The pertinent code is as follows:

> case IDM_PASTE:
>   if (OpenClipboard(hWnd)) {
>       if (!(hClipData = GetClipboardData(CF_BITMAP))) {
>           CloseClipboard();
>           break;
>       }
>       if (!(lpClipData = GlobalLock(hClipData))) {
>           OutOfMemory();              /* <--- usually fails here */
>           CloseClipboard();
>           break;
>       }
>       /*
>        * ... do something with data at lpClipData ...
>        */
>       GlobalUnlock(hClipData);
>       CloseClipboard();
>   }

Sometimes the GlobalLock() works but usually it fails.  With a given
content in the clipboard it either always succeeds or always fails.

Miscellaneous info:
    - I'm using a 386 clone with 8 Meg of memory (also occurs using
        a PS/2 Model 80 with 2 Meg of memory).
    - I have tried both Microsoft C 6.00 and Borland Turbo C 2.0
    - occurs in 386-enhanced, Standard, and Real modes
    - occurs with both Windows 3.0 and Windows 3.00a

In an attempt to solve my problem by side-stepping it, I tried using
the GetBitmapBits() function to copy the bitmap data to a buffer.
It seems to work (even when the bitmap handle appears to be invalid!).
I guess I'm a little worried about leaving my problem unsolved since
the SDK suggests using GlobalLock() to look at the bitmap data.

Thanks in advance for any advice.

- Gary Pianosi.
-- 
--
E-mail:  garyp@csg.UWaterloo.CA                     Phone: (519) 888-4678
Computer Systems Group, University of Waterloo, Waterloo, Ontario, Canada

 * Origin: Seaeast - Fidonet<->Usenet Gateway - sunbrk (1:343/15.0)

Gary.Pianosi@sunbrk.FidoNet.Org (Gary Pianosi) (05/28/91)

I wrote:
>In an attempt to solve my problem by side-stepping it, I tried using
>the GetBitmapBits() function to copy the bitmap data to a buffer.
>It seems to work (even when the bitmap handle appears to be invalid!).

I take this back!!!  The data returned in my buffer seems to be garbage
(strings, random characters, etc.) and not bitmap data when the handle
is invalid.  GetBitmapBits() does however return that the correct number
of bytes was read (!?!).

- Gary.
-- 
--
E-mail:  garyp@csg.UWaterloo.CA                     Phone: (519) 888-4678
Computer Systems Group, University of Waterloo, Waterloo, Ontario, Canada

 * Origin: Seaeast - Fidonet<->Usenet Gateway - sunbrk (1:343/15.0)