好或坏 - SetParent() win32 API 在不同进程之间 [英] Good or evil - SetParent() win32 API between different processes

查看:41
本文介绍了好或坏 - SetParent() win32 API 在不同进程之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SetParent 函数接受一个子窗口句柄和一个新的父窗口句柄.当子窗口位于不同的 Windows 进程中时,这似乎也有效.

The SetParent function takes a child and new parent window handle. This also seems to work when the child window is in a different Windows process.

我看到了一篇帖子声称这一点不受官方支持,但 当前文档 不支持不再提这个了.这是当前文档中的缺陷,还是这种行为发生了变化?

I have seen a post that claims this is not officially supported, but the current docs don't mention this any more. Is this a flaw in the current docs, or did this behavior change?

HWND WINAPI SetParent(
  __in      HWND hWndChild,
  __in_opt  HWND hWndNewParent
);

推荐答案

可以与不同进程中的窗口建立父子关系.让它在所有情况下都能正常工作是很棘手的.您可能需要调试各种奇怪的症状.

You can have a parent-child relationship with the windows in different processes. It's tricky to get it to work right in all cases. You may have to debug various strange symptoms.

通常,单独进程中的窗口会使用单独的消息泵从单独的输入队列中获取消息.当您对另一个进程中的窗口使用 SendMessage 时,它实际上发布到另一个窗口的队列中,在那里进行处理,并且返回被有效地编组回原始进程.因此,如果其中一个进程停止处理消息,您也可以有效地锁定另一个进程.(即使在一个进程中,当窗口是在不同的线程上创建并且线程队列尚未附加时也是如此.)

Normally, windows in separate processes would get their messages from separate input queues using separate message pumps. When you use SendMessage to a window in another process, it's actually posted to the other window's queue, processed there, and the return is effectively marshaled back to the original process. So if one of the processes stops handling messages, you can effectively lock up the other as well. (That's true even within a process when the windows are created on different threads and the thread queues haven't been attached.)

但是当你在不同线程中设置窗口之间的父/子关系时,Windows会将这些输入队列附加在一起,迫使消息处理同步.您不再处于正常情况,但您面临着同样的问题:一个窗口的处理挂起有效地挂起另一个进程.

But when you set up the parent/child relationship among windows in different threads, Windows attaches those input queues together, forcing the message processing to be synchronous. You're no longer in the normal case, but you face the same kinds of problems: a hang in the processing for one window effectively hangs the other process.

注意在参数中传递指针的消息.指针在接收过程中将无效.(有几个例外,例如 WM_COPYDATA,它会在接收过程中为您重新创建数据.但即使是这些也有限制.)

Watch out for messages that pass pointers in the params. The pointers will not be valid in the receiving process. (There are a couple of exceptions, like WM_COPYDATA, which recreates the data in the receiving process for you. But even those have limitations.)

当窗户被破坏时,你必须特别小心.如果可能,在销毁任一窗口之前断开父子关系.如果不可能,那么最好在销毁父窗口之前手动销毁子窗口.通常,销毁父进程会导致子进程自动销毁,但是当子进程处于另一个进程(或未附加线程)时很容易挂起.

You have to be especially careful when the windows are being destroyed. If possible, disconnect the parent-child relationship before destroying either window. If it's not possible, then it's probably best to manually destroy the child window before the parent is destroyed. Normally, destroying a parent will cause the children to be destroyed automatically, but it's easy to get hangs when the child is in another process (or un-attached thread).

在较新版本的 Windows (Vista+) 中,如果进程以不同的完整性级别运行,您还可能会遇到一些安全问题.

In newer versions of Windows (Vista+), you can also hit some security speedbumps if the processes run at different integrity levels.

感谢 IInspectable 在我之前的回答中指出了一个错误.

这篇关于好或坏 - SetParent() win32 API 在不同进程之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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