set focus on the program with win32

G

Guest

Hello Everyone,

I am not sure what am I doing wrong here. I spend almost whole day working
on this. I am trying to set focus on the process that I started by
Process.start. Below is my code
Process process;
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = @"C:\Program Files\test.exe";
process.StartInfo.WorkingDirectory = @"C:\Program Files\";
process.Start();

IntPtr hWnd =
System.Diagnostics.Process.GetProcessById(process.Id).MainWindowHandle;
if (!hWnd.Equals(IntPtr.Zero))
{

if (IsIconic(hWnd))
{

ShowWindow(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

}

if (process.Responding)
{

SendKeys.SendWait("{ENTER}");
}
else
{
process.Kill();
}

[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

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

[DllImport("user32.dll")]
private static extern int SetForegroundWindow (IntPtr hWnd);


while debugging my code never goes inside this if
(!hWnd.Equals(IntPtr.Zero)) statement. I tried to rewrite the code after
reading lot of articles on internet, but it didn't work. Can anyone please
help me on this. I am trying to setfocus on the "test" program so that I can
press enter key, but since there is no focus on the test program, the enter
key is useless. I can start the program successfully, but I cannot put the
focus on that program so that enter key will work.

Any help will be greatly appreciated
 
M

Michael Letterle

Hello Everyone,

I am not sure what am I doing wrong here. I spend almost whole day working
on this. I am trying to set focus on the process that I started by
Process.start. Below is my code
Process process;
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = @"C:\Program Files\test.exe";
process.StartInfo.WorkingDirectory = @"C:\Program Files\";
process.Start();

IntPtr hWnd =
System.Diagnostics.Process.GetProcessById(process.Id).MainWindowHandle;
if (!hWnd.Equals(IntPtr.Zero))
{

if (IsIconic(hWnd))
{

ShowWindow(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

}

if (process.Responding)
{

SendKeys.SendWait("{ENTER}");
}
else
{
process.Kill();
}

[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

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

[DllImport("user32.dll")]
private static extern int SetForegroundWindow (IntPtr hWnd);

while debugging my code never goes inside this if
(!hWnd.Equals(IntPtr.Zero)) statement. I tried to rewrite the code after
reading lot of articles on internet, but it didn't work. Can anyone please
help me on this. I am trying to setfocus on the "test" program so that I can
press enter key, but since there is no focus on the test program, the enter
key is useless. I can start the program successfully, but I cannot put the
focus on that program so that enter key will work.

Any help will be greatly appreciated

perhaps you need to call process.WaitForInputIdle() after calling
process.Start()?
 
W

Willy Denoyette [MVP]

Vinki said:
Hello Everyone,

I am not sure what am I doing wrong here. I spend almost whole day working
on this. I am trying to set focus on the process that I started by
Process.start. Below is my code
Process process;
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = @"C:\Program Files\test.exe";
process.StartInfo.WorkingDirectory = @"C:\Program Files\";
process.Start();

IntPtr hWnd =
System.Diagnostics.Process.GetProcessById(process.Id).MainWindowHandle;
if (!hWnd.Equals(IntPtr.Zero))
{

if (IsIconic(hWnd))
{

ShowWindow(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

}

if (process.Responding)
{

SendKeys.SendWait("{ENTER}");
}
else
{
process.Kill();
}

[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

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

[DllImport("user32.dll")]
private static extern int SetForegroundWindow (IntPtr hWnd);


while debugging my code never goes inside this if
(!hWnd.Equals(IntPtr.Zero)) statement. I tried to rewrite the code after
reading lot of articles on internet, but it didn't work. Can anyone please
help me on this. I am trying to setfocus on the "test" program so that I can
press enter key, but since there is no focus on the test program, the enter
key is useless. I can start the program successfully, but I cannot put the
focus on that program so that enter key will work.

Any help will be greatly appreciated

You'll have to wait after Start until the process has it's main window created.
When the process has a UI you can use WaitForInputIdle, else you'll have to Sleep until the
MainWindowHandle returns a non null handle value..
Something like this will do...

....
process.Start();
System.Threading.Thread.Sleep(200);
IntPtr hWnd = process.MainWindowHandle;
while(hWnd == IntPtr.Zero)
{
System.Threading.Thread.Sleep(100);
// or:
// process.WaitForInputIdle(100);
hWnd = process.MainWindowHandle;
Application.DoEvents(); // or run this code on a non UI thread!
}
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}
..
Take care that you don't wait endlessly in the while loop!!

Willy.
 
G

Guest

Thanks a lot Willy. That was the problem. I really appreciate your help.

Thanks again.

Willy Denoyette said:
Vinki said:
Hello Everyone,

I am not sure what am I doing wrong here. I spend almost whole day working
on this. I am trying to set focus on the process that I started by
Process.start. Below is my code
Process process;
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = @"C:\Program Files\test.exe";
process.StartInfo.WorkingDirectory = @"C:\Program Files\";
process.Start();

IntPtr hWnd =
System.Diagnostics.Process.GetProcessById(process.Id).MainWindowHandle;
if (!hWnd.Equals(IntPtr.Zero))
{

if (IsIconic(hWnd))
{

ShowWindow(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);

}

if (process.Responding)
{

SendKeys.SendWait("{ENTER}");
}
else
{
process.Kill();
}

[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

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

[DllImport("user32.dll")]
private static extern int SetForegroundWindow (IntPtr hWnd);


while debugging my code never goes inside this if
(!hWnd.Equals(IntPtr.Zero)) statement. I tried to rewrite the code after
reading lot of articles on internet, but it didn't work. Can anyone please
help me on this. I am trying to setfocus on the "test" program so that I can
press enter key, but since there is no focus on the test program, the enter
key is useless. I can start the program successfully, but I cannot put the
focus on that program so that enter key will work.

Any help will be greatly appreciated

You'll have to wait after Start until the process has it's main window created.
When the process has a UI you can use WaitForInputIdle, else you'll have to Sleep until the
MainWindowHandle returns a non null handle value..
Something like this will do...

....
process.Start();
System.Threading.Thread.Sleep(200);
IntPtr hWnd = process.MainWindowHandle;
while(hWnd == IntPtr.Zero)
{
System.Threading.Thread.Sleep(100);
// or:
// process.WaitForInputIdle(100);
hWnd = process.MainWindowHandle;
Application.DoEvents(); // or run this code on a non UI thread!
}
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}
..
Take care that you don't wait endlessly in the while loop!!

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top