Chinaunix首页 | 论坛 | 博客
  • 博客访问: 164247
  • 博文数量: 67
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 22
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-17 17:44
个人简介

一入程序深似海,从此妹子成路人

文章分类

全部博文(67)

文章存档

2016年(13)

2015年(54)

我的朋友

分类: 嵌入式

2015-10-15 15:52:44

    此进程间通信不是等同于Linux下的进程间的发送与接收消息的通信,关于C#下的进程间通信我们可以有两种方式:

    1.建立共同的区域(共享内存,文件,注册表,剪贴板等等)

    2.使用WIN API函数。①使用Process在A进程内部启动另外的可执行文件并且以新的进程运行,而传递参数既可以通过可执行文件main函数的命令行参数,亦可以获取主窗口的句柄而后通过API函数发送消息;②利用API函数找到进程窗口的句柄,然后用API去控制这个窗口。例如:导入“User32.dll”中的FindWindow、FindWindowEx函数查找窗口,并获取窗口句柄,然后进行消息传送。

    本文主要介绍第二种方式,第一种方式相对比较复杂,而且效率不高。

    一、需要的头文件

    进程的启动需要System.Diagnostics;

    API函数的使用需要System.Runtime.InteropServices;此外需要在使用API函数的类中对所应用的函数进行声明:

点击(此处)折叠或打开

  1. [DllImport("User32.dll", EntryPoint = "SendMessage")]
  2.         private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

  3.         [DllImport("User32.dll", EntryPoint = "FindWindow")]
  4.         private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

  5.         [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
  6.         private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

二、实例源码

1.简单的传递参数进程启动

介绍4种方法:

①启动外部程序,不等待其退出

②启动外部程序,等待其退出

③启动外部程序,无限等待其退出

④启动外部程序,通过事件监视其退出

=================================================

①启动外部程序,不等待其退出

点击(此处)折叠或打开

  1.                 Process pro = new Process();
                    pro.StartInfo.FileName = @"F:\Projects\QueryTest\QueryTest\bin\Debug\QueryTest.exe";
  2.               pro.StartInfo.Arguments = string.Format("{0} {1} {2} {3}", XX, XX, XX, XX);
  3.                 //Console.WriteLine("外部进程开始启动。。");

  4.                 Console.WriteLine("QueryTest开始启动。。");
  5.                 pro.Start();

②启动外部程序,等待其退出

点击(此处)折叠或打开

  1. Process pro = new Process();
  2.                 pro.StartInfo.FileName = @"F:\Projects\QueryTest\QueryTest\bin\Debug\QueryTest.exe";

  3.               pro.StartInfo.Arguments = string.Format("{0} {1} {2} {3}", XX, XX, XX, XX);

  4.                 //Console.WriteLine("外部进程开始启动。。");

  5.                 Console.WriteLine("QueryTest开始启动。。");

  6.                 pro.Start();
  7.   if (proc != null)
  8.         {
  9.             proc.WaitForExit(3000);
  10.             if (proc.Ha***ited)
  11.                 Console.WriteLine("XXXXX");
  12.             else
  13.             {
  14.                 // 如果外部程序没有结束运行则强行终止之。

  15.                 proc.Kill();
  16. Console.WriteLine("XXXXX1");

  17.                             }
  18.         }

③启动外部程序,无限等待其退出 //有时候看不见第一个进程的反应,可能是忘记了添加Console.Read()或者是其他的是控制台等待的方法。

点击(此处)折叠或打开

  1. static void Main(string[] args)
  2.         {
  3.             //for (int i = 0; i < 5; i++)

  4.             //{

  5.                 //string usrName = "usr" + i;

  6.                 //string cipher = usrName;

  7.                 string srvIP = "10.25.18.68";
  8.                 string srvPort = "2030";
  9.                 Process pro = new Process();
  10.                 //pro.StartInfo.FileName = "F:\\EC\\enterpriseInternalCommunication0406-0410\\enterpriseInternalCommunication\\bin\\Debug\\enterpriseInternalCommunication.exe";

  11.                 pro.StartInfo.FileName = @"F:\Projects\QueryTest\QueryTest\bin\Debug\QueryTest.exe";
  12.                 //pro.StartInfo.Arguments = string.Format("{0} {1} {2} {3}", usrName, cipher, srvIP, srvPort);

  13.                 //Console.WriteLine("进程{0}开始启动。。", i);

  14.                 Console.WriteLine("QueryTest开始启动。。");
  15.                 pro.Start();
  16.                 if (pro != null)
  17.                 {
  18.                      pro.WaitForExit();
  19.                 }

④启动外部程序,通过事件监视其退出

点击(此处)折叠或打开

  1. static void Main(string[] args)
  2.         {
  3.             //for (int i = 0; i < 5; i++)

  4.             //{

  5.                 //string usrName = "usr" + i;

  6.                 //string cipher = usrName;

  7.                 string srvIP = "10.25.18.68";
  8.                 string srvPort = "2030";
  9.                 Process pro = new Process();
  10.                 //pro.StartInfo.FileName = "F:\\EC\\enterpriseInternalCommunication0406-0410\\enterpriseInternalCommunication\\bin\\Debug\\enterpriseInternalCommunication.exe";

  11.                 pro.StartInfo.FileName = @"F:\Projects\QueryTest\QueryTest\bin\Debug\QueryTest.exe";
  12.                 //pro.StartInfo.Arguments = string.Format("{0} {1} {2} {3}", usrName, cipher, srvIP, srvPort);

  13.                 //Console.WriteLine("进程{0}开始启动。。", i);

  14.                 Console.WriteLine("QueryTest开始启动。。");
  15.                 pro.Start();
  16.                 if (pro != null)
  17.                 {
  18.                     //监视进程退出

  19.                     pro.EnableRaisingEvents = true;
  20.                     //指定退出事件方法

  21.                     pro.Exited += new EventHandler(pro_Exited);
  22.                  }
  23.                 Console.Read();
  24.         }

  25.         static void pro_Exited(object sender, EventArgs e)
  26.         {
  27.             Console.WriteLine("启动的外部程序退出");
  28.         }

2.API函数传递参数

未完待续。。。。。





 

阅读(1514) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~