• win32clipboard writing to clipboard on Windows 11

    From Rob Cliffe@21:1/5 to All on Mon Jun 17 20:27:51 2024
    Recently I acquired a new laptop running WIndows 11; my previous one
    uses WIndows 10.  I encountered a strange problem:
    I am using the win32clipboard backage (part of pywin32), and when I use SetClipboardData() to write text which consists ***entirely of digits***
    to the clipboard, I either get an error (not always the same error
    message) or a program crash.  The problem does not appear if I use SetClipboardText() instead.  The problem does not occur on my old
    machine (where I used the feature extensively).

    Sample program:

    from win32clipboard import *
    OpenClipboard()
    SetClipboardData(CF_UNICODETEXT, "A")
    SetClipboardData(CF_UNICODETEXT, "A0")
    SetClipboardData(CF_UNICODETEXT, "0A")
    SetClipboardText("0", CF_UNICODETEXT)
    print("OK so far")
    SetClipboardData(CF_UNICODETEXT, "0")
    CloseClipboard()

    Sample output:

    OK so far
    Traceback (most recent call last):
      File "C:\TEST*.PY", line 8, in <module>
        SetClipboardData(CF_UNICODETEXT, "0")
    pywintypes.error: (0, 'SetClipboardData', 'No error message is available')

    Can anyone shed light on this?
    Best wishes
    Rob Cliffe

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Rob Cliffe via Python-list on Tue Jun 18 02:30:46 2024
    On 2024-06-17 20:27, Rob Cliffe via Python-list wrote:
    Recently I acquired a new laptop running WIndows 11; my previous one
    uses WIndows 10.  I encountered a strange problem:
    I am using the win32clipboard backage (part of pywin32), and when I use SetClipboardData() to write text which consists ***entirely of digits***
    to the clipboard, I either get an error (not always the same error
    message) or a program crash.  The problem does not appear if I use SetClipboardText() instead.  The problem does not occur on my old
    machine (where I used the feature extensively).

    Sample program:

    from win32clipboard import *
    OpenClipboard()
    SetClipboardData(CF_UNICODETEXT, "A")
    SetClipboardData(CF_UNICODETEXT, "A0")
    SetClipboardData(CF_UNICODETEXT, "0A")
    SetClipboardText("0", CF_UNICODETEXT)
    print("OK so far")
    SetClipboardData(CF_UNICODETEXT, "0")
    CloseClipboard()

    Sample output:

    OK so far
    Traceback (most recent call last):
      File "C:\TEST*.PY", line 8, in <module>
        SetClipboardData(CF_UNICODETEXT, "0")
    pywintypes.error: (0, 'SetClipboardData', 'No error message is available')

    Can anyone shed light on this?
    Best wishes
    Rob Cliffe

    I tried it on Windows 10 and got this:

    from win32clipboard import *
    OpenClipboard()
    SetClipboardData(CF_UNICODETEXT, "A")
    1830508101640
    CloseClipboard()
    OpenClipboard()
    SetClipboardData(CF_UNICODETEXT, "0")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    pywintypes.error: (6, 'SetClipboardData', 'The handle is invalid.')
    CloseClipboard()

    It looks like it's something to memory ownership:

    https://stackoverflow.com/questions/1264137/how-to-copy-string-to-clipboard-in-c

    If you're putting text on the clipboard, why not just use
    SetClipboardText()? That's what I do.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to MRAB via Python-list on Mon Jun 17 22:23:07 2024
    On 6/17/2024 9:30 PM, MRAB via Python-list wrote:
    On 2024-06-17 20:27, Rob Cliffe via Python-list wrote:
    Recently I acquired a new laptop running WIndows 11; my previous one
    uses WIndows 10.  I encountered a strange problem:
    I am using the win32clipboard backage (part of pywin32), and when I use
    SetClipboardData() to write text which consists ***entirely of digits***
    to the clipboard, I either get an error (not always the same error
    message) or a program crash.  The problem does not appear if I use
    SetClipboardText() instead.  The problem does not occur on my old
    machine (where I used the feature extensively).

    Sample program:

    from win32clipboard import *
    OpenClipboard()
    SetClipboardData(CF_UNICODETEXT, "A")
    SetClipboardData(CF_UNICODETEXT, "A0")
    SetClipboardData(CF_UNICODETEXT, "0A")
    SetClipboardText("0", CF_UNICODETEXT)
    print("OK so far")
    SetClipboardData(CF_UNICODETEXT, "0")
    CloseClipboard()

    Sample output:

    OK so far
    Traceback (most recent call last):
        File "C:\TEST*.PY", line 8, in <module>
          SetClipboardData(CF_UNICODETEXT, "0")
    pywintypes.error: (0, 'SetClipboardData', 'No error message is
    available')

    Can anyone shed light on this?
    Best wishes
    Rob Cliffe

    I tried it on Windows 10 and got this:

    from win32clipboard import *
    OpenClipboard()
    SetClipboardData(CF_UNICODETEXT, "A")
    1830508101640
    CloseClipboard()
    OpenClipboard()
    SetClipboardData(CF_UNICODETEXT, "0")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    pywintypes.error: (6, 'SetClipboardData', 'The handle is invalid.')
    CloseClipboard()

    It looks like it's something to memory ownership:

    https://stackoverflow.com/questions/1264137/how-to-copy-string-to-clipboard-in-c

    If you're putting text on the clipboard, why not just use
    SetClipboardText()? That's what I do.

    If you can make a change, and you only need to work with text on the
    clipboard, you could change to use pyperclip. It also works on Linux,
    if you might care about that in the future. It's available as a pip
    install. It's easier to use than the win32 approach.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)