getting win32clipboard data

Andrew Dalke dalke at acm.org
Sat Apr 22 02:54:28 EDT 2000


Hello,

  I've been trying to figure out how to get data from the clipboard
using win32clipboard.  I'm having a problem turning the integer
handle returned from GetClipboardData into something I can extract
data from.

  I've done a workaround using Sam Rushing's calldll and DynWin.windll,
based on hints from Wesley Phoa's "clipboard.py" at
http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=486991372&fmt=text

 = = = =
kernel32 = windll.module('Kernel32')

def get_data_by_format_id(id):
    win32clipboard.OpenClipboard(0)
    h = win32clipboard.GetClipboardData(id)
    if not h:
        win32clipboard.CloseClipboard()
        return ""

    kernel32.GlobalLock(h)
    try:
        size = kernel32.GlobalSize(h)
        text = []
        for j in range(size):
            t = calldll.read_byte(h+j)
            # read_byte returns a signed character
            if t < 0:
                t = t + 256
            text.append(t)
    finally:
        kernel32.GlobalUnlock(h)
        win32clipboard.CloseClipboard()

    # convert to the actual byte string
    text = string.join(map(chr, text), "")
    return text
 = = = =
(Note: this isn't the original function - I moved in the Open/Close
Clipboard calls from the calling function, but haven't tested it.)

I would like to replace the kernel32.{GlobalLock, GlobalUnlock,
GlobalSize} and the calldll.read_byte calls with something part of
the standard win32 distribution.  I figure that if win32clipboard
is a standard module, there has to be a way to use it.

Also, I'm not reading text data, which explains the read_byte
loop as compared to Wesley Phoa's use of read_string.  It seems
to work.  It would be nice to have a "read_data" :)

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list