V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
sun82kg
V2EX  ›  Python

求助,实在找不到问题所在。ctypes 库

  •  
  •   sun82kg · 93 天前 · 725 次点击
    这是一个创建于 93 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 试过了,也不行
    实在找不到问题所在。有没有大佬帮忙解惑
    Vegetable
        1
    Vegetable  
       93 天前
    D:\work\workspace\fetch\ctypes.py 和内置的 ctypes 包的名字冲突了,改成别的,比如 D:\work\workspace\fetch\ctypes_wrapper.py ,然后搞清楚你这个文件到底在引用谁,把引用都修改一下
    sun82kg
        2
    sun82kg  
    OP
       93 天前
    找到问题了。我被自己蠢哭
    sun82kg
        3
    sun82kg  
    OP
       93 天前
    @Vegetable 确实是这个问题,谢谢,我真的是蠢哭了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3130 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 14:01 · PVG 22:01 · LAX 07:01 · JFK 10:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.