一个是WebForm,一个是WinForm,我想让他们进行相互通信,能实现吗?通信的目的有两点:
1、激活对方程序中的事件。
2、主动向对方传递字符串,以备用。
3、获取对方程序中公开的字符串。
本人对程序间通信不是很熟,还请各位高手提供一下源代码。

解决方案 »

  1.   

    weebservices,remoting,xml,三种方法都能实现。
      

  2.   

    weebservices,socket,xml,remoting四种方法都能实现。
      

  3.   

    1、激活对方程序中的事件。
    如何通过xml来激活对方程序的事件呢?
      

  4.   

    //--------------------------------------------------- //接受方 //--------------------------------------------------- using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Runtime.InteropServices;namespace WindowsFormGetMsg
    {    public partial class Form1 : System.Windows.Forms.Form
        {        private System.Windows.Forms.TextBox textBox1;        private System.ComponentModel.Container components = null;        const int WM_COPYDATA = 0x004A;        public Form1()
            {            InitializeComponent();        }        protected override void Dispose(bool disposing)
            {            if (disposing)
                {                if (components != null)
                    {                    components.Dispose();                }            }            base.Dispose(disposing);        }        private void InitializeComponent()
            {            this.textBox1 = new System.Windows.Forms.TextBox();            this.SuspendLayout();            //             // textBox1             //             this.textBox1.Location = new System.Drawing.Point(176, 32);            this.textBox1.Name = "textBox1";            this.textBox1.Size = new System.Drawing.Size(160, 21);            this.textBox1.TabIndex = 0;            this.textBox1.Text = "textBox1";            //             // Form1             //             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);            this.ClientSize = new System.Drawing.Size(432, 266);            this.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox1});            this.Name = "Form1";            this.Text = "接收方窗体";            this.ResumeLayout(false);        }        static void Main()
            {            Application.Run(new Form1());        }        protected override void DefWndProc(ref System.Windows.Forms.Message m)
            {            switch (m.Msg)
                {                case WM_COPYDATA:                    COPYDATASTRUCT mystr = new COPYDATASTRUCT();                    Type mytype = mystr.GetType();                    mystr = (COPYDATASTRUCT)m.GetLParam(mytype);                    this.textBox1.Text = mystr.lpData;                    break;                default:                    base.DefWndProc(ref m);                    break;            }        }    }    public struct COPYDATASTRUCT
        {        public IntPtr dwData;        public int cbData;        [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;    }}
      

  5.   

    using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Runtime.InteropServices;namespace WinFormSendMsg
    {    public partial class Form1 : System.Windows.Forms.Form
        {        private System.Windows.Forms.TextBox textBox1;        private System.Windows.Forms.Button button1;        private System.ComponentModel.Container components = null;        const int WM_COPYDATA = 0x004A;        public Form1()
            {            InitializeComponent();        }        protected override void Dispose(bool disposing)
            {            if (disposing)
                {                if (components != null)
                    {                    components.Dispose();                }            }            base.Dispose(disposing);        }        private void InitializeComponent()
            {            this.textBox1 = new System.Windows.Forms.TextBox();            this.button1 = new System.Windows.Forms.Button();            this.SuspendLayout();            //             // textBox1             //             this.textBox1.Location = new System.Drawing.Point(184, 24);            this.textBox1.Name = "textBox1";            this.textBox1.Size = new System.Drawing.Size(128, 21);            this.textBox1.TabIndex = 0;            this.textBox1.Text = "textBox1";            //             // button1             //             this.button1.Location = new System.Drawing.Point(344, 16);            this.button1.Name = "button1";            this.button1.Size = new System.Drawing.Size(112, 32);            this.button1.TabIndex = 1;            this.button1.Text = "button1";            this.button1.Click += new System.EventHandler(this.button1_Click);            //             // Form1             //             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);            this.ClientSize = new System.Drawing.Size(536, 142);            this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1, this.textBox1});            this.Name = "Form1";            this.Text = "发送方窗体";            this.ResumeLayout(false);        }        static void Main()
            {            Application.Run(new Form1());        }        [DllImport("User32.dll", EntryPoint = "SendMessage")]        private static extern int SendMessage(        int hWnd, // handle to destination window         int Msg, // message         int wParam, // first message parameter         ref COPYDATASTRUCT lParam // second message parameter         );        [DllImport("User32.dll", EntryPoint = "FindWindow")]        private static extern int FindWindow(string lpClassName, string        lpWindowName);        private void button1_Click(object sender, System.EventArgs e)
            {            int WINDOW_HANDLER = FindWindow(null, @"接收方窗体");            if (WINDOW_HANDLER == 0)
                {            }            else
                {                byte[] sarr = System.Text.Encoding.Default.GetBytes(this.textBox1.Text);
                    int len = sarr.Length;                COPYDATASTRUCT cds;                cds.dwData = (IntPtr)100;                cds.lpData = this.textBox1.Text;                cds.cbData = len + 1;                SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);            }        }    }    public struct COPYDATASTRUCT
        {        public IntPtr dwData;        public int cbData;        [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;    }}
      

  6.   

    以上使用WM_COPYDATA实现进程间通迅
      

  7.   

    好容易 使用API的SendMessage
      

  8.   

    我看 看错了 有web程序 只好webservice了
      

  9.   

    我是楼主,补充说明:还是没有解决,那种最简单的方法使asp.net与普通程序之间通讯呢?都是在本地运行的。希望能给原代码,谢谢。
    或者能不能用以下过程写个代码参考下?1、asp.net传一个数字给这个进程A;
    2、进程A将收到的数字乘于999。
    3、进程A将结果返回asp.net。
    4、asp.net将结果显示在屏幕。
      

  10.   

    用Remoting是最简单的实现!如果是在本机通讯的话,建议你使用IPC通道。它比TCP通道性能高很多!
      

  11.   

    观注
    但我觉得WEBserver的方法应最好,可是我不会,请高手给点说明!