1

(This is not a repost as explain in comments)

My purpose is to host an old .exe (an old .exe generated from VB6 who work well alone) inside my container in WPF.

My solution work pretty well with some application like notepad but dont work with the targeted VB6 .exe, the VB6 window is never hosted inside my app, after 2 or 3 try she will just crash and nothing more.

This is my code from my WPF Container :

View :

<WindowsFormsHost Grid.Row="1" x:Name="windowsFormsHost1" Height="500" Width="650" ></WindowsFormsHost>

Code Behind :

        [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

        [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("user32")]
    private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);


private void button1_Click(object sender, RoutedEventArgs e)
{
    try
    {
        Process p = Process.GetProcessById(Int32.Parse(ProcessNameTextBox.Text));

        // Get the main handle
        var appWin = p.MainWindowHandle;

        System.Windows.Forms.Panel _panel = new System.Windows.Forms.Panel();
        windowsFormsHost1.Child = _panel;

        // Put it into this form
        SetParent(appWin, windowsFormsHost1.Child.Handle);
        MessageBox.Show(appWin.ToString());

        // Remove border and whatnot

        SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);

        // Move the window to overlay it on this window
        MoveWindow(appWin, 0, 0, (int)this.Width, (int)this.Height, true);

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error");
    }
}

I tried severals code samples and different approach (like start the .exe from my code behind, put a timer,...) but i couldn't achieve it, i think i should miss a specificity or something...

thanks a lot for your help !

EDIT : The MessageBox after the SetParent give me the information that it could "localize" my targeted vb6 .exe with concordant value.

Nimp
  • 441
  • 1
  • 5
  • 15
  • Possible duplicate of [Can/how do you host a full VB6 Form in a C# WPF app?](http://stackoverflow.com/questions/2239845/can-how-do-you-host-a-full-vb6-form-in-a-c-sharp-wpf-app) – mm8 Feb 16 '17 at 21:22
  • No i have already read this post, my purpose is different i have a .exe (so a normal application window) and i want to host this window inside my wpf container. My code work with a lot of app but not this .exe generated from VB6 (and this application work pretty good alone). – Nimp Feb 16 '17 at 21:24

0 Answers0