Nafees abbasi
Nafees abbasi

Reputation: 53

IntPtr always Zero C#

I'm starting internet explorer process. the problem is that it always return zero in p.MainWindowHandle. my objective is to get mainwindowHandler and minimize that particular window which is just started. the same code is working with chrome browser. but in internet explorer it's not working. my code is below.

Process p = Process.Start("IEXPLORE.EXE", "www.google.com");
ShowWindow(p.MainWindowHandle, 2);

ShowWindow is a method resize window.

Upvotes: 2

Views: 379

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186748

Among the many other reasons (see comments to the question), you have to wait for process to create the main window:

  // Process is IDisposable, wrap it into using
  using (Process p = Process.Start("IEXPLORE.EXE", "www.google.com")) {
    ...
    // wait till the process creates the main window
    p.WaitForInputIdle();

    // Main window (if any) has been created, so we can ask for its handle
    ShowWindow(p.MainWindowHandle, 2);
  }

Upvotes: 2

Related Questions