C# unable to get handle when multiple instances of app are open in windows 8.1
-
I have been trying to start multiple instances of iexplore and other apps then get the handle so i can reposition it and other actions. It works great in Windows 7 using myProcess.Id to find the process and retrieve the MainWindowHandle. It also works in Windows 8.1 if there is only 1 instance of the application running. The Problem: When i get more than one instance of the app open in W8.1 I can not find the handle. I can get the process id but can't get the handle even by looping through all processes and looking for the handle. Help
-
I have been trying to start multiple instances of iexplore and other apps then get the handle so i can reposition it and other actions. It works great in Windows 7 using myProcess.Id to find the process and retrieve the MainWindowHandle. It also works in Windows 8.1 if there is only 1 instance of the application running. The Problem: When i get more than one instance of the app open in W8.1 I can not find the handle. I can get the process id but can't get the handle even by looping through all processes and looking for the handle. Help
-
This is a bit long but shows most of what i've tried
//typical prcess start
string ProcToStart = "iexplore";
int saveProcId;Process myProcess = new Process(); myProcess.StartInfo.FileName = TargetFile; myProcess.StartInfo.Arguments = TargetArguments; myProcess.StartInfo.UseShellExecute = true; //seems to make no difference myProcess.Start(); saveProcId = myProcess.Id;
1-the simple approach, just trying to access myProcess.MainWindowHandle gets 0 even after a 10 min sleep
2--here is one method looping through all processes
I also tried this wraped in: while (proc.MainWindowHandle == IntPtr.Zero) (ran forever & didnt find a hit)....
foreach (Process proc in Process.GetProcesses())
{
if (proc.Id == saveProcId)
{
Console.WriteLine(" PID: " + proc.Id + " Handle: " + proc.MainWindowHandle);
}
}3--Here is another attempt using ....GetProcessById
try
{
ChosenProc = Process.GetProcessById(ProcId);
Console.WriteLine("Your Process Info: " );
Console.WriteLine(" Id: " + ChosenProc.Id
+ " Handle: " + ChosenProc.Handle
+ " MainWinHndl: " + ChosenProc.MainWindowHandle
+ " Name: " + ChosenProc.ProcessName);
return true;
}
catch
{
ChosenProc = null;
Console.WriteLine("Incorrect entry.");
return false;
} -
This is a bit long but shows most of what i've tried
//typical prcess start
string ProcToStart = "iexplore";
int saveProcId;Process myProcess = new Process(); myProcess.StartInfo.FileName = TargetFile; myProcess.StartInfo.Arguments = TargetArguments; myProcess.StartInfo.UseShellExecute = true; //seems to make no difference myProcess.Start(); saveProcId = myProcess.Id;
1-the simple approach, just trying to access myProcess.MainWindowHandle gets 0 even after a 10 min sleep
2--here is one method looping through all processes
I also tried this wraped in: while (proc.MainWindowHandle == IntPtr.Zero) (ran forever & didnt find a hit)....
foreach (Process proc in Process.GetProcesses())
{
if (proc.Id == saveProcId)
{
Console.WriteLine(" PID: " + proc.Id + " Handle: " + proc.MainWindowHandle);
}
}3--Here is another attempt using ....GetProcessById
try
{
ChosenProc = Process.GetProcessById(ProcId);
Console.WriteLine("Your Process Info: " );
Console.WriteLine(" Id: " + ChosenProc.Id
+ " Handle: " + ChosenProc.Handle
+ " MainWinHndl: " + ChosenProc.MainWindowHandle
+ " Name: " + ChosenProc.ProcessName);
return true;
}
catch
{
ChosenProc = null;
Console.WriteLine("Incorrect entry.");
return false;
}RRLCoder wrote:
This is a bit long but shows most of what i've tried
InvalidOperationException? Try using the full path to the application;
Process myProcess = new Process(); myProcess.StartInfo.FileName = @"C:\\Program Files\\Internet Explorer\\iexplore.exe"; myProcess.StartInfo.Arguments = @"http://www.codeproject.com"; myProcess.Start(); Console.WriteLine(myProcess.Id); Console.ReadLine();
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
RRLCoder wrote:
This is a bit long but shows most of what i've tried
InvalidOperationException? Try using the full path to the application;
Process myProcess = new Process(); myProcess.StartInfo.FileName = @"C:\\Program Files\\Internet Explorer\\iexplore.exe"; myProcess.StartInfo.Arguments = @"http://www.codeproject.com"; myProcess.Start(); Console.WriteLine(myProcess.Id); Console.ReadLine();
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
Thanks... I just tried it both ways (fully qualified and just the iexplore) followed by: Console.WriteLine("IE Instance started... PID= " + myProc.Id + myProc.MainWindowHandle); I get an unhandled exception on the myProcess.MainWindowHandle no matter how much of a sleep or if i loop looking for the process id. Again, it works find if there is only 1 instance of the target app (iexplor in this case) open.