Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# unable to get handle when multiple instances of app are open in windows 8.1

C# unable to get handle when multiple instances of app are open in windows 8.1

Scheduled Pinned Locked Moved C#
helpcsharp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RRLCoder
    wrote on last edited by
    #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

    L 1 Reply Last reply
    0
    • R RRLCoder

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      RRLCoder wrote:

      I can get the process id but can't get the handle

      Can you post a SMALL examle that reproduces this problem?

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      R 1 Reply Last reply
      0
      • L Lost User

        RRLCoder wrote:

        I can get the process id but can't get the handle

        Can you post a SMALL examle that reproduces this problem?

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        R Offline
        R Offline
        RRLCoder
        wrote on last edited by
        #3

        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;
        }

        L 1 Reply Last reply
        0
        • R RRLCoder

          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;
          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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[^]

          R 1 Reply Last reply
          0
          • L Lost User

            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[^]

            R Offline
            R Offline
            RRLCoder
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups