110,538
社区成员
发帖
与我相关
我的任务
分享
private static void SendMsg(IntPtr hWnd, string msg)
{
if (NativeMethods.IsWindow(hWnd))
{
//找到 发送 按钮
IntPtr hwndButton = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "Button", "发送(S)");
if (IntPtr.Zero != hwndButton)
{
//找到窗体顺序上的第一个RichEdit20A控件,其实就是消息显示框
IntPtr hwndRichEdit = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "RichEdit20A", null);
//在顺序上是显示框的下一个窗体
if (IntPtr.Zero != hwndRichEdit)
{
//找到 AfxWnd42 这个窗体
hwndRichEdit = NativeMethods.GetWindow(hwndRichEdit, NativeMethods.GW_HWNDNEXT);
if (IntPtr.Zero != hwndRichEdit)
{
//这才是真正的消息输入框
hwndRichEdit = NativeMethods.FindWindowEx(hwndRichEdit, IntPtr.Zero, "RichEdit20A", null);
if (hwndRichEdit != IntPtr.Zero)
{
//发送消息,因为QQ屏蔽了 WM_SETTEXT, WM_PASTE 命令,所有采用 EM_REPLACESEL 来实现
NativeMethods.SendMessage(hwndRichEdit, NativeMethods.EM_REPLACESEL, IntPtr.Zero, msg);
//给发送按钮发 鼠标单击消息
NativeMethods.SendMessage(hwndButton, NativeMethods.BM_CLICK, IntPtr.Zero, IntPtr.Zero);
}
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace InjectionLibrary.Window
{
/// <summary>
/// API声明类
/// </summary>
public static class NativeMethods
{
#region Callback Delegate Section
public delegate bool EnumDesktopWindowsDelegate(IntPtr hWnd, uint lParam);
#endregion
#region Windows API Import Section
//[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true, CharSet = CharSet.Unicode)]
//public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,
string lpszWindow);
[DllImport("user32.dll", EntryPoint = "GetWindowTextLength", SetLastError = true,
CharSet = CharSet.Unicode)]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "GetClassName", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder buf, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false,
CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumDesktopWindows(IntPtr hDesktop,
EnumDesktopWindowsDelegate lpEnumCallbackFunction, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "GetWindow", SetLastError = true)]
public static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);
[DllImport("user32.dll", EntryPoint = "IsWindow", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindow(IntPtr hWnd);
#endregion
#region Windows Message Declaration Section
public const uint BM_CLICK = 0xF5;
public const uint GW_HWNDNEXT = 0x02;
public const uint EM_REPLACESEL = 0xC2;
#endregion
}
}
消息注入!