• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            旅途

            如果想飛得高,就該把地平線忘掉

            在C#中用WM_COPYDATA消息來實(shí)現(xiàn)進(jìn)程間通信

            進(jìn)程之間通訊的幾種方法:
            在Windows程序中,各個進(jìn)程之間常常需要交換數(shù)據(jù),進(jìn)行數(shù)據(jù)通訊。常用的方法有
            使用內(nèi)存映射文件
            通過共享內(nèi)存DLL共享內(nèi)存
            使用SendMessage向另一進(jìn)程發(fā)送WM_COPYDATA消息
            比起前兩種的復(fù)雜實(shí)現(xiàn)來,WM_COPYDATA消息無疑是一種經(jīng)濟(jì)實(shí)惠的一中方法.(ZT)

            WM_COPYDATA消息的主要目的是允許在進(jìn)程間傳遞只讀數(shù)據(jù)。Windows在通過WM_COPYDATA消息傳遞期間,不提供繼承同步方式。SDK文檔推薦用戶使用SendMessage函數(shù),接受方在數(shù)據(jù)拷貝完成前不返回,這樣發(fā)送方就不可能刪除和修改數(shù)據(jù):
            這個函數(shù)的原型及其要用到的結(jié)構(gòu)如下:
            SendMessage(hwnd,WM_COPYDATA,wParam,lParam);
            其中,WM_COPYDATA對應(yīng)的十六進(jìn)制數(shù)為0x004A
            wParam設(shè)置為包含數(shù)據(jù)的窗口的句柄。lParam指向一個COPYDATASTRUCT的結(jié)構(gòu):
            typedef struct tagCOPYDATASTRUCT{
            DWORD dwData;//用戶定義數(shù)據(jù)
            DWORD cbData;//數(shù)據(jù)大小
            PVOID lpData;//指向數(shù)據(jù)的指針
            }COPYDATASTRUCT;
            該結(jié)構(gòu)用來定義用戶數(shù)據(jù)。
            具體過程如下:

            首先,在發(fā)送方,用FindWindow找到接受方的句柄,然后向接受方發(fā)送WM_COPYDATA消息.
            接受方在DefWndProc事件中,來處理這條消息.由于中文編碼是兩個字節(jié),所以傳遞中文時候字節(jié)長度要搞清楚.
            體代碼如下:
            //---------------------------------------------------
            //發(fā)送方:
            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 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 = "發(fā)送方窗體";
            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;
            }

            }

            //---------------------------------------------------
            //接受方
            //---------------------------------------------------
            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 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;
            }
            }



            posted on 2007-07-25 00:30 旅途 閱讀(2198) 評論(0)  編輯 收藏 引用 所屬分類: 深入windows

            久久国产精品99精品国产987| 国产精品亚洲综合久久| 国产情侣久久久久aⅴ免费| 97热久久免费频精品99| 久久99精品久久久久久噜噜| 日本精品久久久久影院日本| 伊人色综合久久天天人手人婷| 国产情侣久久久久aⅴ免费| 亚洲欧美国产日韩综合久久| 国产亚洲色婷婷久久99精品| 久久久久亚洲AV综合波多野结衣| 亚洲精品乱码久久久久久按摩 | www.久久热| 久久综合九色综合欧美就去吻| 婷婷五月深深久久精品| 久久亚洲天堂| 久久久久免费精品国产| 伊人久久大香线蕉综合Av| 国产精品成人无码久久久久久| 日韩AV无码久久一区二区| 久久天天躁狠狠躁夜夜2020 | 久久婷婷五月综合成人D啪 | 久久久久无码专区亚洲av| 亚洲精品无码久久久久久| 欧美麻豆久久久久久中文| 久久综合久久综合久久| 精品国产青草久久久久福利| 99久久综合国产精品免费| 99久久无色码中文字幕人妻| 激情五月综合综合久久69| 久久成人永久免费播放| 久久国产乱子精品免费女| 久久夜色精品国产网站| 久久婷婷国产剧情内射白浆| 久久这里只精品99re66| 亚洲精品无码久久毛片| 欧美久久综合九色综合| 色狠狠久久综合网| 久久99九九国产免费看小说| 久久亚洲日韩看片无码| 久久精品国产男包|