Air fix/shot in the dark for copydata()

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Air fix/shot in the dark for copydata()

Post by Marco »

I have a weird problem with copydata - sending strings sometimes add weird characters at the end.
It doesn't happen between two XY, but only from a XY to a Python interactive console. I can't repro, with the same strings, even between a custom Python sender script and a custom receiving one.
So I'm a bit :?
I can't find any visible flaw in the python code, even if I'm not an expert. Also, you Don are faster at solving bugs than the Python community, so if it's an XY bug I've better chances :)

ADD: this is the python code, put the right hwnd (the hwnd of the XY that will communicate with the script) at line 68. When it starts, it will immediately send its own hwnd to that XY window, so that you can address the messages to the right window.

Code: Select all

import ctypes
import ctypes.wintypes
import win32api
import win32con
import win32gui

class COPYDATASTRUCT_Send(ctypes.Structure):
    _fields_ = [
        ("dwData", ctypes.wintypes.LPARAM),
        ("cbData", ctypes.wintypes.DWORD),
        ("lpData", ctypes.c_char_p) 
    ]

class COPYDATASTRUCT_Receive(ctypes.Structure):
    _fields_ = [
        ("dwData", ctypes.wintypes.LPARAM),
        ("cbData", ctypes.wintypes.DWORD),
        ("lpData", ctypes.c_wchar_p)
    ]
    
def Send(hwnd, content):
    content = bytes(str(content), "utf-16le")
    cds = COPYDATASTRUCT_Send()
    cds.dwData = 0x400000 #as per Help.pdf, page 436
    cds.cbData = ctypes.sizeof(ctypes.create_string_buffer(content))
    cds.lpData = ctypes.c_char_p(content)
    ctypes.windll.user32.SendMessageW(hwnd, win32con.WM_COPYDATA, 0, ctypes.byref(cds))

class Listener:

    def __init__(self):
        message_map = {
            win32con.WM_COPYDATA: self.OnCopyData
        }
        wc = win32gui.WNDCLASS()
        wc.lpfnWndProc = message_map
        wc.lpszClassName = "pYreX"
        hinst = wc.hInstance = win32api.GetModuleHandle(None)
        classAtom = win32gui.RegisterClass(wc)
        self.hwnd = win32gui.CreateWindow (
            classAtom,
            "pYreX",
            0,
            0, 
            0,
            win32con.CW_USEDEFAULT, 
            win32con.CW_USEDEFAULT,
            0, 
            0,
            hinst, 
            None
        )

    def OnCopyData(self, hwnd, msg, wparam, lparam):
        PCOPYDATASTRUCT_Receive = ctypes.POINTER(COPYDATASTRUCT_Receive)
        pCDS = ctypes.cast(lparam, PCOPYDATASTRUCT_Receive)
        print(ctypes.wstring_at(pCDS.contents.lpData))

l = Listener()

xy_hwnd = 1442884 #sys.argv[1]

Send(xy_hwnd, l.hwnd)

win32gui.PumpMessages()
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: Air fix/shot in the dark for copydata()

Post by bdeshi »

I'd like to try but

Code: Select all

Python WNDPROC handler failed
Traceback (most recent call last):
  File "E:\downloads\tmp\pyrex_xy_copydata.py", line 57, in OnCopyData
    print(ctypes.wstring_at(pCDS.contents.lpData))
  File "C:\programs\python3\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 4-5: character maps to <undefined>
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Air fix/shot in the dark for copydata()

Post by Marco »

What version of Python are you using? I'm using 3.4 myself.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: Air fix/shot in the dark for copydata()

Post by bdeshi »

~same here. 3.4.3
:cry:

sending and receiving works, but the incoming data from my XY is apparently MORE disturbing! :?
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Post Reply