import win32con
import win32gui
import ctypes
# 定义 COPYDATASTRUCT 结构体
class COPYDATASTRUCT(ctypes.Structure):
_fields_ = [("dwData", ctypes.c_ulong),
("cbData", ctypes.c_ulong),
("lpData", ctypes.c_char_p)]
def send_copydata_message(hwnd, data):
# 封装数据到 COPYDATASTRUCT 结构体
copydata = COPYDATASTRUCT()
copydata.dwData = 1 # 你可以使用自定义的数值
copydata.cbData = len(data)
copydata.lpData = data.encode('utf-8') # 将字符串编码为字节流
# 发送 WM_COPYDATA 消息
win32gui.SendMessage(hwnd, win32con.WM_COPYDATA, 0, ctypes.byref(copydata))
if __name__ == "__main__":
# 替换为目标窗口的标题或类名
target_window_title = "YourWindowTitle"
# 查找窗口句柄
hwnd = win32gui.FindWindow(None, target_window_title)
if hwnd == 0:
print(f"窗口标题为'{target_window_title}'的窗口未找到。")
else:
# 要发送的数据
data_to_send = "Hello, this is a test message."
# 发送 WM_COPYDATA 消息
send_copydata_message(hwnd, data_to_send)
环境是 win10 。python 版本 3.10.11
这样一段代码。
运行时报错:
Traceback (most recent call last):
File "D:\work\workspace\fetch\
ctypes.py", line 2, in <module>
import ctypes
File "D:\work\workspace\fetch\
ctypes.py", line 5, in <module>
class COPYDATASTRUCT(ctypes.Structure):
AttributeError: partially initialized module 'ctypes' has no attribute 'Structure' (most likely due to a circular import)
win32con 和 win32gui 都删掉,单独引入 ctypes 试过了,也不行
实在找不到问题所在。有没有大佬帮忙解惑