111 lines
3.9 KiB
C#
111 lines
3.9 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Runtime.InteropServices;
|
|||
|
using System.Text;
|
|||
|
using System.Windows.Forms;
|
|||
|
|
|||
|
namespace DyLine
|
|||
|
{
|
|||
|
public class clsMsg
|
|||
|
{
|
|||
|
[DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
|
|||
|
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
|
|||
|
|
|||
|
[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
|
|||
|
private static extern IntPtr GetParent(IntPtr hWnd);
|
|||
|
|
|||
|
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
|
|||
|
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
|
|||
|
|
|||
|
[DllImport("user32.dll", EntryPoint = "IsWindow")]
|
|||
|
private static extern bool IsWindow(IntPtr hWnd);
|
|||
|
private const int WM_COPYDATA = 0x004A;
|
|||
|
public struct COPYDATASTRUCT
|
|||
|
{
|
|||
|
public IntPtr dwData;
|
|||
|
public int cData;
|
|||
|
[MarshalAs(UnmanagedType.LPStr)]
|
|||
|
public string lpData;
|
|||
|
}
|
|||
|
[DllImport("User32.dll", EntryPoint = "SendMessage")]
|
|||
|
private static extern int SendMessage(int hwnd, int msg, int wParam, ref COPYDATASTRUCT IParam);
|
|||
|
private delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
|
|||
|
public void SendMsg(int handle, int wParam, string str)
|
|||
|
{
|
|||
|
byte[] arr = System.Text.Encoding.Default.GetBytes(str);
|
|||
|
int len = arr.Length;
|
|||
|
COPYDATASTRUCT cdata;
|
|||
|
cdata.dwData = (IntPtr)100;
|
|||
|
cdata.lpData = str;
|
|||
|
cdata.cData = len + 1;
|
|||
|
SendMessage(handle, WM_COPYDATA, wParam, ref cdata);
|
|||
|
}
|
|||
|
|
|||
|
readonly string msgText = "";
|
|||
|
readonly int _wParam = 0;
|
|||
|
//public void SendMsgToryProcess(int wParam,string str)
|
|||
|
//{
|
|||
|
// IntPtr ptrWnd = IntPtr.Zero;
|
|||
|
// Process[] items = Process.GetProcessesByName("ryProcessManager");
|
|||
|
// msgText = str;
|
|||
|
// _wParam = wParam;
|
|||
|
// for (int i = 0; i < items.Length; i++)
|
|||
|
// {
|
|||
|
// uint uiPid = (uint)items[i].Id; // 进程 ID
|
|||
|
// bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
|
|||
|
// }
|
|||
|
//}
|
|||
|
public void SendMsgToryProcess(int wParam, string str)
|
|||
|
{
|
|||
|
for (int i = 1; i < 5; i++)
|
|||
|
{
|
|||
|
object mainHandle = ryCommon.Model.ryMemoryShare.ReadFromMemory(1024, typeof(int), "ryProcessManager" + i.ToString());
|
|||
|
if (mainHandle != null)
|
|||
|
{
|
|||
|
SendMsg((int)mainHandle, wParam, str);
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
public void SendMsgToryProcess(int wParam, string MemoryId, string str)
|
|||
|
{
|
|||
|
if (MemoryId == null || MemoryId.Length == 0)
|
|||
|
{
|
|||
|
SendMsgToryProcess(wParam, str);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
object mainHandle = ryCommon.Model.ryMemoryShare.ReadFromMemory(1024, typeof(int), MemoryId);
|
|||
|
if (mainHandle != null)
|
|||
|
{
|
|||
|
SendMsg((int)mainHandle, wParam, str);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
public static string GetMsg(Message m)
|
|||
|
{
|
|||
|
COPYDATASTRUCT cdata = new COPYDATASTRUCT();
|
|||
|
Type mytype = cdata.GetType();
|
|||
|
cdata = (COPYDATASTRUCT)m.GetLParam(mytype);
|
|||
|
return cdata.lpData;
|
|||
|
}
|
|||
|
private bool EnumWindowsProc(IntPtr hwnd, uint lParam)
|
|||
|
{
|
|||
|
uint uiPid = 0;
|
|||
|
|
|||
|
if (GetParent(hwnd) == IntPtr.Zero)
|
|||
|
{
|
|||
|
GetWindowThreadProcessId(hwnd, ref uiPid);
|
|||
|
if (uiPid == lParam) // 找到进程对应的主窗口句柄
|
|||
|
{
|
|||
|
SendMsg(hwnd.ToInt32(), _wParam, msgText);
|
|||
|
//return false; // 返回 false 以终止枚举窗口
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|