hi im tryin to change a window style of a currently running process
basically i start a whole bunch of small exe's written in c++ and when i initially run them hidden like below
Process line = new Process();
line.StartInfo.FileName = Application.StartupPath + "\\file.exe";
line.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
line.Start();
but later on i wanna check whats happening in that exe and would like to change to windowstyle to
ProcessWindowStyle.Normal;
how can i do that ? i can save the process ID or handle to file for later reference.
Loading

AlanRank #669 · 3.6K reputationPosted Aug 16, 2007, 2:49 PM EDT
If you can get a handle to the window, then you can call the API function ShowWindow(hWnd, SW_SHOW) to make it visible.
However, the problem is getting a handle to the window in the first place as I think Process.MainWindowHandle always returns IntPtr.Zero for windows which are hidden when they're created. You could try the API function FindWindow instead.
Here are some declarations that you'll need for all this:
using System.Runtime.InteropServices;
........
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_SHOW = 5;
Daniel van Pletzen25 reputationPosted Aug 16, 2007, 2:56 PM EDT
I was hoping i didnt have to use that API and that c# had somethin like that.
AlanRank #669 · 3.6K reputationPosted Aug 17, 2007, 1:27 AM EDT
No, I don't think there's any managed alternative to ShowWindow when you're dealing with external processes. However, I have thought of a managed way to get the window handle of a hidden window which is to start the window as normal, get the handle and then hide it :)
When I ran this test program using wordpad, this approach worked quickly enough for wordpad not to have chance to open its window until I forced it to display after a 5 seconds delay:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
class Program
{
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;
static void Main()
{
Process proc = new Process();
proc.StartInfo.FileName = "wordpad.exe";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc.Start();
proc.WaitForInputIdle(); // wait until process has initialized
IntPtr hWnd = proc.MainWindowHandle;
ShowWindow(hWnd, SW_HIDE);
Thread.Sleep(5000); // wait 5 secs before displaying
ShowWindow(hWnd, SW_SHOW);
proc.WaitForExit();
}
}
Daniel van Pletzen25 reputationPosted Aug 17, 2007, 1:47 AM EDT
I was trying something similar to that earlier .. but proc.windowhandle() always returned 0, untill i used that WaitForInputIdle(), program doesnt have an input but put a try block around it ignoring error worked with out me having to sleep the thread ;)
Daniel van Pletzen25 reputationPosted Aug 17, 2007, 2:00 AM EDT
Daniel van Pletzen25 reputationPosted Aug 17, 2007, 2:04 AM EDT
1st convert to long
then type cast the long to Intptr ;)