0

I want to write a program that does the same as the desktop ( like second desktop ) , I mean I want to run processes GUI not on the explorer process, I want to run it on my Form. I found that I can use User32.dll and I found the code for that . here is the code:

private void LoadApplication(string path, IntPtr handle)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        int timeout = 10 * 1000;     // Timeout value (10s) in case we want to cancel the task if it's taking too long.

        Process p = Process.Start(path);

        //p.WaitForInputIdle();
        IntPtr Handle = new IntPtr();
        for (int i = 0; Handle == IntPtr.Zero && i < 300; i++)
        {
            Handle = p.MainWindowHandle;
            Thread.Sleep(10);
        }

        while (Handle == IntPtr.Zero)
        {
            System.Threading.Thread.Sleep(10);
            p.Refresh();

            if (sw.ElapsedMilliseconds > timeout)
            {
                sw.Stop();
                return;
            }
        }

        SetParent(Handle, handle);      // Set the process parent window to the window we want
        SetWindowPos(Handle, 0, 0, 0, 0, 0, 0x0001 | 0x0040);       // Place the window in the top left of the parent window without resizing it
    }

if I run that code with a path to cmd , internet Explorer or even notepad it's working fine , but when I try to run It with firefox or calc.exe its opening the program but not on my form - on the explorer.exe Maybe I am doing something wrong? or maybe there are another ways to do 'virtual desktop' as I want ?

thanx

  • Some program, such as firefox (and most browsers for that matter) will start multiple processes behind the scene when you fire them up and oneof the processes that it creates will be the window that you actually see. That's how they implement the ability to force all new frames to either appear in the one window or in another window. So for those kinds of processes you won't have the same level of control as they do their own work in trying to get things displayed the way they want to. – Jens Meinecke Jan 13 '16 at 11:02
  • There is another way to move the gui to the form? Maybe other language ? – Mher Tolpin Jan 13 '16 at 11:51

1 Answers1

0

I found a solution !!!! after you run your program, sleep the thread for +-3000 seconds , then run the code like as me :

private void LoadApplication(string path, IntPtr handle)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        int timeout = 10 * 1000;     // Timeout value (10s) in case we want to cancel the task if it's taking too long.
        Process p = Process.Start(path);
        Thread.Sleep(3000);
        Process[] proc = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(path));
        for(int j = 0 ;j<proc.Length;j++)
        {
            p = proc[j];
            //p.WaitForInputIdle();
            IntPtr Handle = new IntPtr();
            Handle = p.MainWindowHandle;
            if(Handle == IntPtr.Zero)
            {
                continue;
            }

            while (Handle == IntPtr.Zero)
            {
                System.Threading.Thread.Sleep(10);
                p.Refresh();

                if (sw.ElapsedMilliseconds > timeout)
                {
                    sw.Stop();
                    return;
                }
            }

            SetParent(Handle, handle);      // Set the process parent window to the window we want

        }

    }