Process.start在同一窗口中 [英] Process.start in the same window

查看:132
本文介绍了Process.start在同一窗口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我在同一窗口中运行exe吗?

Can anybody help me in running an exe in the same window ?

推荐答案

不是一个好主意……谁知道exe在做什么?在一般情况下,这是不可能的,而在其他情况下则非常困难.

在某些特殊情况下,这很有可能.控制台应用程序就是这种情况之一,您只需要在WPF UI中获取其输出即可.在这种情况下,您可以使用System.Diagnostics.Process.Run运行子应用程序,并重定向两个流:stdoutstderr.请参阅我最近的答案以获取更多信息:如何从命令提示符中读取 [ ^ ].这是一个相对简单的任务,将始终有效.

还有另一个机会:exe是.NET程序集.可以将其用作常规.NET类库,并从主机应用程序中通过该程序集运行一些代码.对于.NET,EXE文件和DLL文件之间的差异并不明显.任何EXE文件都可以引用为库.但是,如果要干净地编写要用作库的应用程序,则不会将许多类型导出为公共类型.在这种情况下,可以通过反射访问所有类型及其成员.并且应该在运行时使用System.Reflection.Assembly.LoadFile加载EXE文件.要完成此技巧,需要对要使用的应用程序的结构有非常深入的了解.如果此应用程序的源代码可用,那么就没有任何办法可做,即使没有,也可能需要大量的研究和实验.您是否想浪费这么多时间而没有任何保证的结果?

即使与前一个相比,所有其他可能性似乎也不太现实.我不会讨论它们.


关于Prera​​k和Abhinav的建议的一些细节.这就是我所说的非常困难".您可以更改第二个(子)过程的主窗口的父窗口.第一个问题:在启动过程后您不能立即执行此操作:此时未创建主窗口.您需要通过某种方法(甚至是计时器)来触发该手术,这将使该过程高度不可靠(如何知道多少时间足够?它取决于系统).第二个问题是:子应用程序在重新建立父级后不能保证正确的操作.下一个问题:您需要一些具有窗口句柄的现有窗口才能成为父窗口.您正在使用WPF:所有控件都不是Windows.您只能使用顶层窗口(对不起,我不是100%确信;从未尝试过),或者您需要在WPF UI中托管基于HWND的System.Windows.Forms控件,这是一个完整的故事(请参阅 http://msdn.microsoft.com/en-us/library/ms751761.aspx [ ^ ]).
如果全部完成,那么您付出的努力仍然是痛苦的:您获得的实际上只是协调窗口/应用程序激活(例如,通过ALT + Tab)与宿主应用程序.
您是否想做到所有这些效果?我要说的是,与您已经拥有的相比,几乎没有什么……

-SA
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.


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


http: //geekswithblogs.net/gyoung/archive/2006/04/26/76521.aspx [
http://geekswithblogs.net/gyoung/archive/2006/04/26/76521.aspx[^] could help.


这篇关于Process.start在同一窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆