本文主要是介绍c# copydata,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
发送方:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ProcessWM_CopyData
{
public partial class SendTest : Form
{
public SendTest()
{
InitializeComponent();
this.Text = "发送窗口";
}
const int WM_COPYDATA = 0x004A;
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
[DllImport("User32.dll")]
public static extern int SendMessage(int hwnd, int msg, int wParam, ref COPYDATASTRUCT IParam);
[DllImport("User32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
private void button1_Click(object sender, EventArgs e)
{
int WINDOW_HANDLE = FindWindow(null, "接受窗口");
if (WINDOW_HANDLE!=0)
{
byte[] arr = System.Text.Encoding.Default.GetBytes(textBox1.Text);
int len = arr.Length;
COPYDATASTRUCT cdata;
cdata.dwData = (IntPtr)100;
cdata.lpData = textBox1.Text;
cdata.cData = len + 1;
SendMessage(WINDOW_HANDLE, WM_COPYDATA, 0, ref cdata);
}
}
}
}
接受方:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WM_COPYDATAGET
{
public partial class GetTest : Form
{
public GetTest()
{
InitializeComponent();
this.Text = "接受窗口";
}
const int WM_COPYDATA = 0x004A;//WM_COPYDATA消息的主要目的是允许在进程间传递只读数据。
//Windows在通过WM_COPYDATA消息传递期间,不提供继承同步方式。
//其中,WM_COPYDATA对应的十六进制数为0x004A
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
protected override void DefWndProc(ref Message m)
{
switch (m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT cdata = new COPYDATASTRUCT();
Type mytype = cdata.GetType();
cdata = (COPYDATASTRUCT)m.GetLParam(mytype);
this.textBox1.Text = cdata.lpData;
break;
default:
base.DefWndProc(ref m);
break;
}
}
}
}
这篇关于c# copydata的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!