Embedding a Console App in WPF with WinAPI, UII and Windows Forms Integration

1 Comment

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;
using System.Windows;
using Microsoft.Uii.Csr;

namespace Carl.UIISample
{
    public partial class WindowsFormsHostSample : Window
    {
        System.Diagnostics.Process cmdProcess;

        public WindowsFormsHostSample()
        {
            InitializeComponent();
        }

        private void BtnRun_Click(object sender, RoutedEventArgs e)
        {
            // Run console app
            string exe = @"cmd.exe";
            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)
        {
            // Create a panel
            System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();

            // Use Win32API to set the command process to the panel
            Win32API.SetParent(cmdProcess.MainWindowHandle, panel.Handle);

            // Set the Windows Forms Host to the panel
            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

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Embedding a Console App in WPF with WinAPI, UII and Windows Forms Integration

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *