关于如何使用SendMessage方法发送文字
接收侧窗口类可以这么写(C#)
namespace MessageHandler
{
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPTStr)] //这里用LPStr会报错
public string lpData;
}
public partial class MessageForm : Form
{
protected override void DefWndProc(ref Message m)
{
base.DefWndProc(ref m);
if (m.Msg != 74) return;
var lParam = m.LParam;
var data = Marshal.PtrToStructure<COPYDATASTRUCT>(lParam);
Console.WriteLine(data.lpData);
if (data.lpData == null) return;
var s = data.lpData.Substring(0, data.cbData / 2); //不切割尾部会多东西
Console.WriteLine(s);//业务代码
}
public MessageForm()
{
InitializeComponent();
Console.WriteLine(this.Handle);
}
}