Why do I get: ERROR_INVALID_WINDOW_HANDLE 1400 (0x578)?

  • Thread starter Thread starter apvw
  • Start date Start date
A

apvw

Guest
Wishing to embed Excel in a Panel, I "stole" a few code fragments from the web and mixed and merged them, to get the scaffolding working.

I can see that the code works up until the "SetParent".

It seems that panel1.Handle is invalid. Any ideas?

namespace embedProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

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

[DllImport("Kernel32.dll")]
private static extern IntPtr GetLastError();

private void Form1_Load(object sender, EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo("powerpnt.exe");
psi.WindowStyle = ProcessWindowStyle.Minimized;
Process p = Process.Start(psi);
Process[] pname = Process.GetProcessesByName("powerpnt");
int waitCount = 0;
while (pname.Length == 0)
{
if (waitCount > 3000)
{
// Exit with a message
MessageBox.Show("Problem starting PowerPoint", "KKZIS");
System.Windows.Forms.Application.Exit();
}
Thread.Sleep(10);
waitCount += 1;
}
// Generates error code 1400
SetParent(p.MainWindowHandle, panel1.Handle);
// Show return code of the just created process
uint rc = (uint)GetLastError();
MessageBox.Show($"{rc}");
CenterToScreen();
psi.WindowStyle = ProcessWindowStyle.Normal;
}
}
}

Continue reading...
 
Back
Top