0

I recently tried to work on an old project I had an i am not able to get the setparent to work it keeps giving me the "InvalidOperationException" error, here is the code:

private void button1_Click(object sender, EventArgs e)
    {
        Process proc = Process.Start("calc.exe");
        proc.WaitForInputIdle();
        Thread.Sleep(500);
        SetParent(proc.MainWindowHandle, this.Handle);
    }

Its being called with a button and when it tries to set the parent it errors out. Everything i can find online say that my code is right.

Austin
  • 47
  • 7
  • Where is your SetParent method ? I dont think its possible to set parent to another process. Take a look here, try it first, probably works : http://stackoverflow.com/questions/10773003/attach-form-window-to-another-window-in-c-sharp –  Jun 30 '16 at 04:50
  • `SetParent` is a native Windows API and cannot possibly throw any .NET exception, ever. The `Process.MainWindowHandle` property can. The `Control.Handle` property can too. In either case, the exception stack trace will tell you specifically where the exception is thrown, and the message will give more details than just "InvalidOperationException". There's not enough information in your question for anyone to be able to answer your question, but your debugger should be able to tell you everything you need to know. –  Jun 30 '16 at 04:54
  • It says that the process has exited so the requested information is not available, could this be an issue with windows 10? – Austin Jun 30 '16 at 14:40
  • On my machine this works fine for 32 bit apps, but throws the exception on every 64 bit app. – Dan Sep 13 '16 at 03:25

1 Answers1

2

This code below is working fine on my side (Please check the declaration of your Windows API function SetParent):

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    private void button1_Click(object sender, EventArgs e)
    {
        Process proc = Process.Start("calc.exe");
        proc.WaitForInputIdle();
        Thread.Sleep(500);
        SetParent(proc.MainWindowHandle, this.Handle);
    }

Result:

enter image description here

Hope that helps :)

  • I see you're on windows 7 could this be an issue with windows 10? – Austin Jun 30 '16 at 14:42
  • Well, in Windows 10, the calculator app is a **Windows Store app** (not a desktop app). So, `MainWindowHandle` will not work for it. Try `notepad.exe` instead of `calc.exe` and it should work.. check [this screenshot](https://s32.postimg.org/z9qcwonat/Capture.png). – 41686d6564 stands w. Palestine Jul 01 '16 at 01:43
  • You might be able to get the window handle of a Windows store app using Windows API according to [this post](https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/7e25e104-36cb-41ac-8f36-0e4c6b6146a3/finding-hwnd-of-metro-app-using-win32-api?forum=windowsgeneraldevelopmentissues), but unfortunately it's in C++. Although I'm not sure -even if you managed to get the hwnd- that you'll be able to embed a Windows store app in your application using the same approach (I might be wrong about that). Good luck :) – 41686d6564 stands w. Palestine Jul 01 '16 at 01:43