DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1614万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

1、要善用spy++

2、不同的控件主要靠GetDlgCtrlID去区分

3、要获得另一个进程的焦点窗口(GetFocus)需要调用AttachThreadInput

4、尽量少用keybd_event模拟键盘输入,主要是该函数不能保证按键消息一定能被特定进程接收到。取而代之的是SendMessage(hwnd, WM_IME_CHAR, ...)。而模拟回车按下要用PostMessage (hFocus, WM_KEYDOWN, VK_RETURN, 0),SendMessage不知为何不起作用

5、通达信在按下第一个数字键后会弹出键盘精灵,此时焦点窗口会转移到键盘精灵上,要重新调用GetFocus一次

6、GetWindow(hMainWnd, GW_ENABLEDPOPUP)可以获得当前的弹出窗口句柄

7、PostMessage(hPopup, WM_COMMAND, MAKEWPARAM(IDOK, BN_CLICKED), NULL),模拟“确定”键被按下

8、EnumChildWindows函数可以枚举一个窗口的所有子窗口

 

复制代码

class ThreadHandler
{
    #region P/Invoking and constants definition
    const uint WM_GETTEXT = 0x000D;

    [DllImport("user32.dll")]
    static extern IntPtr SetFocus(IntPtr hWnd);

    [DllImport("user32.dll")]
    private static extern int GetWindowThreadProcessId(IntPtr hWnd, uint lpdwProcessId = 0);

    delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
    [DllImport("user32.dll")]
    static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn,
        IntPtr lParam);

    [DllImport("user32.dll")]
    static extern bool AttachThreadInput(int idAttach, int idAttachTo, bool fAttach);

    [DllImport("kernel32.dll")]
    static extern int GetCurrentThreadId();

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, StringBuilder lParam);

    #endregion

    public readonly string ProcessName, WindowName;
    protected readonly int TargetThreadID, CurrentThreadID;
    protected readonly IntPtr TargetWindowHandle;

    public ThreadHandler(string processName, string windowName)
    {
        CurrentThreadID = GetCurrentThreadId();
        ProcessName = processName;
        WindowName = windowName;

        object[] objs = GetWindowThread(processName, windowName);
        if (objs == null)
        {
            throw new ArgumentException("Could not find the specified process/window.");
        }

        TargetThreadID = (int)objs[0];
        TargetWindowHandle = (IntPtr)objs[1];
    }

    public ThreadHandler(string processName)
    {
        CurrentThreadID = GetCurrentThreadId();
        ProcessName = processName;

        var processes = Process.GetProcessesByName(ProcessName);
        if (processes.Length == 0)
        {
            throw new ArgumentException("Could not find the specified process.");
        }
        var appProc = processes[0];

        WindowName = appProc.MainWindowTitle;
        TargetThreadID = GetWindowThreadProcessId(appProc.MainWindowHandle);
        TargetWindowHandle = appProc.MainWindowHandle;
    }

    public bool AttachThreadInput()
    {
        return AttachThreadInput(CurrentThreadID, TargetThreadID, true);
    }

    public bool DetachThreadInput()
    {
        return AttachThreadInput(CurrentThreadID, TargetThreadID, false);
    }

    public void SetFocus()
    {
        SetFocus(TargetWindowHandle);
    }

    static object[] GetWindowThread(string processName, string windowName)
    {
        var processes = Process.GetProcessesByName(processName);
        if (processes.Length > 0)
        {
            //Fill a list of handles
            var handles = new List<IntPtr>();
            foreach (ProcessThread thread in processes[0].Threads)
                EnumThreadWindows(thread.Id,
                    (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);

            //Create a stringbuilder to function as storage unit
            StringBuilder nameBuffer = new StringBuilder(64);
            foreach (var hWnd in handles)
            {
                //And finally compare the caption of the window with the requested name
                nameBuffer.Clear();
                SendMessage(hWnd, WM_GETTEXT, nameBuffer.Capacity, nameBuffer);
                if (nameBuffer.ToString() == windowName)
                {
                    return new object[2] { GetWindowThreadProcessId(hWnd), hWnd };
                }
            }
        }
        return null;
    }
}

复制代码

复制代码

static void Main(string[] args)
    {
        Console.WriteLine("Please input the name of the process to hook: ");
        string pName = Console.ReadLine();
        Console.WriteLine("Input the name of a specific window, or leave blank: ");
        string pWnd = Console.ReadLine();
        ThreadHandler threadHandler;
        try
        {
            if(!String.IsNullOrWhiteSpace(pWnd))
                threadHandler = new ThreadHandler(pName, pWnd);
            else
                threadHandler = new ThreadHandler(pName);
        }
        catch
        {
            Console.WriteLine("Error: " + pName +" does not seem to be running.");
            Console.ReadKey();
            return;
        }

        if (!threadHandler.AttachThreadInput())
        {
            Console.WriteLine("Error: The application tried to attach its Input Processing Mechanism to " + threadHandler.ProcessName + ", but failed.");
            Console.ReadKey();
            return;
        }
        Console.WriteLine("Input Processing Mechanism correctly attached to " + threadHandler.ProcessName + ".");
        threadHandler.SetFocus();
        InputSimulator.SimulateTextEntry("test"); //InputSimulator is a seemingly famous SendInput wrapper. Replacing this line with the code for a keystroke also doesn't work.
        Console.ReadLine();
        Console.WriteLine("Detaching Input Processing Mechanism.");
        threadHandler.DetachThreadInput();
    }

复制代码

posted on   DoubleLi  阅读(990)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2017-11-24 windows服务和进程的区别和联系
2017-11-24 Daemon Process
2017-11-24 C++11中的原子操作(atomic operation)
2013-11-24 MFC消息顺序
点击右上角即可分享
微信分享提示