[Win32utils-devel] win32-clipboard issues and support for additional formats

Daniel Berger djberg96 at gmail.com
Fri Apr 17 02:05:51 EDT 2009


Hi,

Currently the GetClipboardData() function is declared in windows-pr so that
it returns a pointer. I guess my reasoning was that I was only worried about
dealing with text at the time, so it was the easiest thing to do. But, I'd
like to support more formats beyond text, i.e. images.

However, this can cause a segfault. If you copy a jpeg image to the
clipboard first, for example, and try to run the following snippet you'll
see what I mean.

Win32::Clipboard.data(Clipboard::CF_ENHMETAFILE)

I think the first issue is that we need to redefine GetClipboardData() to
return a long (HANDLE). But that means manually converting the handle back
to a string using memcpy() when it comes to dealing with text. But, since we
don't know in advance how much text is on the clipboard, we'll need to
dynamically allocate a string buffer. How do you know if you've allocated
enough? BTW, I don't think we can use memcpy_s() in practice, since it's not
supported by VC++ 6.

Then we need a generic way to handle text or images:

   # something like this
   def self.data(format = TEXT)
      begin
         self.open
         if IsClipboardFormatAvailable(format)
            clipdata = GetClipboardData(format)
            case format
               when text_format
                  clipdata = get_text_data(clipdata)
               when image_format
                  clipdata = get_image_data(clipdata)
               when other_format
                  # ???
               end
         else
            clipdata = ''
         end
      ensure
         self.close
      end

      clipdata
   end

Any suggestions?

Regards,

Dan



More information about the win32utils-devel mailing list