AppDomain 和本机窗口消息

发布于 2024-08-15 16:14:13 字数 1044 浏览 7 评论 0原文

我有一个也与 AppDomain 和 Windows 消息相关的问题。

托管在 Internet Explorer 中的网页,其中包含 .Net WinForms UserControl 派生控件 - HelloWorldCtl。该控件位于 C# 编写的程序集 - HelloWorldControl.dll 内。该控件使用来自另一个用 C++/CLR 编写的程序集的代码 - HelloWorldLibCPP.dll。

HelloWorldCtl 加载 HelloWorldLibCPP.dll 并调用创建 Win32 本机窗口的代码,并将该窗口放置在 HelloWorldCtl 的区域中。

导航到网页,HelloWorldCtl 加载,我可以看到它以及 HelloWorldCtl 区域中心的本机窗口。

C# 控件和本机窗口都有一些消息处理程序,并且消息都工作正常并到达 C# 控件的窗口和本机窗口;鼠标单击、重新绘制等等...但是,本机窗口的某些消息处理程序需要调用作为本机窗口父级的 C# 控件上的方法。这是使用 C# 控件实现的接口来完成的,本机窗口通过将其存储在 GCHandle 中(来自 System::Runtime::InteropServices)来保留对该接口的引用。 GCHandle 的模板。

当本机窗口中的代码尝试使用 GCHandle 调用 C# 控件上的任何方法时,此时就会发生故障。 (C++ 代码使用 /clr 编译为托管代码。)

引发的异常是:

“无法跨 AppDomains 传递 GCHandle”

我放置了一些调试代码来在 C# 和本机中显示 CurrentDomain 的 Id 和 FriendName window 后我发现这些AppDomain 并不相同。

在创建本机窗口期间,CurrentDomain 与 C# 控件的 CurrentDomain 相同,但是当本机窗口接收消息并处理这些消息时,CurrentDomain 与 C# 控件的 CurrentDomain 不同。

这种状况能改变吗?是否可以让两个本机窗口消息处理程序在与 C# 控件相同的 AppDomain 中运行?

也许还有其他建议吗?

谢谢, 罗杰

I have a problem which is also related to AppDomain's and Windows messages.

A web page to be hosted in Internet Explorer that would contain a .Net WinForms UserControl derived control - HelloWorldCtl. This control is inside a C# written assembly - HelloWorldControl.dll. The control uses code from another assembly that is written in C++/CLR - HelloWorldLibCPP.dll.

HelloWorldCtl loads HelloWorldLibCPP.dll and calls code that would create a Win32 native window and places that window in HelloWorldCtl's area.

Navigate to the web page, HelloWorldCtl loads, I can see it as well as the native window in the center of HelloWorldCtl's area.

Both the C# control and the native window have some message handlers and the messages are all working fine and reaching both the C# control's window and the native window; mouse clicks, re-paints and so on... However, some of the message handlers of the native window need to call methods on the C# control which is the parent of the native window. This is done using an interface that the C# control implements and which the native window holds a reference to by storing it in a GCHandle (from System::Runtime::InteropServices.) I used the gcroot<> template for the GCHandle.

The failure is happening at this point when code in the native window is trying to use the GCHandle to call any method on the C# control. (The c++ code is compiled as managed code with /clr.)

The exception that is thrown is :

"Cannot pass a GCHandle across AppDomains"

I put some debugging code to display the Id and FriendName of the CurrentDomain in both the C# and the native window and I found out that these AppDomains are not the same.

During the creation of the native window, the CurrentDomain is the same as that of the C# control, but when the native window receives messages and those messages are handled, the CurrentDomain is different from the C# control's.

Can this situation be changed? Is it possible to have both the native window messages hanlder run in the same AppDomain as that of the C# control?

Any other suggestions perhaps?

Thanks,
Roger

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悟红尘 2024-08-22 16:14:13

我实际上已经找到了解决该问题的方法。我成功地以类似于以下帖子中描述的方式实现了我的代码“来自 Mirality 的想法,
来自 Mirality Systems 的 Miral 的随机想法和沉思”博客,主题为“跨 AppDomains 的非托管回调”。

祝你好运,
罗杰

http://lambert.geek.nz/2007/05/ 29/unmanaged-appdomain-callback/

I have actually found a solution for the problem in question indeed. I successfully implemented my code in a similar fashion to what is described in the following post from the "Thoughts from Mirality,
Random thoughts and musings from Miral at Mirality Systems" blog under the subject "Unmanaged callbacks across AppDomains".

Good Luck,
Roger

http://lambert.geek.nz/2007/05/29/unmanaged-appdomain-callback/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文