[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to return embed process from my app to Desktop?

Posted on 2009-04-26
6
Medium Priority
?
509 Views
Last Modified: 2013-12-17
Hi,

I wrote a program that gets a list of all processes that have GUI and then I embed them in my program. So I have complete control over them. But I have a problem with returning them to desktop. Now if I close my program all embed processes are killed, but i want that they would appear on desktop as they were before I used them.

How can I do that?

I have already tried to save the original MainWindowHandle but when I tried to set the parent to that handle process still crashes. I have no idea what to do now.

 
0
Comment
Question by:rozmansi
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 24235940
Can you post an example on the method your using to embed it?  It may be that the process is owned by a thread that your app's main thread controls... if that is the case instead of it being started on a independant thread then that may be the start of your problem.
0
 

Author Comment

by:rozmansi
ID: 24236687
This is code that I use for embedding it.  

And I get processes with this line:
Process[] proc = Process.GetProcesses();


So process is not created in my program.
        [DllImport("user32.dll", SetLastError = true)]
        private static extern uint SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
 
        public static void LoadProcessInControl(Process _Process, IntPtr _Control)
        {
            //System.Diagnostics.Process p = System.Diagnostics.Process.Start(_Process);
            _Process.WaitForInputIdle();
 
            Native.SetParent(_Process.MainWindowHandle, _Control);
            ShowWindow(_Process, SW_SHOWMAXIMIZED);
        }

Open in new window

0
 
LVL 3

Expert Comment

by:Aleksei_Malkov
ID: 24236785
You are changing parent of the every process.
When your application closing in tries to close every child window.

To solve this problem - change parent of all the subchilds to desktop window before your application closed.

1. Store every window that was parented
2. OnClosing method in your _Control
3. In OnClosing set new parent to every window you've changed parent before.
4. New parent should be DesktopWindow (can be retrieved by GetDesktopWindow from WINAPI)
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:rozmansi
ID: 24237011
Ok for testing I changed that it should unload process when I press button.

But I still cant get this to work.
When I call my function the window still stays in my program and nothing happens.

And I find out that the process I embed is not the same before I use it and when I try to unload it. When I try to unload it the MainWindowHandle is set to 0 and title to "". So when I try to unload it i should find the new version of process with Process.GetProcesses(); OR should I use process that I have stored in Process variable?

Im confused. :D
        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);
 
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetDesktopWindow();
        
        public static void LoadProcessOnDesktop(Process _Process)
        {
            _Process.WaitForInputIdle();
 
            Native.SetParent(_Process.Handle , GetDC(GetDesktopWindow()));
        }

Open in new window

0
 
LVL 3

Accepted Solution

by:
Aleksei_Malkov earned 1000 total points
ID: 24237044
1. You should cache windows handles when you move them to your process.

2. In your call you shouldn't call GetDC. Device context doesn't relate to processes and windows

3. Draft of the code is below...

4. The only point left if somehow process (window) is closed when this process is inside your application it should be removed from the cached window list. Or when moving process out of the current application - check that window is existing
        List<IntPtr> storedHandled = new List<IntPtr>();
 
        public static void LoadProcessInControl(Process _Process, IntPtr _Control)
        {
            //System.Diagnostics.Process p = System.Diagnostics.Process.Start(_Process);
            _Process.WaitForInputIdle();
 
            storedHandles.Add(_Process.MainWindowHandle);
            Native.SetParent(_Process.MainWindowHandle, _Control);
            ShowWindow(_Process, SW_SHOWMAXIMIZED);
        }
 
 
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetDesktopWindow();
 
        public static void UnloadAllProcesses()
        {
            foreach (IntPtr handle in storedHandles)
            {
 
                Native.SetParent(handle, GetDesktopWindow());
            }
        }

Open in new window

0
 

Author Closing Comment

by:rozmansi
ID: 31574653
Ok I got it working. Tnx for help.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question