HWND是否跨流程边界有效? (win32) [英] Is a HWND valid across process boundaries? (win32)

查看:203
本文介绍了HWND是否跨流程边界有效? (win32)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

是一个窗口句柄,它在进程A中被接收(例如,使用FindWindow(..))在任何其他进程中都有效吗?

我知道我可以使用RegisterMessage定义系统范围内的唯一消息.我很想相信窗口句柄在整个系统中也是有效的.但是,这种行为是否得到保证?或者它会在大多数时候起作用",并且在某些特殊情况下,事情将完全出错.

我在做什么:
我我正在开发一个程序,该程序必须能够设置第三方窗口的位置(将给定的窗口移动到另一个屏幕).目标系统是一台PC,最多可连接五台显示器.一个屏幕用作所有应用程序的输入屏幕,其他屏幕用作我们所谓的电视屏幕.

我设法建立了一个系统范围的钩子,该钩子侦听WM_CREATE,WM_SETTEXT,WM_DESTROY消息. 有趣"是指窗口(应重新定位的窗口)是使用进程ID和窗口标题标识的.

目前,我使用FindWindow(应用程序主窗口的名称")查找自己的应用程序并向其发布消息(这发生在我的钩子中).
是否可以将此窗口的HWND放入共享段中,从而避免调用findwindow?
使用EnumWindows是更好的方法吗? (这会占用很多资源吗?)
FindWindow最终还是会使用EnumWindows吗?
是否有办法确保HWND仍然有效?

我还很新Win32 API,但我非常了解C和C ++.

Hi all,

is a window handle, which is received (e.g. using FindWindow(..)) in process A valid in any other process?

I know that i can use RegisterMessage to define a system-wide unique message. I am tempted to believe that window handles are valid throughout the system as well. However, is this behaviour guaranteed or is it something which will "work most of the time", and under some special circumstances things will go totally wrong.

What I am doing:
I am developing a program which must be able to set the position of third-party windows (moving a given window to another screen). The target system is a PC with up to five monitors attached. One screen is used as an input screen for all applications and the others are used as what we call TV screens.

I managed to set-up a system-wide hook which listens for WM_CREATE, WM_SETTEXT, WM_DESTROY messages. "Interesting" windows (the ones which should be repositioned) are identified using a process id and the window's title.

Currently I use FindWindow("Name of my app's mainwindow") to find my own application and post a message to it (This happens inside my hook).
Would it be possible to put this window's HWND into a shared segment, thus avoiding calls to findwindow?
Would it be a better approach to use EnumWindows? (Does this take a lot of resources?)
Does FindWindow eventually use EnumWindows anyway?
Is there a way to make sure a HWND is still valid?

I am quite new to the win32 API, but I know C and C++ quite well.

推荐答案

窗口句柄在同一进程中的所有进程中均有效且可用桌面.当前用户启动的所有内容都在同一桌面上.

是的,您可以将HWND放置在共享内存中,以供多个进程使用.要检查HWND是否仍然有效,可以调用IsWindow().但是可能存在争用情况-调用IsWindow()后窗口可能会立即消失.

FindWindow似乎仍然使用EnumWindows.
A window handle is valid and usable in all processes that are in the same desktop.  Everything started by the current user is in the same desktop.

Yes, you could place the HWND in shared memory for use by multiple processes.  To check if an HWND is still valid you can call IsWindow().  But there is a possible race condition - a window could disappear immediately after you call IsWindow().

It seems likely that FindWindow uses EnumWindows anyway.


这篇关于HWND是否跨流程边界有效? (win32)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆