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
qile1
V2EX  ›  Python

请教: Python 如何通过句柄(handle)发送和接收消息,样例取得的 handle 为啥不一样!

  •  
  •   qile1 · 2018-05-29 22:15:36 +08:00 · 5068 次点击
    这是一个创建于 2150 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题,想通过 handle 让别程序关闭我编译为 exe 的程序, 原理是通过向 handle 发送关闭信号,程序检测到,就执行相关代码,顺便想看看一个程序是如何通过句柄控制其他程序的,暂时不使用其他进程间通讯技术 hinst = win32gui.GetModuleHandle(None)

    hWindow = pywintypes.HANDLE()

    这两个获取的句柄不一样,使用 spyxx 获取句柄后,手动发送信号也没反应,不知道是什么原因,在此请教,不胜感激。 代码如下

    # -*- coding: utf-8 -*-
    
    import time,pywintypes
    import threading
    from ctypes import *
    from ctypes.wintypes import *
    import win32con,win32gui
    user32 = windll.user32
    
    class ListenThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
    
        def run(self):
            #保存线程 ID
            # self.ident = threading._get_ident()
    
            msg = MSG()
            #创建线程消息队列
            user32.PeekMessageW(byref(msg), None, win32con.WM_USER,win32con.WM_USER, win32con.PM_NOREMOVE)
            self.hinst = win32gui.GetModuleHandle(None)
            print(self.hinst)
            hWindow = pywintypes.HANDLE()
            print("hWindow:", hWindow)
            #消息循环
            while True:
                user32.GetMessageW(byref(msg), None, 0, 0)
                if msg.message == win32con.WM_QUIT:
                    print ('thread quit' )
                    break
                elif msg.message == win32con.WM_USER + 779:
                    print( 'I get it' )
                else:
                    print("ddd")
                    time.sleep(1)
                user32.TranslateMessage(byref(msg))
                user32.DispatchMessageW(byref(msg))
    
    
    print ('create thread' )
    hThread = ListenThread()
    print ('start thread')
    hThread.start()
    
    for i in range(5):
        time.sleep(1)
        user32.PostMessageW(hThread.hinst, win32con.WM_USER + 779, 0, 0)
        print ("ddddd")
    else:
        user32.PostThreadMessageW(hThread.hinst, win32con.WM_QUIT, 0, 0)
    

    怕代码乱码在下面网站也贴了代码,内容一样的 https://paste.ubuntu.com/p/wNtwywrYRF/

    1 条回复    2018-05-30 15:11:07 +08:00
    GeruzoniAnsasu
        1
    GeruzoniAnsasu  
       2018-05-30 15:11:07 +08:00
    emmmmm win32gui 都快忘得差不多了

    getmodulehandle 获取的是进程实例句柄,c++ api 里的类型是 HINSTANCE

    然后,postmessage 的目标应该是一个窗口句柄,类型是 HWND,消息循环也必须有一个窗口才能工作

    我不太清楚 python 里封装成啥样,但反正给 instance postmessage 肯定是没用的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3359 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:07 · PVG 20:07 · LAX 05:07 · JFK 08:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.