c# copydata 消息
2021-01-20 22:16
标签:type ESS classname oid level cdata etl textbox gets c# copydata 消息 标签:type ESS classname oid level cdata etl textbox gets 原文地址:https://www.cnblogs.com/onlyou13/p/12120608.htmlusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace broadcast2
{
public partial class broadcast2 : Form
{
IPAddress IP = null;
EndPoint SendEP = null;
Socket sock = null;
private string logfile = "";
private int index = 0;
private Thread rcvThread = null;
public broadcast2()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.EnableBroadcast = true;
logfile = Path.Combine(Application.StartupPath, "logs.txt");
this.Text = "发送者";
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cData;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpData;
}
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hwnd, int msg, int wParam, ref COPYDATASTRUCT IParam);
private void SendFormMsg(IntPtr handle, string atext)
{
byte[] arr = Encoding.Unicode.GetBytes(atext);
int len = arr.Length;
COPYDATASTRUCT cdata;
cdata.dwData = (IntPtr)100;
cdata.lpData = atext;
cdata.cData = len + 1;
SendMessage(handle, 0x04A, 0, ref cdata);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x04A)
{
COPYDATASTRUCT cdata = new COPYDATASTRUCT();
Type mytype = cdata.GetType();
cdata = (COPYDATASTRUCT)m.GetLParam(mytype);
MessageBox.Show(cdata.lpData);
}
}
private void button2_Click(object sender, EventArgs e)
{
SendFormMsg(FindWindow("TForm2", "Form2"), "xxx 暗室逢灯12345 54321");
}
private void Send()
{
//byte[] sendbuf = Encoding.UTF8.GetBytes(string.Format("消息 {0}", ++index));
byte[] sendbuf = Encoding.UTF8.GetBytes(DateTime.Now.ToString("HHmmss"));
sock.SendTo(sendbuf, SendEP);
//Console.WriteLine(string.Format("{0} 发送广播消息 {1}", DateTime.Now.ToString("HH:mm:ss.fff"), i));
writeLog(string.Format("{0} 发送广播消息 {1}", DateTime.Now.ToString("HH:mm:ss.fff"), DateTime.Now.ToString("HHmmss")));
//byte[] receiveBuf = new byte[1000];
//sock.Receive(receiveBuf);
//string msg = Encoding.UTF8.GetString(receiveBuf, 0, receiveBuf.Length);
////Console.WriteLine(string.Format("{0} 收到回复: {0}", DateTime.Now.ToString("HH:mm:ss.fff"), msg);
//writeLog(string.Format("{0} 收到回复: {1}", DateTime.Now.ToString("HH:mm:ss.fff"), msg));
}
private void writeLog(string text)
{
using (StreamWriter sw = new StreamWriter(logfile, true, Encoding.UTF8))
{
sw.WriteLine(text);
}
}
private void button1_Click(object sender, EventArgs e)
{
index = 0;
if (!timer1.Enabled)
{
IP = IPAddress.Parse(textBox1.Text.Trim());
SendEP = new IPEndPoint(IP, 11000);
timer1.Start();
button1.Text = "停止";
textBox1.Enabled = false;
rcvThread = new Thread(new ThreadStart(DoReceive));
rcvThread.IsBackground = true;
rcvThread.Start();
}
else
{
timer1.Stop();
button1.Text = "开始";
textBox1.Enabled = true;
if (rcvThread != null)
{
rcvThread.Abort();
rcvThread = null;
}
}
}
///