如果用sendmessage发送来结构体消息,我如何接受,copymemory在c#中如何声明,谢谢大家的帮助,搞了几天搞不明白

解决方案 »

  1.   

    http://dev.csdn.net/article/15/15598.shtm
      

  2.   

    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.ComponentModel;namespace TestSendMessage
    {
    /// <summary>
    /// Win32APIを呼び出すためのクラス
    /// </summary>
    public class Win32API
    {
    /// <summary>
    /// リストビューのコラム
    /// </summary>
    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct LV_COLUMN
    {
    /// <summary>
    /// マスク
    /// </summary>
    public UInt32 mask; /// <summary>
    /// 
    /// </summary>
    public Int32 fmt; /// <summary>
    /// 
    /// </summary>
    public Int32 cx; /// <summary>
    /// 
    /// </summary>
    public String pszText; /// <summary>
    /// 
    /// </summary>
    public Int32 cchTextMax; /// <summary>
    /// サブアイテム
    /// </summary>
    public Int32 iSubItem; /// <summary>
    /// イメージ
    /// </summary>
    public Int32 iImage; /// <summary>
    /// 並び
    /// </summary>
    public Int32 iOrder;
    } /// <summary>
    /// メッセージを発信する
    /// </summary>
    /// <param name="hWnd">リストビューのハンドル</param>
    /// <param name="msg">メッセージ</param>
    /// <param name="wParam">パラメータ</param>
    /// <param name="lParam">パラメータ</param>
    /// <returns>メッセージを発信</returns>
    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, ref
    LV_COLUMN lParam); /// <summary>
    /// メッセージを発信する
    /// </summary>
    /// <param name="hwnd">リストビューのハンドル</param>
    /// <param name="wMsg">メッセージ</param>
    /// <param name="wParam">パラメータ</param>
    /// <param name="lParam">パラメータ</param>
    /// <returns>メッセージを発信</returns>
    [DllImport("user32.dll")]
    public static extern int SendMessage (IntPtr hwnd, int wMsg, int wParam,int lParam); /// <summary>
    /// 指定ウィンドウを取得する
    /// </summary>
    /// <param name="lpClassName"></param>
    /// <param name="lpWindowName"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary>
    /// 指定ウィンドウのVisible
    /// </summary>
    /// <param name="hWnd"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public extern static bool IsWindowVisible(IntPtr hWnd); /// <summary>
    /// 指定ウィンドウを手前に表示させる
    /// </summary>
    /// <param name="hWnd"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public extern static bool SetForegroundWindow(IntPtr hWnd); /// <summary>
    /// WM_COPYDATA
    /// </summary>
    public const int WM_COPYDATA = 0x004a; /// <summary>
    /// COPYDATASTRUCT構造体
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack=2)]
    public struct COPYDATASTRUCT 
    {
    /// <summary>
    /// 数値データ(任意の送信データ)
    /// </summary>
    public uint dwData;
    /// <summary>
    /// lpDataが指すデータのサイズ
    /// </summary>
    public uint  cbData;
    /// <summary>
    /// データを指すポインタ
    /// </summary>
    public IntPtr lpData;
    };
    }
    }
      

  3.   

    Form1:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;namespace TestSendMessage
    {
    /// <summary>
    /// Form1 の概要の説明です。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必要なデザイナ変数です。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    InitializeComponent();
    } /// <summary>
    /// 使用されているリソースに後処理を実行します。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows フォーム デザイナで生成されたコード 
    /// <summary>
    /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
    /// コード エディタで変更しないでください。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(24, 40);
    this.label1.Name = "label1";
    this.label1.TabIndex = 0;
    this.label1.Text = "Message";
    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // label2
    // 
    this.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.label2.Location = new System.Drawing.Point(142, 40);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(208, 23);
    this.label2.TabIndex = 1;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(142, 92);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(88, 23);
    this.button1.TabIndex = 2;
    this.button1.Text = "Show Form2";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
    this.ClientSize = new System.Drawing.Size(376, 137);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.label1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// アプリケーションのメイン エントリ ポイントです。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } protected override void WndProc(ref Message m)
    {
    switch(m.Msg)
    {
    case Win32API.WM_COPYDATA:
    Win32API.COPYDATASTRUCT cds = new Win32API.COPYDATASTRUCT();
    cds = (Win32API.COPYDATASTRUCT)Marshal.PtrToStructure(
    m.LParam, typeof(Win32API.COPYDATASTRUCT));
    string strData = Marshal.PtrToStringAnsi(cds.lpData);
    label2.Text = strData;
    break;
    } base.WndProc (ref m);
    } private void button1_Click(object sender, System.EventArgs e)
    {
    Form2 form = new Form2();
    form.Show();
    } }
    }
      

  4.   

    Form2:using System;
    using System.Text;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace TestSendMessage
    {
    /// <summary>
    /// Form2 の概要の説明です。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    /// <summary>
    /// 必要なデザイナ変数です。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form2()
    {
    //
    // Windows フォーム デザイナ サポートに必要です。
    //
    InitializeComponent(); //
    // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
    //
    } /// <summary>
    /// 使用されているリソースに後処理を実行します。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows フォーム デザイナで生成されたコード 
    /// <summary>
    /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
    /// コード エディタで変更しないでください。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(92, 60);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "Send";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(132, 20);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "";
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(20, 20);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(100, 16);
    this.label1.TabIndex = 2;
    this.label1.Text = "Message";
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
    this.ClientSize = new System.Drawing.Size(258, 99);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.button1);
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 指定ウィンドウを存在するがどうかの判断
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    protected bool IsExistSpecifiedForm(string text)
    {
    IntPtr hWnd = Win32API.FindWindow(null, text);
    if(hWnd == IntPtr.Zero)
    {
    return false;
    }
    return true;
    } /// <summary>
    /// 指定ウィンドウを手前に表示させる
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    protected bool ForegroundWindow(string text)
    {
    IntPtr hWnd = Win32API.FindWindow(null, text);
    if(hWnd == IntPtr.Zero)
    {
    return false;
    }
    if(Win32API.IsWindowVisible(hWnd) == false)
    {
    return false;
    } Win32API.SetForegroundWindow(hWnd);
    return true;
    } /// <summary>
    /// 指定ウィンドウにメッセージを送信する
    /// </summary>
    /// <param name="text"></param>
    /// <param name="num"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    protected bool SendCopyDataMessage(string text, uint num, string data)
    {
    IntPtr hWnd = Win32API.FindWindow(null, text);
    if(hWnd == IntPtr.Zero)
    {
    return false;
    } uint uLength = 0;
    if(data != null)
    {
    uLength = (uint)Encoding.Default.GetBytes(data).Length + 1;
    } IntPtr ptrData = Marshal.StringToHGlobalAnsi(data);
    try
    {
    Win32API.COPYDATASTRUCT cds = new Win32API.COPYDATASTRUCT();
    cds.dwData = num; // 数値データ(任意の送信データ)
    cds.cbData = uLength; // lpDataが指すデータのサイズ
    cds.lpData = ptrData; // データを指すポインタ IntPtr ptrCopyData = Marshal.AllocHGlobal(Marshal.SizeOf(cds));
    try
    {
    Marshal.StructureToPtr(cds, ptrCopyData, true);
    Win32API.SendMessage(hWnd, Win32API.WM_COPYDATA, (int)this.Handle, (int)ptrCopyData);
    }
    finally
    {
    Marshal.FreeHGlobal(ptrCopyData);
    }
    }
    finally
    {
    Marshal.FreeHGlobal(ptrData);
    } return true;
    } private void button1_Click(object sender, System.EventArgs e)
    {
    if (IsExistSpecifiedForm("Form1"))
    {
    SendCopyDataMessage("Form1", 0, textBox1.Text);
    }
    }
    }
    }
      

  5.   

    就是这个结构体阿~消息应该是在lpData这个指针指向的内存里,我是这么认为的hoho
    今天来了好多bug要改可能没时间给你详细解释咯。/// <summary>
    /// COPYDATASTRUCT構造体
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack=2)]
    public struct COPYDATASTRUCT 
    {
    /// <summary>
    /// 数値データ(任意の送信データ)
    /// </summary>
    public uint dwData;
    /// <summary>
    /// lpDataが指すデータのサイズ
    /// </summary>
    public uint  cbData;
    /// <summary>
    /// データを指すポインタ
    /// </summary>
    public IntPtr lpData;
    };
      

  6.   

    typedef struct data
    {
    int nMode
    char s1[100];
    unsigned int ID;
    char address[256];
    char Name[256]
    }
    例如,我在vc里的一个结构体,在c#里怎么声明这个结构体,和作为一个结构体的数据在两个进程间用WM_COPYDATA在两个进程间发送和接受,谢谢了,希望回答问题的哥们给我搞个例子好吗,谢谢,我的油箱[email protected]
      

  7.   

    我公司里面对163邮箱有屏蔽,代码就贴在这里吧,见谅.
    对于你的结构体char数组我改成string了,不知道是否符合要求.Win32API.cs:using System;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.ComponentModel;namespace TestSendMessage
    {
    /// <summary>
    /// Win32APIを呼び出すためのクラス
    /// </summary>
    public class Win32API
    {
    /// <summary>
    /// リストビューのコラム
    /// </summary>
    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct LV_COLUMN
    {
    /// <summary>
    /// マスク
    /// </summary>
    public UInt32 mask; /// <summary>
    /// 
    /// </summary>
    public Int32 fmt; /// <summary>
    /// 
    /// </summary>
    public Int32 cx; /// <summary>
    /// 
    /// </summary>
    public String pszText; /// <summary>
    /// 
    /// </summary>
    public Int32 cchTextMax; /// <summary>
    /// サブアイテム
    /// </summary>
    public Int32 iSubItem; /// <summary>
    /// イメージ
    /// </summary>
    public Int32 iImage; /// <summary>
    /// 並び
    /// </summary>
    public Int32 iOrder;
    } /// <summary>
    /// メッセージを発信する
    /// </summary>
    /// <param name="hWnd">リストビューのハンドル</param>
    /// <param name="msg">メッセージ</param>
    /// <param name="wParam">パラメータ</param>
    /// <param name="lParam">パラメータ</param>
    /// <returns>メッセージを発信</returns>
    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, ref
    LV_COLUMN lParam); /// <summary>
    /// メッセージを発信する
    /// </summary>
    /// <param name="hwnd">リストビューのハンドル</param>
    /// <param name="wMsg">メッセージ</param>
    /// <param name="wParam">パラメータ</param>
    /// <param name="lParam">パラメータ</param>
    /// <returns>メッセージを発信</returns>
    [DllImport("user32.dll")]
    public static extern int SendMessage (IntPtr hwnd, int wMsg, int wParam,int lParam); /// <summary>
    /// 指定ウィンドウを取得する
    /// </summary>
    /// <param name="lpClassName"></param>
    /// <param name="lpWindowName"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public extern static IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary>
    /// 指定ウィンドウのVisible
    /// </summary>
    /// <param name="hWnd"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public extern static bool IsWindowVisible(IntPtr hWnd); /// <summary>
    /// 指定ウィンドウを手前に表示させる
    /// </summary>
    /// <param name="hWnd"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    public extern static bool SetForegroundWindow(IntPtr hWnd); /// <summary>
    /// WM_COPYDATA
    /// </summary>
    public const int WM_COPYDATA = 0x004a; /// <summary>
    /// COPYDATASTRUCT構造体
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack=2)]
    public struct COPYDATASTRUCT 
    {
    /// <summary>
    /// 数値データ(任意の送信データ)
    /// </summary>
    public uint dwData;
    /// <summary>
    /// lpDataが指すデータのサイズ
    /// </summary>
    public uint  cbData;
    /// <summary>
    /// データを指すポインタ
    /// </summary>
    public IntPtr lpData;
    }; [StructLayout(LayoutKind.Sequential, Pack=2)]
    public struct data
    {
    public int nMode;
    public IntPtr s1;
    public uint ID;
    public IntPtr address;
    public IntPtr Name;
    }
    }
    }
      

  8.   

    Form1:using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;namespace TestSendMessage
    {
    /// <summary>
    /// Form1 の概要の説明です。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Label label7;
    private System.Windows.Forms.Label label10;
    private System.Windows.Forms.Label lbnMode;
    private System.Windows.Forms.Label lbs1;
    private System.Windows.Forms.Label lbID;
    private System.Windows.Forms.Label lbAddresss;
    private System.Windows.Forms.Label lbName;
    /// <summary>
    /// 必要なデザイナ変数です。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    InitializeComponent();
    } /// <summary>
    /// 使用されているリソースに後処理を実行します。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows フォーム デザイナで生成されたコード 
    /// <summary>
    /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
    /// コード エディタで変更しないでください。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.lbnMode = new System.Windows.Forms.Label();
    this.button1 = new System.Windows.Forms.Button();
    this.label3 = new System.Windows.Forms.Label();
    this.lbs1 = new System.Windows.Forms.Label();
    this.label5 = new System.Windows.Forms.Label();
    this.lbID = new System.Windows.Forms.Label();
    this.label7 = new System.Windows.Forms.Label();
    this.lbAddresss = new System.Windows.Forms.Label();
    this.lbName = new System.Windows.Forms.Label();
    this.label10 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(24, 14);
    this.label1.Name = "label1";
    this.label1.TabIndex = 0;
    this.label1.Text = "nMode";
    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // lbnMode
    // 
    this.lbnMode.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lbnMode.Location = new System.Drawing.Point(142, 14);
    this.lbnMode.Name = "lbnMode";
    this.lbnMode.Size = new System.Drawing.Size(208, 23);
    this.lbnMode.TabIndex = 1;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(130, 210);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(88, 23);
    this.button1.TabIndex = 2;
    this.button1.Text = "Show Form2";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // label3
    // 
    this.label3.Location = new System.Drawing.Point(24, 54);
    this.label3.Name = "label3";
    this.label3.TabIndex = 0;
    this.label3.Text = "s1";
    this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // lbs1
    // 
    this.lbs1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lbs1.Location = new System.Drawing.Point(142, 54);
    this.lbs1.Name = "lbs1";
    this.lbs1.Size = new System.Drawing.Size(208, 23);
    this.lbs1.TabIndex = 1;
    // 
    // label5
    // 
    this.label5.Location = new System.Drawing.Point(24, 94);
    this.label5.Name = "label5";
    this.label5.TabIndex = 0;
    this.label5.Text = "ID";
    this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // lbID
    // 
    this.lbID.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lbID.Location = new System.Drawing.Point(142, 94);
    this.lbID.Name = "lbID";
    this.lbID.Size = new System.Drawing.Size(208, 23);
    this.lbID.TabIndex = 1;
    // 
    // label7
    // 
    this.label7.Location = new System.Drawing.Point(24, 132);
    this.label7.Name = "label7";
    this.label7.TabIndex = 0;
    this.label7.Text = "address";
    this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // lbAddresss
    // 
    this.lbAddresss.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lbAddresss.Location = new System.Drawing.Point(142, 132);
    this.lbAddresss.Name = "lbAddresss";
    this.lbAddresss.Size = new System.Drawing.Size(208, 23);
    this.lbAddresss.TabIndex = 1;
    // 
    // lbName
    // 
    this.lbName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.lbName.Location = new System.Drawing.Point(142, 170);
    this.lbName.Name = "lbName";
    this.lbName.Size = new System.Drawing.Size(208, 23);
    this.lbName.TabIndex = 1;
    // 
    // label10
    // 
    this.label10.Location = new System.Drawing.Point(24, 168);
    this.label10.Name = "label10";
    this.label10.TabIndex = 0;
    this.label10.Text = "name";
    this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
    this.ClientSize = new System.Drawing.Size(376, 245);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.lbnMode);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.lbs1);
    this.Controls.Add(this.label5);
    this.Controls.Add(this.lbID);
    this.Controls.Add(this.label7);
    this.Controls.Add(this.lbAddresss);
    this.Controls.Add(this.lbName);
    this.Controls.Add(this.label10);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// アプリケーションのメイン エントリ ポイントです。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } protected override void WndProc(ref Message m)
    {
    switch(m.Msg)
    {
    case Win32API.WM_COPYDATA:
    Win32API.data getData = new TestSendMessage.Win32API.data();
    getData = (Win32API.data)Marshal.PtrToStructure(m.LParam, typeof(Win32API.data));
    string strName = Marshal.PtrToStringAnsi(getData.Name);
    string straddresss = Marshal.PtrToStringAnsi(getData.address);
    string strS1 = Marshal.PtrToStringAnsi(getData.s1);
    lbName.Text = strName;
    lbs1.Text = strS1;
    lbAddresss.Text = straddresss;
    lbID.Text = getData.ID.ToString();
    lbnMode.Text = getData.nMode.ToString();
    break;
    } base.WndProc (ref m);
    } private void button1_Click(object sender, System.EventArgs e)
    {
    Form2 form = new Form2();
    form.Show();
    } }
    }
      

  9.   

    Form2:using System;
    using System.Text;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace TestSendMessage
    {
    /// <summary>
    /// Form2 の概要の説明です。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox tbxnMode;
    private System.Windows.Forms.Label lbnMode;
    private System.Windows.Forms.TextBox tbxs1;
    private System.Windows.Forms.Label lbs1;
    private System.Windows.Forms.TextBox tbxID;
    private System.Windows.Forms.Label lbId;
    private System.Windows.Forms.Label lbAddress;
    private System.Windows.Forms.TextBox tbxaddresss;
    private System.Windows.Forms.Label lbName;
    private System.Windows.Forms.TextBox tbxName;
    /// <summary>
    /// 必要なデザイナ変数です。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form2()
    {
    //
    // Windows フォーム デザイナ サポートに必要です。
    //
    InitializeComponent(); //
    // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
    //
    } /// <summary>
    /// 使用されているリソースに後処理を実行します。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows フォーム デザイナで生成されたコード 
    /// <summary>
    /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
    /// コード エディタで変更しないでください。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.tbxnMode = new System.Windows.Forms.TextBox();
    this.lbnMode = new System.Windows.Forms.Label();
    this.tbxs1 = new System.Windows.Forms.TextBox();
    this.lbs1 = new System.Windows.Forms.Label();
    this.tbxID = new System.Windows.Forms.TextBox();
    this.lbId = new System.Windows.Forms.Label();
    this.lbAddress = new System.Windows.Forms.Label();
    this.tbxaddresss = new System.Windows.Forms.TextBox();
    this.lbName = new System.Windows.Forms.Label();
    this.tbxName = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(82, 236);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "Send";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // tbxnMode
    // 
    this.tbxnMode.Location = new System.Drawing.Point(132, 20);
    this.tbxnMode.Name = "tbxnMode";
    this.tbxnMode.TabIndex = 1;
    this.tbxnMode.Text = "";
    // 
    // lbnMode
    // 
    this.lbnMode.Location = new System.Drawing.Point(20, 21);
    this.lbnMode.Name = "lbnMode";
    this.lbnMode.Size = new System.Drawing.Size(100, 16);
    this.lbnMode.TabIndex = 2;
    this.lbnMode.Text = "nMode";
    // 
    // tbxs1
    // 
    this.tbxs1.Location = new System.Drawing.Point(132, 46);
    this.tbxs1.Name = "tbxs1";
    this.tbxs1.TabIndex = 1;
    this.tbxs1.Text = "";
    // 
    // lbs1
    // 
    this.lbs1.Location = new System.Drawing.Point(20, 47);
    this.lbs1.Name = "lbs1";
    this.lbs1.Size = new System.Drawing.Size(100, 16);
    this.lbs1.TabIndex = 2;
    this.lbs1.Text = "s1";
    // 
    // tbxID
    // 
    this.tbxID.Location = new System.Drawing.Point(132, 72);
    this.tbxID.Name = "tbxID";
    this.tbxID.TabIndex = 1;
    this.tbxID.Text = "";
    // 
    // lbId
    // 
    this.lbId.Location = new System.Drawing.Point(20, 72);
    this.lbId.Name = "lbId";
    this.lbId.Size = new System.Drawing.Size(100, 16);
    this.lbId.TabIndex = 2;
    this.lbId.Text = "ID";
    // 
    // lbAddress
    // 
    this.lbAddress.Location = new System.Drawing.Point(20, 98);
    this.lbAddress.Name = "lbAddress";
    this.lbAddress.Size = new System.Drawing.Size(100, 16);
    this.lbAddress.TabIndex = 2;
    this.lbAddress.Text = "address";
    // 
    // tbxaddresss
    // 
    this.tbxaddresss.Location = new System.Drawing.Point(132, 97);
    this.tbxaddresss.Name = "tbxaddresss";
    this.tbxaddresss.TabIndex = 1;
    this.tbxaddresss.Text = "";
    // 
    // lbName
    // 
    this.lbName.Location = new System.Drawing.Point(20, 130);
    this.lbName.Name = "lbName";
    this.lbName.Size = new System.Drawing.Size(100, 16);
    this.lbName.TabIndex = 2;
    this.lbName.Text = "Name";
    // 
    // tbxName
    // 
    this.tbxName.Location = new System.Drawing.Point(132, 129);
    this.tbxName.Name = "tbxName";
    this.tbxName.TabIndex = 1;
    this.tbxName.Text = "";
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
    this.ClientSize = new System.Drawing.Size(258, 267);
    this.Controls.Add(this.lbnMode);
    this.Controls.Add(this.tbxnMode);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.tbxs1);
    this.Controls.Add(this.lbs1);
    this.Controls.Add(this.tbxID);
    this.Controls.Add(this.lbId);
    this.Controls.Add(this.lbAddress);
    this.Controls.Add(this.tbxaddresss);
    this.Controls.Add(this.lbName);
    this.Controls.Add(this.tbxName);
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 指定ウィンドウを存在するがどうかの判断
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    protected bool IsExistSpecifiedForm(string text)
    {
    IntPtr hWnd = Win32API.FindWindow(null, text);
    if(hWnd == IntPtr.Zero)
    {
    return false;
    }
    return true;
    } /// <summary>
    /// 指定ウィンドウを手前に表示させる
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    protected bool ForegroundWindow(string text)
    {
    IntPtr hWnd = Win32API.FindWindow(null, text);
    if(hWnd == IntPtr.Zero)
    {
    return false;
    }
    if(Win32API.IsWindowVisible(hWnd) == false)
    {
    return false;
    } Win32API.SetForegroundWindow(hWnd);
    return true;
    } protected bool SendCopyDataMessage(string formName,int inmode, string s1, uint ID, string address, string name)
    {
    IntPtr hWnd = Win32API.FindWindow(null, formName);
    if(hWnd == IntPtr.Zero)
    {
    return false;
    } IntPtr ptrName = Marshal.StringToHGlobalAnsi(name);
    IntPtr ptrS1 = Marshal.StringToHGlobalAnsi(s1);
    IntPtr ptrAddress = Marshal.StringToHGlobalAnsi(address); try
    {
    Win32API.data sendData = new TestSendMessage.Win32API.data();
    sendData.nMode = inmode;
    sendData.s1 = ptrS1;
    sendData.ID = ID;
    sendData.address = ptrAddress;
    sendData.Name = ptrName; IntPtr ptrCopyData = Marshal.AllocHGlobal(Marshal.SizeOf(sendData)); try
    {
    Marshal.StructureToPtr(sendData, ptrCopyData, true);
    Win32API.SendMessage(hWnd, Win32API.WM_COPYDATA, (int)this.Handle, (int)ptrCopyData);
    }
    finally
    {
    Marshal.FreeHGlobal(ptrCopyData);
    }
    }
    finally
    {
    Marshal.FreeHGlobal(ptrS1);
    Marshal.FreeHGlobal(ptrAddress);
    Marshal.FreeHGlobal(ptrName);
    } return true;
    } private void button1_Click(object sender, System.EventArgs e)
    {
    if (IsExistSpecifiedForm("Form1"))
    {
    int inMode = int.Parse(tbxnMode.Text);
    string s1 = tbxs1.Text;
    uint id = Convert.ToUInt32(tbxID.Text);
    SendCopyDataMessage("Form1", inMode, s1, id, tbxaddresss.Text, tbxName.Text);
    }
    }
    }
    }