views:

57

answers:

2

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

A: 

Hi Roger, We are also facing this problem. I hope you might have got the solution by this time. Can you please share the solution. Thanks in advance.

A: 

Hi,

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/

Roger Khoueiry