2

WM_COPYDATA メッセージを正常に送信 (SendMessage) する小さな Python 3.3 スクリプトがあります (ここから着想を得て、 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 でレシーバー スクリプトを作成する必要があります。この回答printのスクリプトは機能しているようです (フォーム内のすべてのステートメントを修正した後print())。メッセージの内容を除いて、受信したメッセージのすべてのプロパティ ( 、、など)を出力するためと思われます。代わりにUnicodeEncodeErrorというエラーが発生します。すなわち、hwndwparamlparam

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たが、成功しませんでした (後者の場合、バイナリ文字列を取得します)。

4

1 に答える 1