C#中调用exe文件示例

C#中调用exe文件示例

0、功能描述

        在C#中调用另一个exe文件;参数通过面板上的控件进行设置输出。面板如下,其中,"加载文件"用于选择要调用的exe文件;“加载图片”、“OdrWidth”、“OdrHeight”用于设置被调用的exe的三个传输参数。
在这里插入图片描述
        

1、代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace EpsonWarpAdjust
{
    public partial class Form1 : Form
    {
        OpenFileDialog Fp_Picture = new OpenFileDialog();
        OpenFileDialog Fp_ExeObj = new OpenFileDialog();

        public Form1()
        {
            InitializeComponent();
        }

        private void SelectPictureObj_Click(object sender, EventArgs e)       /*“浏览”的点击事件,用于选择作为参数的图片*/
        {
            Fp_Picture.Filter = "(jpg)|*.jpg";

            if (Fp_Picture.ShowDialog() == DialogResult.OK)
            {
                textOfPicturePath.Text = Fp_Picture.FileName.ToString();     /*在文件选择框内显示文件路径*/
            }
        }

        private void SelectExeObj_Click(object sender, EventArgs e)			/*“浏览”的点击事件,用于选择exe文件*/
        {
            Fp_ExeObj.Filter = "(exe)|*.exe";

            if (Fp_ExeObj.ShowDialog() == DialogResult.OK)
            {
                textOfExePath.Text = Fp_ExeObj.FileName.ToString();         /*在文件选择框内显示文件路径*/
            }
        }


        private void StartButton_Click(object sender, EventArgs e)			/*“开始”的点击事件,用于调用指定的exe文件*/
        {
            string[] arg = new string[3];
            arg[0] = Fp_Picture.FileName;                                   /*被调用的exe的参数*/   
            arg[1] = OdrWidth.Text;
            arg[2] = OdrHeight.Text;

            StartProcess(textOfExePath.Text, arg);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textOfExePath.Text = System.Environment.CurrentDirectory + "\\offsets.exe";   /*初始值设为当前路径下的offset.exe*/
        }

        public bool StartProcess(string filename, string[] args)
        {
            try
            {
                string output;
                string parameters = "";

                int Index = args[0].LastIndexOf("\\");             /*获取路径名结束下标(不包含文件名)*/
                string PathOfBin = args[0].Substring(0, Index);    /*获取路径名*/
                txtReceive.Text = "文件生成路径:\n" + PathOfBin;

                foreach (string arg in args)
                {
                    parameters = parameters + '"' + arg + '"' + " ";/*将每个参数加上双引号,防止参数中带有空格带来的错误*/
                }
                parameters = parameters.Trim();

                Process myprocess = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo(filename,parameters);
                myprocess.StartInfo = startInfo;
                myprocess.StartInfo.UseShellExecute = false;        /*要重定向IO流,Process对象必须将UseShellExecute属性设置为false*/
                myprocess.StartInfo.RedirectStandardInput = true;   /*标准输入流的重定向,重定向至Process*/
                myprocess.StartInfo.RedirectStandardOutput = true;  /*标准输出流的重定向,*/
                myprocess.StartInfo.RedirectStandardError = true;
                myprocess.Start();
                myprocess.WaitForExit();
                output = myprocess.StandardOutput.ReadToEnd();
                
                if (output.Contains("SUCCESS"))                     /*判断exe的调用是否成功("SUCCESS"是被调用的exe的标准流输出(如"printf()中的内容"))*/
                {
                    /*弹出提示选择框;若用户选择是的话,进行路径跳转*/
                    DialogResult result = MessageBox.Show("转换完成,是否跳转到文件生成路径", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                    if (result == DialogResult.OK)
                    {
                        Process.Start(PathOfBin);
                    }
                    else
                    {

                    }
                }
                else 
                {
                    MessageBox.Show("转换未完成\n"+"请检查OdrWidth与OdrHeigth的参数", "错误提示");
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("启动应用程序时出错!原因:" + ex.Message);
            }
            return false;
        }
    }
}

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Runtime.InteropServices; namespace exe_test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Process process = null; IntPtr appWin; private string exeName = ""; [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)] private static extern long GetWindowLong(IntPtr hwnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)] private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong); //private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); [DllImport("user32.dll", SetLastError = true)] private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags); [DllImport("user32.dll", SetLastError = true)] private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint); [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)] private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam); private const int SWP_NOOWNERZORDER = 0x200; private const int SWP_NOREDRAW = 0x8; private const int SWP_NOZORDER = 0x4; private const int SWP_SHOWWINDOW = 0x0040; private const int WS_EX_MDICHILD = 0x40; private const int SWP_FRAMECHANGED = 0x20; private const int SWP_NOACTIVATE = 0x10; private const int SWP_ASYNCWINDOWPOS = 0x4000; private const int SWP_NOMOVE = 0x2; private const int SWP_NOSIZE = 0x1; private const int GWL_STYLE = (-16); private const int WS_VISIBLE = 0x10000000; private const int WM_CLOSE = 0x10; private const int WS_CHILD = 0x40000000; public string ExeName { get { return exeName; } set { exeName = value; } } private void button1_Click(object sender, EventArgs e) { this.exeName = @"calc.exe"; try { process = System.Diagnostics.Process.Start(this.exeName); process.WaitForInputIdle(); System.Threading.Thread.Sleep(150); appWin = process.MainWindowHandle; } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error"); } SetParent(appWin, this.splitContainer1.Panel2.Handle); MoveWindow(appWin, 0, 0, this.Width, this.Height, true); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值