Communication and control between. Net programs

Source: Internet
Author: User

If there is A requirement, use one program to control another program, which is the simplest. For example, use program a to open Program B, which must be used at ordinary times. You can use the Process class and related methods. Then, when B is enabled, some parameters are sent, and B then reflects these parameters. How can this problem be achieved? In fact, Process is still used. Sender: copy the code static void Main (string [] args) {Console. writeLine ("Enter the receiver path:"); string path = Console. readLine (); Console. writeLine ("Enter the receiver startup parameter:"); string para = Console. readLine (); ProcessStartInfo pi = new ProcessStartInfo (); pi. fileName = @ path; pi. arguments = para; try {Process. start (pi);} catch {Console. writeLine ("the receiver cannot be found or an error occurs! ");} Console. readKey ();} copy the code receiver: copy the code static void Main (string [] args) {if (args. length = 0) {Console. writeLine ("message not received! ");} Else {foreach (string s in args) {Console. writeLine (s) ;}} Console. readKey () ;}copy the code so that we can use program A To Start Program B and process program B according to the parameters passed in by program. However, in WPF, The args parameter in Main cannot be used directly. For WPF, you can use the following method: 1. Delete StartupUri in App. xaml and add the Startup copy Code <Application x: Class = "WpfApplication65.App" xmlns =" http://schemas.microsoft.com/winfx/2006/xaml /Presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "Startup =" Application_Startup "> <Application. resources> </Application. resources> </Application> copy Code 2. in the App. xaml. copy the private void Application_Startup (object sender, StartupEventArgs e) {MainWindow mw = new MainWindow (); foreach (string s in e. args) {mw.txt Show. text + = s;} mw. show ();} copy the code to obtain the passed parameters. However, to change the requirement, we should not only pass in parameters when the program starts, but also input parameters to a started program, so we need to use the process to communicate with IPC. IPC uses Windows APIs. The following describes how WPF implements inter-process communication. 1. create a new class library for the struct Class Library of New data. The content of the class library is as follows: copy the code using System; using System. collections. generic; using System. linq; using System. runtime. interopServices; using System. text; using System. threading. tasks; namespace DataStruct {[StructLayout (LayoutKind. sequential)] public struct DataStruct {public IntPtr dwData; public int cbData; // String Length [financialas (UnmanagedType. LPStr)] public string lpData; // string} copy Code 2. new information help class library new A class library references the structure class library in the previous step. The content of the class library is as follows: copy the code using System; using System. collections. generic; using System. linq; using System. runtime. interopServices; using System. text; using System. threading. tasks; namespace MessageHelper {public class MessageHelper {public const int WM_DOWNLOAD_COMPLETED = 0x00AA; public const int WM_COPYDATA = 0x004A; [DllImport ("User32.dll", EntryPoint = "FindWindow"] public static extern IntPtr FindWindow (string lpClassName, string lpWindowName); [DllImport ("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage (IntPtr wnd, int msg, int wP, ref DataStruct. dataStruct cds) ;}} copy code 3. the sender first references the first two class libraries for sending in two ways:. copy the code var lstProcess = Process by Process name. getProcessesByName (txtProcess. text); if (lstProcess. length> 0) {Process proc = lstProcess [0]; DataStruct. dataStruct cds; Cds. dwData = IntPtr. zero; cds. lpData = txtMSG. text; cds. cbData = System. text. encoding. default. getBytes (txtMSG. text ). length + 1; int from=whandler = 0; MessageHelper. messageHelper. sendMessage (proc. main1_whandle, MessageHelper. messageHelper. WM_COPYDATA, from?whandler, ref cds);} copy the code. Note: If ShowInTaskbar of the form is false, that is, it is not displayed on the taskbar, there is no way to obtain the window through main1_whandle. B. Copy the code IntPtr hwnd = MessageHelper. MessageHelper. FindWindow (null, txtTitle. Text); if (hwnd! = IntPtr. zero) {DataStruct. dataStruct cds; cds. dwData = IntPtr. zero; cds. lpData = txtMSG. text; cds. cbData = System. text. encoding. default. getBytes (txtMSG. text ). length + 1; // message source form int from?whandler = 0; MessageHelper. messageHelper. sendMessage (hwnd, MessageHelper. messageHelper. WM_COPYDATA, from?whandler, ref cds);} copy the code. Note: using this method, if the Title of multiple windows is the same, conflicts may occur. 4. the receiving end first references the class libraries in the previous two steps. Copy the public MainWindow () {InitializeComponent (); Loaded + = new RoutedEventHandler (Window_Loaded);} private void Window_Loaded (object sender, RoutedEventArgs e) {(PresentationSource. fromVisual (this) as HwndSource ). addHook (new HwndSourceHook (this. wndProc);} IntPtr WndProc (IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {if (msg = MessageHelper. messageHelper. WM_COPYDATA) {DataStruct. dataStruct cds = (DataStruct. dataStruct) System. runtime. interopServices. marshal. ptrToStructure (lParam, typeof (DataStruct. dataStruct); txtShow. text = cds. lpData;} return hwnd ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.