windows 好或坏 - SetParent() 不同进程之间的 win32 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3459874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:01:16  来源:igfitidea点击:

Good or evil - SetParent() win32 API between different processes

windowswinapi

提问by Bernard Vander Beken

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

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

I have seen a postthat claims this is not officially supported, but the current docsdon'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
);

回答by Adrian McCarthy

You canhave 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.

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

Normally, windows in separate processes would get their messages from separate input queues using separate message pumps. When you use SendMessageto a window in another process, it's actually postedto 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.)

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

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.

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

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.)

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

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 beforethe 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).

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

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

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

Thanks to IInspectable who pointed out an error in my earlier answer.

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

回答by user3400760

Just remove WS_CHILDWINDOWfrom the child window. It avoid locks.
Sorry , that has not been helped

只需WS_CHILDWINDOW从子窗口中删除即可。它避免了锁。
抱歉,这没有帮助