5

I am attempting to start an external process in a Visual C# 2010 - Windows Forms application. The goal is to start the process as a hidden window, and unhide the window at a later time.

I've updated my progress:

//Initialization
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool EnableWindow(IntPtr hwnd, bool enable);
[DllImport("user32.dll")]
private static extern bool MoveWindow(IntPtr handle, int x, int y, int width, 
int height, bool redraw);

SW_SHOW = 5;

The following was placed in my main function:

ProcessStartInfo info = new ProcessStartInfo("process.exe");
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(info);

p.WaitForInputIdle();
IntPtr HWND = p.MainWindowHandle;

System.Threading.Thread.Sleep(1000);    

ShowWindow(HWND, SW_SHOW);
EnableWindow(HWND, true);
MoveWindow(HWND, 0, 0, 640, 480, true);

However, because the window was started as "hidden," p.MainWindowHandle = 0. I am not able to successfully show the window. I have also tried HWND = p.Handle with no success.

Is there a way to provide a new handle to my window? This could potentially fix my problem.

References:

MSDN ShowWindow

MSDN Forums

How to Import .dll

Matt Barr
  • 434
  • 1
  • 5
  • 17
  • 1
    Hiding a process? (dodgy) - or hiding a form? – Marc Gravell Apr 30 '12 at 17:06
  • Hiding the window of a process. For the sake of argument, let's assume it's internet explorer: `ProcessStartInfo info = new ProcessStartInfo("iexplore");` – Matt Barr Apr 30 '12 at 17:08
  • CreateNoWindow only works on a console mode app. Hidden requires a GUI app to cooperate and pay attention to the `nCmdShow` argument that Windows passes to its WinMain() function. That is however often ignored. Nothing you can do about it other than contacting the owner. – Hans Passant Apr 30 '12 at 17:44
  • I've updated my progress above. Please advise, and thank you for your feedback. – Matt Barr May 01 '12 at 15:12

3 Answers3

11

Finally, the process is operating properly. Thanks to all of your help, I came up with this fix.

The p.MainWindowHandle was 0, so I had to use the user32 FindWindow() function to get the window handle.

//Initialization
int SW_SHOW = 5;

[DllImport("user32.dll",SetLastError=true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

[DllImport("user32.dll")]
private static extern bool EnableWindow(IntPtr hwnd, bool enabled);

And in my main function:

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "notepad";
info.UseShellExecute = true;
info.WindowStyle = ProcessWindowStyle.Hidden;

Process p = Process.Start(info);
p.WaitForInputIdle();
IntPtr HWND = FindWindow(null, "Untitled - Notepad");

System.Threading.Thread.Sleep(1000);

ShowWindow(HWND, SW_SHOW);
EnableWindow(HWND, true);

References:

pinvoke.net: FindWindow()

Edit: Removed WindowShowStyle from the dllImport declaration: you can define this as an int instead. I defined an enum called WindowShowStyle to define the constants outlined in this article. It just better fits my coding patterns to have enums defined instead of using constant or hard-coded values.

Matt Barr
  • 434
  • 1
  • 5
  • 17
  • Hi Matt thanks for sharing. I know this is late, but when I try this code, Visual Studio can't recognise `WindowShowStyle`. I have checked all the links you provided but can't find any mention of this type - could you please let me know where this is coming from? (I am referring to the type of the 2nd parameter in the `ShowWindow` import declaration) – Bassie Feb 17 '16 at 15:24
  • So sorry for the delay in this answer! Hope you found what you needed. – Matt Barr Jan 04 '19 at 16:58
2

Sample code to unhide the window:

int hWnd;
Process[] processRunning = Process.GetProcesses();
foreach (Process pr in processRunning)
{
    if (pr.ProcessName == "notepad")
    {
        hWnd = pr.MainWindowHandle.ToInt32();
        ShowWindow(hWnd, SW_HIDE);
    }
}
Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
  • Let me try this when i get home. If it works, I'll accept it. Isn't there a find window method? – Matt Barr Apr 30 '12 at 17:52
  • 1
    After re-reading this, I have further concerns. There is no way to know that pr is *my* process. For instance, what if my program opened two notepad windows? I wonder if there is a better way to identify the window that I have opened. – Matt Barr Apr 30 '12 at 19:14
1

The documention details that to use ProcessWindowStyle.Hidden you must also set ProcessStartInfo.UseShellExecute to false. http://msdn.microsoft.com/en-us/library/system.diagnostics.processwindowstyle.aspx

You would have to somehow know the window handle to unhide it later.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • Please provide documentation. Testing shows that `ProcessStartInfo.UseShellExecute = true` is the only way that `ProcessWindowStyle.Hidden` will work. – Matt Barr May 01 '12 at 14:27
  • 1
    looking up ProcessWindowStyle on MSDN was assumed; but I've added the link for you. – Peter Ritchie May 01 '12 at 15:35
  • Thank you. Unfortunately, for whatever reason, it does not work in my case. As mentioned above, I tested with both `true` and `false`. While using `false`, the window failed to hide. – Matt Barr May 01 '12 at 15:37