ATTENTION ALL FANS!!! THIS BLOG HAS MOVED!!!
go to: http://www.taotekaching.com

Thursday, December 04, 2008

Native MDI Containers, Managed Child Forms, and Me...

So, at work we have this (ugly) native C++ MFC application that the Powers-That-Be insist needs a complex web-services riddled interface to our social-networking-meets-file-sharing new website.  Initially, said interface was to be in MFC native code action.  I said, "sheeeeeeeeeiiiiiiiiiiiiiiiittt".  But then the light came: why not do the interface in a .NET form and pass the form handle somehow to the native application to pull it into its window as a child window.  Any interaction between the two can be handled easily through messaging calls.  I immediately began to Google on it.  I found nothing that directly addressed this problem.  I concluded it was one of two things: either it was so easy, no one bothered documenting it, or so rarely done, no one bothered documenting it.  Either way, no one bothered documenting it...UNTIL NOW!!!

Basically my solution was to grab the parent window's class using a call to GetClassName, then send this as a command line parameter to the .NET exe via a ShellExecute call.  Once in the .NET client, I use two DllImport-ed calls: FindWindow and SetParent -- FindWindow to get the window handle from the class name I passed in as a parameter, and SetParent to, well, set the parent of my form.

The sample code is here.  Note, you'll need to change the hardcoded path in the NativeMaster C++ solution to the output of the ManagedChild project.  Hope it's helpful...

~ZagNut

Submit this story to DotNetKicks