Monthly Archives: February 2010

Using SetParent with a Winforms Exe not working

Standard

violence

Not a thrilling philosophical treatise on user experience this week folks.  Instead a problem that frustrated me for the last hour…

The SetParent api allows a windows developer to use Win32 host the window from one process into another process.

This code:

Declare Auto Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr

Dim loProcess As Process
loProcess = Process.Start("path to a winforms executable")
loProcess.WaitForInputIdle()
loProcess.Refresh()
SetParent(loProcessInfo.MainWindowHandle, Me.Handle)

…works fine for notepad.exe, but wasn’t working at all for a Winforms application I was executing.

For some reason, MainWindowHandle of the process and always returned zero.  Very frustrating.

It turns out that the “ShowInTaskbar” property on the main form of the process you are running must be set = true for this to work.

So there you go.

For a simple overview of the technique, take a look at Hosting Exe Applications in a Winform Project.

Advertisements