private void MonitorGocator_Tick(object sender, EventArgs e) { // 该函数由定时器配置,周期性执行 if (data_watcher == true) { // 一旦在 data_watcher 为真的情况下触发了定时器,说明数据采集结束了 timer_MonitorGocator.Stop();// 先停掉定时器 // 初始化消息发送 if (If_Init_WM_COPY == false) { hwndRecvWindow = ImportFromDLL.FindWindow(null, strDlgTitle); if (hwndRecvWindow == IntPtr.Zero) { Console.WriteLine("请先启动接收消息程序"); // 这个接口可能要修改,程序需要自动启动 return; } hwndSendWindow = ImportFromDLL.GetForegroundWindow(); if (hwndSendWindow == IntPtr.Zero) { Console.WriteLine("获取自己的窗口句柄失败,请重试"); return; } If_Init_WM_COPY = true; } // Enfof-初始化消息发送 ImportFromDLL.COPYDATASTRUCT test_copydata; test_copydata.dwData = IntPtr.Zero; test_copydata.cbData = 0; test_copydata.lpData = IntPtr.Zero; ImportFromDLL.SendMessage(hwndRecvWindow, MyMsg.WM_USR_TEST, hwndSendWindow, ref test_copydata); } }
}// - Endof - Form1Load private void Btn_Test_Click(object sender, EventArgs e) { // 初始化消息发送 if (If_Init_WM_COPY == false) { hwndRecvWindow = ImportFromDLL.FindWindow(null, strDlgTitle); if (hwndRecvWindow == IntPtr.Zero) { Console.WriteLine("请先启动接收消息程序"); // 这个接口可能要修改,程序需要自动启动 return; } hwndSendWindow = ImportFromDLL.GetForegroundWindow(); if (hwndSendWindow == IntPtr.Zero) { Console.WriteLine("获取自己的窗口句柄失败,请重试"); return; } If_Init_WM_COPY = true; } // Enfof-初始化消息发送 // 1. z方向轮廓数据 string file_path = @"C:\Users\jiajun\SynologyDrive\005-LeiMu_GangJin\p01-C++\Gocater——data\New_Data\Profile_Z"; List <double> db_list_z = new List <double>(); if (File.Exists(file_path)) { using (StreamReader sr = File.OpenText(file_path)) { string s = ""; while ((s = sr.ReadLine()) != null) { db_list_z.Add(double.Parse(s)); } } } else { return; } // 尝试向共享内存中写入数据 double[] db_array_z = db_list_z.ToArray(); accessor.WriteArray <double>(0, db_array_z, 0, db_array_z.Length); // 向C++发送消息,通知读取 If_received = false; ImportFromDLL.COPYDATASTRUCT copydata; copydata.dwData = (IntPtr)1; // 表明是z copydata.cbData = db_array_z.Length; copydata.lpData = IntPtr.Zero; ImportFromDLL.SendMessage(hwndRecvWindow, ImportFromDLL.WM_COPYDATA, hwndSendWindow, ref copydata); // 等待C++返回消息,读取完成 while (If_received == false) { Thread.Sleep(1000); Console.WriteLine("Waiting for received..."); } db_array_z = null; // 2. x 方向轮廓数据 file_path = @"C:\Users\jiajun\SynologyDrive\005-LeiMu_GangJin\p01-C++\Gocater——data\New_Data\Profile_X"; List <double> db_list_x = new List <double>(); if (File.Exists(file_path)) { using (StreamReader sr = File.OpenText(file_path)) { string s = ""; while ((s = sr.ReadLine()) != null) { db_list_x.Add(double.Parse(s)); } } } else { return; } double[] db_array_x = db_list_x.ToArray(); accessor.WriteArray <double>(0, db_array_x, 0, db_array_x.Length); If_received = false; copydata.dwData = (IntPtr)2; // 表明是x copydata.cbData = db_list_x.Count; copydata.lpData = IntPtr.Zero; ImportFromDLL.SendMessage(hwndRecvWindow, ImportFromDLL.WM_COPYDATA, hwndSendWindow, ref copydata); while (If_received == false) { Thread.Sleep(1000); Console.WriteLine("Waiting for received..."); } db_array_x = null; // 3. 时间戳 file_path = @"C:\Users\jiajun\SynologyDrive\005-LeiMu_GangJin\p01-C++\Gocater——data\New_Data\TimeStamp"; List <ulong> ulTimeStamp = new List <ulong>(); if (File.Exists(file_path)) { using (StreamReader sr = File.OpenText(file_path)) { string s = ""; while ((s = sr.ReadLine()) != null) { //ulTimeStamp.Add(ulong.Parse(s)); ulTimeStamp.Add(ulong.Parse(s)); } } } else { return; } ulong[] ulong_timestamps = ulTimeStamp.ToArray(); accessor.WriteArray <ulong>(0, ulong_timestamps, 0, ulong_timestamps.Length); If_received = false; copydata.dwData = (IntPtr)3; // 表明是 time stamp copydata.cbData = ulTimeStamp.Count; copydata.lpData = IntPtr.Zero; ImportFromDLL.SendMessage(hwndRecvWindow, ImportFromDLL.WM_COPYDATA, hwndSendWindow, ref copydata); while (If_received == false) { Thread.Sleep(1000); Console.WriteLine("Waiting for received..."); } copydata.dwData = (IntPtr)4; // 表明是 要求退出 copydata.cbData = 0; copydata.lpData = IntPtr.Zero; ImportFromDLL.SendMessage(hwndRecvWindow, ImportFromDLL.WM_COPYDATA, hwndSendWindow, ref copydata); return; }
private void SendBtn_Click(object sender, RoutedEventArgs e) { string strDlgTitle = "RecvMessage"; strDlgTitle = "WindowsProject2"; //接收端的窗口句柄 IntPtr hwndRecvWindow = ImportFromDLL.FindWindow(null, strDlgTitle); if (hwndRecvWindow == IntPtr.Zero) { Console.WriteLine("请先启动接收消息程序"); return; } //自己的窗口句柄 IntPtr hwndSendWindow = ImportFromDLL.GetConsoleWindow(); hwndSendWindow = WHwnd.GetWindowHwndSource(this); if (hwndSendWindow == IntPtr.Zero) { Console.WriteLine("获取自己的窗口句柄失败,请重试"); return; } for (int i = 0; i < 1; i++) { string strText = DateTime.Now.ToString(); byte[] imagedata = new byte[65536]; byte[] data = new byte[65536]; Random random = new Random(); for (int index = 0; index < imagedata.Length; index++) { int ccc = random.Next(0, byte.MaxValue); imagedata[index] = (byte)44; data[index] = (byte)ccc; } //填充COPYDATA结构 ImportFromDLL.COPYDATASTRUCT copydata = new ImportFromDLL.COPYDATASTRUCT(); //copydata.cbData = Encoding.Default.GetBytes(strText).Length; //长度 注意不要用strText.Length; //copydata.lpData = strText; //内容 // { ImportFromDLL.COPYDATASTRUCT cds = new ImportFromDLL.COPYDATASTRUCT(); //cds.dwData = (IntPtr)flag; cds.cbData = data.Length; cds.lpData = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, cds.lpData, data.Length); //SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds); // } //copydata.cbData = imagedata.Length; //长度 注意不要用strText.Length; //copydata.lpData = imagedata; //内容 ImportFromDLL.SendMessage(hwndRecvWindow, ImportFromDLL.WM_COPYDATA, hwndSendWindow, ref cds); Console.WriteLine(strText); Thread.Sleep(1000); } }