积极答复者
如何在一个程序中调用另一个程序

问题
答案
-
调用另一个进程并隐藏该进程的窗体是可以做到的,但“执行当前按钮操作,目的进程也需要被操作”有些难度
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true; //不显示另一个程序的窗口
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;
// Call WaitForExit and then the using-statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
若是.NET程序,考虑用反射技术直接调用目的程序中的方法。
若是非.NET程序,目的程序要公布SDK接口才可以这样做,比如Outlook二次开发
或是hook,code inject技术,将代码注入到目的进程中,参考下面
http://www.codeproject.com/Articles/5038/Using-Hooks-from-C
http://www.codeproject.com/Articles/20565/Assembly-Manipulation-and-C-VB-NET-Code-Injection
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
- 已建议为答案 Herro wongMicrosoft contingent staff, Moderator 2016年6月6日 3:00
- 已标记为答案 Herro wongMicrosoft contingent staff, Moderator 2016年6月8日 5:07
全部回复
-
Hi,
你可以用Win32 APi中的'SendMessage','FindWindows','FindWindowsEx'来实现你的需求(不过需要先捕获到对应按钮的句柄)//消息常量
private const int WM_LBUTTONUP = 0x202;
private const int WM_LBUTTONDOWN 0x201 ;//寻找窗口句柄 [DllImport("User32.dll", EntryPoint = "FindWindow")] public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); //寻找子窗口句柄(控件句柄) [DllImport("User32.dll", EntryPoint = "FindWindowEx")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName); //发送Message [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
Regards,
Moonlight
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- 已编辑 Moonlight ShengMicrosoft contingent staff 2016年5月30日 5:53
-
调用另一个进程并隐藏该进程的窗体是可以做到的,但“执行当前按钮操作,目的进程也需要被操作”有些难度
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true; //不显示另一个程序的窗口
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;
// Call WaitForExit and then the using-statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
若是.NET程序,考虑用反射技术直接调用目的程序中的方法。
若是非.NET程序,目的程序要公布SDK接口才可以这样做,比如Outlook二次开发
或是hook,code inject技术,将代码注入到目的进程中,参考下面
http://www.codeproject.com/Articles/5038/Using-Hooks-from-C
http://www.codeproject.com/Articles/20565/Assembly-Manipulation-and-C-VB-NET-Code-Injection
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
- 已建议为答案 Herro wongMicrosoft contingent staff, Moderator 2016年6月6日 3:00
- 已标记为答案 Herro wongMicrosoft contingent staff, Moderator 2016年6月8日 5:07