Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:


In interfacing some new .NET code to a legacy c++ application, I would find it very convenient to be able to convert the Process "Handle" to a proper Windows SDK HWND so I can make my .NET forms proper child windows of my legacy app. Anybody out there know how to do this?

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
private static extern int SetParent(IntPtr hwndChild, IntPtr hwndParent);


void SetMyParent (IntPtr parentHwnd) {
   Process p = System.Diagnostics.Process.GetCurrentProcess();
   SetParent(p.Handle, parentHwnd); // this doesn't work!
}


I've tried several variations on this theme, all to no avail. I can make this work by using the User32.dll function to GetForegroundWindow() rather than p.Handle, but I can't guarantee that the window I launch will always be on top. I've managed to end up with Microsoft Outlook as my child window by trying that.


I don't understand why p.Handle isn't identical to an old fashioned HWND, and even if it isn't, why there isn't an easy conversion between the two types somewhere in InteropServices.


Thanks for any help on this!

Note: Thanks Chris, for your post (below). Granted, I have confused "process" with "window" here, but the legacy Windows SDK works almost exclusively with window handles, so if a single process has multiple windows, that still begs the question of how to enumerate them in .NET without resorting to FindWindow or EnumWindows. The problem with FindWindow is that it demands a class name, and when I examine .NET window class names, they seem to be constructed at runtime to be unique, but have little relation to the actual form class name. I'll look at the details of the link you gave to see if that provides a usable workaround for this .NET shortcoming. :thumbsup:

Comment: Thanks also to Eddy, which I've marked as the Answer. Getting lazy with code-completion led me to not see the .MainWindowHandle member of process, which is the HWND I need. Also, interesting to note that for a form, the "this" pointer has a Handle member which is the form's HWND.

Posted
Updated 4-Feb-10 4:05am
v3

It's not identical, because that makes zero sense. A process has a handle, and it can have many windows associated with it. It's a different thing entirely.

This[^] seems to have a solution, found it with google.
 
Share this answer
 
sloughin wrote:
I don't understand why p.Handle isn't identical to an old fashioned HWND, and even if it isn't, why there isn't an easy conversion between the two types somewhere in InteropServices.


They represent different things - a proces and a Window. A proces like a console has a handle, but no forms. There's a WaitForInputIdle in the code below to give the proces the time to load and build it's mainform.

C#
p =Process.Start("Calc.exe"); 
p.WaitForInputIdle(); 
hMainWindow = p.MainWindowHandle;
SetParent(hMainWindow, this.Handle);

Enjoy :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900