Page 1 of 1

Air fix/shot in the dark for copydata()

Posted: 12 Aug 2015 11:56
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()

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

Posted: 12 Aug 2015 16:35
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>

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

Posted: 12 Aug 2015 16:44
by Marco
What version of Python are you using? I'm using 3.4 myself.

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

Posted: 12 Aug 2015 16:55
by bdeshi
~same here. 3.4.3
:cry:

sending and receiving works, but the incoming data from my XY is apparently MORE disturbing! :?