我有一个小的Python 3.3脚本,可以成功发送(SendMessage)WM_COPYDATA消息(从here启发,与XYplorer一起使用:]
import win32api
import win32gui
import struct
import array
def sendScript(window, message):
CopyDataStruct = "IIP"
dwData = 0x00400001 #value required by XYplorer
buffer = array.array("u", message)
cds = struct.pack(CopyDataStruct, dwData, buffer.buffer_info()[1] * 2 + 1, buffer.buffer_info()[0])
win32api.SendMessage(window, 0x004A, 0, cds) #0x004A is the WM_COPYDATA id
message = "helloworld"
sendScript(window, message) #I write manually the hwnd during debug
现在,我仍然需要使用Python编写一个接收器脚本。 answer中的脚本似乎可以正常工作(在更正了print
格式的所有print()
语句之后)。 似乎,因为它打印出接收到的消息的所有属性(hwnd
,wparam
,lparam
等)。[[除外消息的内容。我收到一个错误,UnicodeEncodeError。更具体地说,
Python WNDPROC handler failed
Traceback (most recent call last):
File "C:\Python\xxx.py", line 45, in OnCopyData
print(ctypes.wstring_at(pCDS.contents.lpData))
File "C:\Python\python-3.3.2\lib\encodings\cp850.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 10-13: character maps to <undefined>
我不知道如何解决它,也因为我没有在消息中使用“花哨”字符,所以我真的不明白为什么会收到此错误。我也尝试过在print(ctypes.wstring_at(pCDS.contents.lpData))
中设置不同的消息长度以及仅使用string_at
,但没有成功(在后一种情况下,我获得了二进制字符串)。
ctypes.wstring
(在print (ctypes.wstring_at(pCDS.contents.lpData))
行中)可能不是发送方发送的字符串类型。尝试将其更改为:print (ctypes.string_at(pCDS.contents.lpData))