Click here to Skip to main content
15,895,839 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Can anybody help me in running an exe in the same window ?
Posted
Comments
Sergey Alexandrovich Kryukov 6-Apr-11 1:38am    
Is that exe a console application (then it's trivial) or GUI?
--SA
vaishnavirs 6-Apr-11 1:49am    
I have created a WPF application and I am trying to run wordweb.exe in the same window. This Wordweb is an external application that is installed in my PC and I am passing the filepathinfo through process.start in my WPF application. I got it running but in a new window. Can I get the same displayed inside a panel within the same window ? Thanks alot in advance!
vaishnavirs 6-Apr-11 2:01am    
Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Can I please get the above code in C#? Thank you v much!
Sergey Alexandrovich Kryukov 6-Apr-11 2:04am    
It won't help much. You can try, but your panel is not a window, because you're using WPF.
What is Wordweb? Not a .NET application?
--SA
RaviRanjanKr 6-Apr-11 1:44am    
Please be clear while asking question.

Not a very good idea… who knows what that exe is doing? In general case, this is impossible, in others — very difficult.

There are some special cases when it is quite possible. One such case is console application, when you simply need to get its output in your WPF UI. In this case, you run the child application using System.Diagnostics.Process.Run and redirect two streams: stdout and stderr. Please see my recent Answer for further information: How to read from command prompt[^]. This is a relatively simple task and will always work.

There is another remote opportunity: the exe is a .NET assembly. There can be a chance to use it as a regular .NET class library and run some code from this assembly from the host application. For .NET, the difference between EXE and DLL files is not significant; any EXE file can be referenced as a library. However, if the application to be used as a library is written cleanly, it won't export many types as public. In this case, all types and their members can be accessed through Reflection; and the EXE file should be loaded during run-time using System.Reflection.Assembly.LoadFile. A very deep knowledge of the structure of the application to be used will be needed to do this trick. If the source code of this application is available, there no point to do any tricks, if not — it may require a lot of research and experiments. Do you want to waste so much time without any guaranteed result?

All other possibilities seem to be much less realistic, even compared with the previous one; I would not discuss them.

[EDIT]
Some detail on suggestion by Prerak and Abhinav. This is what I called "very difficult". You can change the parent of the main window of the second (child) process. First problem: you cannot do it immediately after the process is started: the main window is not created at this moment. You need to trigger this surgery by something, maybe even timer, which would make the process highly unreliable (how knows how much time is enough? it depends on the system). Second problem is: the child application cannot guarantee correct operation after being re-parented. Next problem: you need some existing windows with a window handle to become a parent. You're using WPF: all the controls are not windows. You can use only top-level window (sorry, I'm not 100% sure; never tried) or you need to host a HWND-based control of System.Windows.Forms inside your WPF UI, which is a whole separate story (see http://msdn.microsoft.com/en-us/library/ms751761.aspx[^]).
If you do it all, your useful effect per effort is still miserable: all you gained is practically just coordination of window/application activation (for example, via ALT+Tab) with the host application.
Do you want to do it all for such effect? Very little compared to what you already have, I would say…

—SA
 
Share this answer
 
v4
Comments
Abhinav S 6-Apr-11 4:53am    
Great answer. 5.
Sergey Alexandrovich Kryukov 6-Apr-11 16:20pm    
Thank you very much, Abhinav.
--SA
Espen Harlinn 6-Apr-11 16:46pm    
My 5!!
Sergey Alexandrovich Kryukov 6-Apr-11 17:03pm    
Thank you, Espen,
--SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Apr-11 2:02am    
Abhinav, this is for Forms. How about WPF? See my comments to the Answer by Prerak. Any my Answer.
--SA
Sergey Alexandrovich Kryukov 6-Apr-11 2:09am    
Prerak removed the Answer. You see, you need a window to become a parent. You probably can use a top-level window or a Form's control hosted by WPF. And the only effect is running inside the host's window. (Practically, the only useful effect would be window/application activation coordination with the host application, nothing else.)
--SA
Sergey Alexandrovich Kryukov 6-Apr-11 2:22am    
I added more detail on this approach in my Answer (after [EDIT]) I don't think it worth the effort; it's up to OP to decide...
--SA
Abhinav S 6-Apr-11 4:54am    
The OP could probably use the same concept and try migrating this code to WPF.
Sergey Alexandrovich Kryukov 6-Apr-11 16:24pm    
The situation is just the opposite. As I say, migration to WPF would only add some problems. The approach you refer to is better applied with Forms, but even with Forms I'm not sure it can guarantee good result.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900