Question

I'm working on a python script to interact with a program using wm_copydata, but am having trouble with reading the return data. I've turned a few examples into the following:

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

FindWindow = ctypes.windll.user32.FindWindowW
SendMessage = ctypes.windll.user32.SendMessageW

class COPYDATASTRUCT(ctypes.Structure):
    _fields_ = [
        ('dwData', ctypes.wintypes.LPARAM),
        ('cbData', ctypes.wintypes.DWORD),
        ('lpData', ctypes.c_char_p)
        #formally lpData is c_void_p, but we do it this way for convenience
]

PCOPYDATASTRUCT = ctypes.POINTER(COPYDATASTRUCT)

# hwnd = FindWindow(None, "ZhornSoftwareStickiesMain")
# cds = COPYDATASTRUCT()
# cds.dwData = 0
# str = b'api do register'
# cds.cbData = ctypes.sizeof(ctypes.create_string_buffer(str))
# cds.lpData = ctypes.c_char_p(str)

class Listener:

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

    def register(self):
        hwnd = FindWindow(None, "ZhornSoftwareStickiesMain")
        cds = COPYDATASTRUCT()
        cds.dwData = 0
        str = b'api do register'
        cds.cbData = ctypes.sizeof(ctypes.create_string_buffer(str))
        cds.lpData = ctypes.c_char_p(str)
        SendMessage(hwnd, win32con.WM_COPYDATA, self.hwnd, ctypes.byref(cds))

    def OnCopyData(self, hwnd, msg, wparam, lparam):
        print(hwnd)
        print(msg)
        print(wparam)
        print(lparam)
        pCDS = ctypes.cast(lparam, PCOPYDATASTRUCT)
        print(pCDS.contents.dwData)
        print(pCDS.contents.cbData)
        print(ctypes.wstring_at(pCDS.contents.lpData))
        return 1

l = Listener()
win32gui.PumpMessages()

This gives me the error of:

2621936
74
65880
2223904
0
32
Python WNDPROC handler failed
Traceback (most recent call last):
  File "C:\Users\mastelj\workspace\stickies\stickies.py", line 67, in OnCopyData
    print(ctypes.wstring_at(pCDS.contents.lpData))
  File "C:\Python33\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u3030' in position 0: character maps to <undefined>

I've tried encoding the string to "utf-8", but it gives me an unusable string.

Was it helpful?

Solution

I figured out the error I was having. Changing the offending line to:

print(pCDS.contents.lpData.decode("ascii", "ignore"))

fixed the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top