发送方源码:
接收方源码:
using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WinFormSendMsg
... {
partial class Form1 : System.Windows.Forms.Form
...{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
const int WM_COPYDATA = 0x004A;
public Form1()
...{
InitializeComponent();
}
[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.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WinFormSendMsg
... {
partial class Form1 : System.Windows.Forms.Form
...{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
const int WM_COPYDATA = 0x004A;
public Form1()
...{
InitializeComponent();
}
[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
... {
partial class Form1 : System.Windows.Forms.Form
...{
private System.Windows.Forms.TextBox textBox1;
const int WM_COPYDATA = 0x004A;
public Form1()
...{
InitializeComponent();
}
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;
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsFormGetMsg
... {
partial class Form1 : System.Windows.Forms.Form
...{
private System.Windows.Forms.TextBox textBox1;
const int WM_COPYDATA = 0x004A;
public Form1()
...{
InitializeComponent();
}
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;
}
}
示例:
http://file.mofile.com/
提取码:5588340629395252