2

我想用新项目扩展默认扬声器通知的(托盘图标)右键单击上下文菜单。另外,我想使用 C++ 处理鼠标点击。

插图

在此处输入图像描述

到目前为止我所知道的

我学习了如何使用CreateRemoteThread()进行 dll 注入,因为我认为这是要走的路。我的问题是:在注入的 dll 里面做什么?例如,如何访问 NotifyIcon 对象?

也许可以通过简单的 Windows API 调用,但我不熟悉它。

4

1 回答 1

2

感谢卢克的提示。我使用EasyHook让它工作。我选择了它,因为它还支持 64 位 dll-inject。

DLL注入:

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using EasyHook;
namespace InjectDLL
    {
    public class Main : EasyHook.IEntryPoint
    {
        LocalHook CreateTrackPopupMenuExHook;
        public Main(RemoteHooking.IContext InContext){}
        public void Run(RemoteHooking.IContext InContext)
        {
            try
            {
                CreateTrackPopupMenuExHook = LocalHook.Create(
                    LocalHook.GetProcAddress("user32.dll", "TrackPopupMenuEx"),
                    new DTrackPopupMenuEx(TrackPopupMenuEx_Hooked),
                    this);
                CreateTrackPopupMenuExHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
            }
            catch
            {
                return;
            }
            while (true)
            {
                Thread.Sleep(500);
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool AppendMenu(IntPtr hMenu, long uFlags, int uIDNewItem, string lpNewItem);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        static extern IntPtr TrackPopupMenuEx(
            IntPtr hMenu,
            uint fuFlags,
            int x,
            int y,
            IntPtr hwnd,
            IntPtr lptpm);

        [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        delegate IntPtr DTrackPopupMenuEx(
            IntPtr hMenu,
            uint fuFlags,
            int x,
            int y,
            IntPtr hwnd,
            IntPtr lptpm);

        const long MF_STRING = 0x00000000L;
        const long MF_SEPARATOR = 0x00000800L;

        static IntPtr TrackPopupMenuEx_Hooked(
            IntPtr hMenu,
            uint fuFlags,
            int x,
            int y,
            IntPtr hwnd,
            IntPtr lptpm)
        {
            IntPtr returnValue = IntPtr.Zero;
            try
            {
                    //Separator
                    AppendMenu(hMenu, MF_SEPARATOR, 0, null);
                    //New menu item
                    AppendMenu(hMenu, MF_STRING, 40010, "TestMenuItem");
                    //call the default procedure
                    returnValue = TrackPopupMenuEx(hMenu, fuFlags, x, y, hwnd, lptpm);
                    //our menu item is selected
                    if (returnValue == (IntPtr)40010)
                    {
                        /* CODE HERE */
                        returnValue = IntPtr.Zero;
                    }
                    return returnValue;
            }
            catch
            {
                return;
            }
            return returnValue;
        }
    }
}
于 2012-07-06T11:56:33.683 回答