2012-07-03 62 views
2

我想用一個新項目擴展默認的Speakers Notificon's(托盤圖標)右鍵點擊上下文菜單。另外,我想用C++來處理鼠標點擊。如何更改explorer.exe中的NotifyIcon的上下文菜單?

插圖

enter image description here

我所知道的,到目前爲止

我學會了如何DLL的注射用CreateRemoteThread的(),因爲我認爲這是要走的路。我的問題是:在注入的DLL內部做什麼?例如,如何訪問NotifyIcon對象?

也許這是可能的一個簡單的Windows API調用,但我不熟悉它。

+0

最終有人會打電話給TrackPopupMenu()。我想你可以勾住它,複製菜單,插入你的項目,然後用TPM_RETURNCMD調用真正的TrackPopupMenu()。如果身份證是你的,你可以處理它;如果該ID不是你的,那麼你可以返回給調用者。不過,如果你問我,這是一個非常毛茸茸的方法。 – Luke

回答

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; 
     } 
    } 
} 
相關問題