Using SetParent with a Winforms Exe not working



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")
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.

2 responses »

  1. hi anoriginalidea,

    Thanks for the tips. However, I did what you said: setting the “ShowInTaskbar” property on the main form of the process I are running but without any luck. The process still run outside of my main form. Do you have any further suggestions? Thanks in advance.

    my environment: windows 7, Microsoft VS 2010,

    Best regard,
    Carlos Yu

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s