In this post we will look at how to embed a Console App into a WPF application. We will do this utilizing the Microsoft User Interface Integration Framework, which is used with Unified Service Desk integrations. In this example we will work outside of USD, but this can be also applied to USD.
Create a new WPF App project:

You will see:

Add the assemblies:
- WindowsFormsIntegration
- System.Windows.Forms
Note the default location for the assembly is %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll.
We will be using Windows Forms Integration to host our console app.
Next, we will add the assemblies for UII. Note you can use the UII assemblies which contain the WinAPI, or you can just use the WinAPI assemblies instead.
We can do this through NuGet. Search for UII and select the Microsoft.CrmSdk.UII.CommonAssemblies:

Now we will create our window. In the XAML, add a WindowsFormsHost.
Create 2 buttons:
- Run – this will open a command prompt
- Set Parent – this will set the parent of the command prompt to the

Now the code. Once the command prompt is opened, we will get its handle. Then, we will create a new panel, set the parent of the command to the panel, and then use that panel as a child to the WindowsFormsHost control:
using System.Diagnostics; |
public partial class WindowsFormsHostSample : Window |
System.Diagnostics.Process cmdProcess; |
public WindowsFormsHostSample() |
private void BtnRun_Click( object sender, RoutedEventArgs e) |
var processStartInfo = new System.Diagnostics.ProcessStartInfo(exe); |
processStartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(exe); |
cmdProcess = Process.Start(processStartInfo); |
private void BtnSet_Click( object sender, RoutedEventArgs e) |
System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel(); |
Win32API.SetParent(cmdProcess.MainWindowHandle, panel.Handle); |
winFormsHost.Child = panel; |
Running this, we see the console app open by clicking Run, and then being embedded in the WPF form by clicking Set Parent:

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.
IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.
THANK YOU, AND LET'S KEEP LEARNING TOGETHER.
CARL
https://www.youtube.com/carldesouza
hi work good,
but if i add this:
processStartInfo.Verb = “runas”;
to the prosses it fail.
i need the cmd to run as admin.
can you help in that?
thx boaz.
VS 17.12
This does not work – the CMD window does not embed?
Perhaps needs an update for latest VS?
Thanks,
Finally figured why ‘not working’ – you (may) need to use SetWindowPos to relocate the target process to 0,0 in the host panel.