Exemplo n.º 1
0
        private void openBaidu()
        {
            var    Shell_TrayWnd = win32.FindWindow("Shell_TrayWnd", null);
            IntPtr hNext         = IntPtr.Zero;
            //第一个对话框
            var TrayNotifyWnd   = win32.FindWindowEx(Shell_TrayWnd, hNext, "TrayNotifyWnd", "");
            var SysPager        = win32.FindWindowEx(TrayNotifyWnd, hNext, "SysPager", "");
            var ToolbarWindow32 = win32.FindWindowEx(SysPager, hNext, null, "用户升级的通知区域");
            int Pid             = 0;

            GetWindowThreadProcessId(ToolbarWindow32, out Pid);
            //打开ToolbarWindow32所在的进程,就是explorer.exe
            IntPtr hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, Pid);
            //在进程explorer.exe中申请内存

            IntPtr lpButton = VirtualAllocEx(hProcess, IntPtr.Zero, 1024, AllocationType.Commit, MemoryProtection.ReadWrite);
            //执行TB_GETBUTTON
            TBBUTTONINFO info  = new TBBUTTONINFO();
            var          count = API.SendMessage(ToolbarWindow32, API.TB_GETBUTTON, 0, 0);

            API.SendMessage(ToolbarWindow32, API.TB_GETBUTTON, 1, ref info);

            //从进程explorer.exe中读取需要的数据
            byte[] lpBuffer           = new byte[Marshal.SizeOf(info)];
            int    lpNumberOfByteRead = 0;

            ReadProcessMemory(hProcess, info.lParam, out lpBuffer, Marshal.SizeOf(info), out lpNumberOfByteRead);
            //释放在进程explorer.exe中申请的内存
            VirtualFreeEx(hProcess, lpButton, Marshal.SizeOf(info), FreeType.Release);
            CloseHandle(hProcess);
        }
Exemplo n.º 2
0
        private IntPtr GetButtonLParam(int itemId)
        {
            TBBUTTONINFO info = new TBBUTTONINFO();

            info.cbSize    = Marshal.SizeOf(info);
            info.dwMask    = TBIF_COMMAND | TBIF_LPARAM;
            info.idCommand = itemId;
            int ret = (int)PInvoke.SendMessage(BreadcrumbController.Handle, TB_GETBUTTONINFOW, (IntPtr)itemId, ref info);

            return(ret == -1 ? IntPtr.Zero : info.lParam);
        }
Exemplo n.º 3
0
 public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTONINFO lParam);
Exemplo n.º 4
0
        internal static TBBUTTONINFO[] GetTrayButtons(IntPtr sysTray)
        {
            bool Is64Bit      = IntPtr.Size == 8;
            int  tbButtonSize = Marshal.SizeOf(Is64Bit ? new TBBUTTON64().GetType() : new TBBUTTON32().GetType());

            User32.GetWindowThreadProcessId(sysTray, out uint trayProcess);
            IntPtr hProcess = Kernel32.OpenProcess(Kernel32.PROCESS_ALL_ACCESS, false, (int)trayProcess);

            if (hProcess == IntPtr.Zero)
            {
                Logger.Instance.Log(LoggerVerbosity.Basic, "CppHelper/GetTrayButtons", "Could not get tray buttons: OpenProcess failed.");
                return(Array.Empty <TBBUTTONINFO>());
            }
            IntPtr dataPtr = Kernel32.VirtualAllocEx(hProcess, IntPtr.Zero, (uint)tbButtonSize, Kernel32.MEM_COMMIT, Kernel32.PAGE_READWRITE);

            if (dataPtr == IntPtr.Zero)
            {
                int errocode = Marshal.GetLastWin32Error();
                Logger.Instance.Log(LoggerVerbosity.Basic, "CppHelper/GetTrayButtons", "Could not get tray buttons: VirtualAllocEx failed.");
                return(Array.Empty <TBBUTTONINFO>());
            }
            int count = (int)User32.SendMessage(sysTray, User32.TB_BUTTONCOUNT, 0, 0);

            TBBUTTONINFO[] tBBUTTONINFOs = new TBBUTTONINFO[count];
            if (Is64Bit)
            {
                for (int i = 0; i < count; i++)
                {
                    TBBUTTON64 tbButton = new();
                    TRAYDATA   trData   = new();

                    User32.SendMessage(sysTray, User32.TB_GETBUTTON, new(i), dataPtr);

                    byte[] tbButtonBytes = new byte[tbButtonSize];
                    Kernel32.ReadProcessMemory(hProcess, dataPtr, tbButtonBytes, tbButtonSize, out var bytesRead);
                    GCHandle tbButtonHandle = GCHandle.Alloc(tbButtonBytes, GCHandleType.Pinned);
                    tbButton = (TBBUTTON64)Marshal.PtrToStructure(tbButtonHandle.AddrOfPinnedObject(), tbButton.GetType());
                    tbButtonHandle.Free();

                    byte[] trDataBytes = new byte[Marshal.SizeOf <TRAYDATA>()];
                    Kernel32.ReadProcessMemory(hProcess, tbButton.dwData, trDataBytes, trDataBytes.Length, out bytesRead);
                    GCHandle trDataHandle = GCHandle.Alloc(trDataBytes, GCHandleType.Pinned);
                    trData = (TRAYDATA)Marshal.PtrToStructure(trDataHandle.AddrOfPinnedObject(), trData.GetType());
                    trDataHandle.Free();

                    User32.GetWindowThreadProcessId(trData.hwnd, out uint iconPid);

                    byte[]        buffer  = new byte[2];
                    StringBuilder toolTip = new(1024);
                    if ((tbButton.fsState & User32.TBSTATE_HIDDEN) == 0)
                    {
                        IntPtr pTip = tbButton.iString;
                        while (true)
                        {
                            Kernel32.ReadProcessMemory(hProcess, pTip, buffer, 2, out bytesRead);
                            string character = Encoding.Unicode.GetString(buffer);
                            if (character == "\0")
                            {
                                break;
                            }
                            toolTip.Append(character);
                            pTip += 2;
                        }
                    }

                    TBBUTTONINFO buttonInfo = new();
                    buttonInfo.toolTip         = toolTip.ToString();
                    buttonInfo.hwnd            = trData.hwnd;
                    buttonInfo.visible         = (tbButton.fsState & User32.TBSTATE_HIDDEN) == 0;
                    buttonInfo.icon            = trData.hIcon;
                    buttonInfo.callbackMessage = trData.uCallbackMessage;
                    buttonInfo.id    = trData.uID;
                    tBBUTTONINFOs[i] = buttonInfo;
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    TBBUTTON32 tbButton = new();
                    TRAYDATA   trData   = new();

                    User32.SendMessage(sysTray, User32.TB_GETBUTTON, i, (int)dataPtr);

                    byte[] tbButtonBytes = new byte[tbButtonSize];
                    Kernel32.ReadProcessMemory(hProcess, dataPtr, tbButtonBytes, tbButtonSize, out var bytesRead);
                    GCHandle tbButtonHandle = GCHandle.Alloc(tbButtonBytes, GCHandleType.Pinned);
                    tbButton = (TBBUTTON32)Marshal.PtrToStructure(tbButtonHandle.AddrOfPinnedObject(), tbButton.GetType());
                    tbButtonHandle.Free();

                    byte[] trDataBytes = new byte[Marshal.SizeOf <TRAYDATA>()];
                    Kernel32.ReadProcessMemory(hProcess, tbButton.dwData, trDataBytes, trDataBytes.Length, out bytesRead);
                    GCHandle trDataHandle = GCHandle.Alloc(trDataBytes, GCHandleType.Pinned);
                    trData = (TRAYDATA)Marshal.PtrToStructure(trDataHandle.AddrOfPinnedObject(), trData.GetType());
                    trDataHandle.Free();

                    User32.GetWindowThreadProcessId(trData.hwnd, out uint iconPid);

                    byte[]        buffer  = new byte[1];
                    StringBuilder toolTip = new(1024);
                    if ((tbButton.fsState & User32.TBSTATE_HIDDEN) == 0)
                    {
                        IntPtr pTip = tbButton.iString;
                        while (true)
                        {
                            Kernel32.ReadProcessMemory(hProcess, pTip, buffer, 2, out bytesRead);
                            string character = Encoding.Unicode.GetString(buffer);
                            if (character == "\0")
                            {
                                break;
                            }
                            toolTip.Append(character);
                            pTip += 2;
                        }
                    }

                    TBBUTTONINFO buttonInfo = new();
                    buttonInfo.toolTip         = toolTip.ToString();
                    buttonInfo.hwnd            = trData.hwnd;
                    buttonInfo.visible         = (tbButton.fsState & User32.TBSTATE_HIDDEN) != 0;
                    buttonInfo.icon            = trData.hIcon;
                    buttonInfo.callbackMessage = trData.uCallbackMessage;
                    buttonInfo.id    = trData.uID;
                    tBBUTTONINFOs[i] = buttonInfo;
                }
            }

            Kernel32.VirtualFreeEx(hProcess, dataPtr, (uint)tbButtonSize, Kernel32.MEM_RELEASE);
            Kernel32.CloseHandle(hProcess);

            return(tBBUTTONINFOs);
        }
 public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTONINFO lParam);
Exemplo n.º 6
0
 private static extern bool WriteProcessMemory(
     IntPtr hProcess,
     IntPtr lpBaseAddress,
     ref TBBUTTONINFO lpBuffer,
     IntPtr nSize,
     out int lpNumberOfBytesWritten);
Exemplo n.º 7
0
        //タスクトレイアイコンのクリック処理
        public static bool ClickTasktrayIcon(string tooltip)
        {
            const string TRAY_WINDOW = "Shell_TrayWnd";
            const string TRAY_NOTIFYWINDOW = "TrayNotifyWnd";
            const string TRAY_PAGER = "SysPager";
            const string TOOLBAR_CONTROL = "ToolbarWindow32";
            //タスクバーのハンドル取得
            var taskbarWin = FindWindow(TRAY_WINDOW, null);
            if (taskbarWin.Equals(IntPtr.Zero)) return false;
            //通知領域のハンドル取得
            var trayWin = FindWindowEx(taskbarWin, IntPtr.Zero, TRAY_NOTIFYWINDOW, null);
            if (trayWin.Equals(IntPtr.Zero)) return false;
            //SysPagerの有無確認。(XP/2000はSysPagerあり)
            var tempWin = FindWindowEx(trayWin, IntPtr.Zero, TRAY_PAGER, null);
            if (tempWin.Equals(IntPtr.Zero)) tempWin = trayWin;
            //タスクトレイがツールバーで出来ているか確認
            // → ツールバーでなければ終了
            var toolWin = FindWindowEx(tempWin, IntPtr.Zero, TOOLBAR_CONTROL, null);
            if (toolWin.Equals(IntPtr.Zero)) return false;
            //タスクトレイのプロセス(Explorer)を取得し、外部から参照するために開く
            int expPid = 0;
            GetWindowThreadProcessId(toolWin, out expPid);
            var hProc = OpenProcess(ProcessAccess.VMOperation | ProcessAccess.VMRead | ProcessAccess.VMWrite, false, expPid);
            if (hProc.Equals(IntPtr.Zero)) return false;

            //プロセスを閉じるためにtry-finally
            try
            {
                var tbButtonLocal = new TBBUTTON();   //本プロセス内のタスクバーボタン情報作成(サイズ特定でのみ使用)
                //Explorer内のタスクバーボタン格納メモリ確保
                var ptbSysButton = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)Marshal.SizeOf(tbButtonLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
                if (ptbSysButton.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
                try
                {
                    var tbButtonInfoLocal = new TBBUTTONINFO();   //本プロセス内ツールバーボタン詳細情報作成
                    //Explorer内のタスクバーボタン詳細情報格納メモリ確保
                    var ptbSysInfo = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)Marshal.SizeOf(tbButtonInfoLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
                    if (ptbSysInfo.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
                    try
                    {
                        const int titleSize = 256;    //Tooltip文字列長
                        var title = "";            //Tooltip文字列
                        //共有メモリにTooltip読込メモリ確保
                        var pszTitle = Marshal.AllocCoTaskMem(titleSize);
                        if (pszTitle.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
                        try
                        {
                            //Explorer内にTooltip読込メモリ確保
                            var pszSysTitle = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)titleSize, AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
                            if (pszSysTitle.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
                            try
                            {
                                //通知領域ボタン数取得
                                var iCount = (int)SendMessage(toolWin, (int)Sm_Message.TB_BUTTONCOUNT, new IntPtr(0), new IntPtr(0));
                                //左から順に情報取得
                                for (var i = 0; i < iCount; i++)
                                {
                                    var dwBytes = 0;  //読み書きバイト数
                                    var tbButtonLocal2 = new TBBUTTON();  //ボタン情報
                                    var tbButtonInfoLocal2 = new TBBUTTONINFO();  //ボタン詳細情報
                                    //共有メモリにボタン情報読込メモリ確保
                                    var ptrLocal = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonLocal));
                                    if (ptrLocal.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
                                    try
                                    {
                                        Marshal.StructureToPtr(tbButtonLocal, ptrLocal, true);   //共有メモリ初期化
                                        //ボタン情報取得(idCommandを取得するため)
                                        SendMessage(
                                            toolWin,
                                            (int)Sm_Message.TB_GETBUTTON,
                                            new IntPtr(i),
                                            ptbSysButton);
                                        //Explorer内のメモリを共有メモリに読み込み
                                        ReadProcessMemory(
                                            hProc,
                                            ptbSysButton,
                                            ptrLocal,
                                            (IntPtr)Marshal.SizeOf(tbButtonLocal),
                                            out dwBytes);
                                        //共有メモリの内容を構造体に変換
                                        tbButtonLocal2 = (TBBUTTON)Marshal.PtrToStructure(
                                                                ptrLocal,
                                                                typeof(TBBUTTON));
                                    }
                                    finally
                                    {
                                        Marshal.FreeCoTaskMem(ptrLocal); //共有メモリ解放
                                    }

                                    //ボタン詳細情報を取得するためのマスク等を設定
                                    tbButtonInfoLocal.cbSize = Marshal.SizeOf(tbButtonInfoLocal);
                                    tbButtonInfoLocal.dwMask = (int)(ToolbarButtonMask.TBIF_COMMAND | ToolbarButtonMask.TBIF_LPARAM | ToolbarButtonMask.TBIF_TEXT);
                                    tbButtonInfoLocal.pszText = pszSysTitle;     //Tooltip書き込み先領域
                                    tbButtonInfoLocal.cchText = titleSize;
                                    //マスク設定等をExplorerのメモリへ書き込み
                                    WriteProcessMemory(
                                        hProc,
                                        ptbSysInfo,
                                        ref tbButtonInfoLocal,
                                        (IntPtr)Marshal.SizeOf(tbButtonInfoLocal),
                                        out dwBytes);
                                    //ボタン詳細情報取得
                                    SendMessage(
                                        toolWin,
                                        (int)Sm_Message.TB_GETBUTTONINFO,
                                        tbButtonLocal2.idCommand,
                                        ptbSysInfo);
                                    //共有メモリにボタン詳細情報を読み込む領域確保
                                    var ptrInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonInfoLocal));
                                    if (ptrInfo.Equals(IntPtr.Zero)) return false; //共有メモリ確保失敗
                                    try
                                    {
                                        Marshal.StructureToPtr(tbButtonInfoLocal, ptrInfo, true);    //共有メモリ初期化
                                        //Explorer内のメモリを共有メモリに読み込み
                                        ReadProcessMemory(
                                            hProc,
                                            ptbSysInfo,
                                            ptrInfo,
                                            (IntPtr)Marshal.SizeOf(tbButtonInfoLocal),
                                            out dwBytes);
                                        //共有メモリの内容を構造体に変換
                                        tbButtonInfoLocal2 = (TBBUTTONINFO)Marshal.PtrToStructure(
                                                                    ptrInfo,
                                                                    typeof(TBBUTTONINFO));
                                    }
                                    finally
                                    {
                                        Marshal.FreeCoTaskMem(ptrInfo);  //共有メモリ解放
                                    }
                                    //Tooltipの内容をExplorer内のメモリから共有メモリへ読込
                                    ReadProcessMemory(hProc, pszSysTitle, pszTitle, (IntPtr)titleSize, out dwBytes);
                                    //ローカル変数へ変換
                                    title = Marshal.PtrToStringAnsi(pszTitle, titleSize);

                                    //Tooltipが指定文字列を含んでいればクリック
                                    if (title.Contains(tooltip))
                                    {
                                        //PostMessageでクリックを送るために、ボタン詳細情報のlParamでポイントされているTRAYNOTIFY情報が必要
                                        var tNotify = new TRAYNOTIFY();
                                        var tNotify2 = new TRAYNOTIFY();
                                        //共有メモリ確保
                                        var ptNotify = Marshal.AllocCoTaskMem(Marshal.SizeOf(tNotify));
                                        if (ptNotify.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
                                        try
                                        {
                                            Marshal.StructureToPtr(tNotify, ptNotify, true); //初期化
                                            //lParamのメモリを読込
                                            ReadProcessMemory(
                                                hProc,
                                                tbButtonInfoLocal2.lParam,
                                                ptNotify,
                                                (IntPtr)Marshal.SizeOf(tNotify),
                                                out dwBytes);
                                            //構造体へ変換
                                            tNotify2 = (TRAYNOTIFY)
                                                            Marshal.PtrToStructure(
                                                                ptNotify,
                                                                typeof(TRAYNOTIFY));
                                        }
                                        finally
                                        {
                                            Marshal.FreeCoTaskMem(ptNotify); //共有メモリ解放
                                        }
                                        //クリックするためには通知領域がアクティブでなければならない
                                        SetForegroundWindow(tNotify2.hWnd);
                                        //左クリック
                                        PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, (IntPtr)tNotify2.uID, (IntPtr)PM_Message.WM_LBUTTONDOWN);
                                        PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, (IntPtr)tNotify2.uID, (IntPtr)PM_Message.WM_LBUTTONUP);
                                        return true;
                                    }
                                }
                                return false;    //該当なし
                            }
                            finally
                            {
                                VirtualFreeEx(hProc, pszSysTitle, (IntPtr)titleSize, MemoryFreeTypes.Release);   //メモリ解放
                            }
                        }
                        finally
                        {
                            Marshal.FreeCoTaskMem(pszTitle);     //共有メモリ解放
                        }
                    }
                    finally
                    {
                        VirtualFreeEx(hProc, ptbSysInfo, (IntPtr)Marshal.SizeOf(tbButtonInfoLocal), MemoryFreeTypes.Release);    //メモリ解放
                    }
                }
                finally
                {
                    VirtualFreeEx(hProc, ptbSysButton, (IntPtr)Marshal.SizeOf(tbButtonLocal), MemoryFreeTypes.Release);      //メモリ解放
                }
            }
            finally
            {
                CloseHandle(hProc);  //Explorerのプロセス閉じる
            }
        }
Exemplo n.º 8
0
 public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TBBUTTONINFO lParam);
Exemplo n.º 9
0
 public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TBBUTTONINFO lParam);
Exemplo n.º 10
0
        //タスクトレイアイコンのクリック処理
        public static bool ClickTasktrayIcon(string tooltip)
        {
            const string TRAY_WINDOW       = "Shell_TrayWnd";
            const string TRAY_NOTIFYWINDOW = "TrayNotifyWnd";
            const string TRAY_PAGER        = "SysPager";
            const string TOOLBAR_CONTROL   = "ToolbarWindow32";
            //タスクバーのハンドル取得
            var taskbarWin = FindWindow(TRAY_WINDOW, null);

            if (taskbarWin.Equals(IntPtr.Zero))
            {
                return(false);
            }
            //通知領域のハンドル取得
            var trayWin = FindWindowEx(taskbarWin, IntPtr.Zero, TRAY_NOTIFYWINDOW, null);

            if (trayWin.Equals(IntPtr.Zero))
            {
                return(false);
            }
            //SysPagerの有無確認。(XP/2000はSysPagerあり)
            var tempWin = FindWindowEx(trayWin, IntPtr.Zero, TRAY_PAGER, null);

            if (tempWin.Equals(IntPtr.Zero))
            {
                tempWin = trayWin;
            }
            //タスクトレイがツールバーで出来ているか確認
            // → ツールバーでなければ終了
            var toolWin = FindWindowEx(tempWin, IntPtr.Zero, TOOLBAR_CONTROL, null);

            if (toolWin.Equals(IntPtr.Zero))
            {
                return(false);
            }
            //タスクトレイのプロセス(Explorer)を取得し、外部から参照するために開く
            int expPid = 0;

            GetWindowThreadProcessId(toolWin, out expPid);
            var hProc = OpenProcess(ProcessAccess.VMOperation | ProcessAccess.VMRead | ProcessAccess.VMWrite, false, expPid);

            if (hProc.Equals(IntPtr.Zero))
            {
                return(false);
            }

            //プロセスを閉じるためにtry-finally
            try
            {
                var tbButtonLocal = new TBBUTTON();   //本プロセス内のタスクバーボタン情報作成(サイズ特定でのみ使用)
                //Explorer内のタスクバーボタン格納メモリ確保
                var ptbSysButton = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)Marshal.SizeOf(tbButtonLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
                if (ptbSysButton.Equals(IntPtr.Zero))
                {
                    return(false);                                  //メモリ確保失敗
                }
                try
                {
                    var tbButtonInfoLocal = new TBBUTTONINFO();   //本プロセス内ツールバーボタン詳細情報作成
                    //Explorer内のタスクバーボタン詳細情報格納メモリ確保
                    var ptbSysInfo = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)Marshal.SizeOf(tbButtonInfoLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
                    if (ptbSysInfo.Equals(IntPtr.Zero))
                    {
                        return(false);                                //メモリ確保失敗
                    }
                    try
                    {
                        const int titleSize = 256; //Tooltip文字列長
                        var       title     = "";  //Tooltip文字列
                        //共有メモリにTooltip読込メモリ確保
                        var pszTitle = Marshal.AllocCoTaskMem(titleSize);
                        if (pszTitle.Equals(IntPtr.Zero))
                        {
                            return(false);                              //メモリ確保失敗
                        }
                        try
                        {
                            //Explorer内にTooltip読込メモリ確保
                            var pszSysTitle = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)titleSize, AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
                            if (pszSysTitle.Equals(IntPtr.Zero))
                            {
                                return(false);                                 //メモリ確保失敗
                            }
                            try
                            {
                                //通知領域ボタン数取得
                                var iCount = (int)SendMessage(toolWin, (int)Sm_Message.TB_BUTTONCOUNT, new IntPtr(0), new IntPtr(0));
                                //左から順に情報取得
                                for (var i = 0; i < iCount; i++)
                                {
                                    var dwBytes            = 0;                  //読み書きバイト数
                                    var tbButtonLocal2     = new TBBUTTON();     //ボタン情報
                                    var tbButtonInfoLocal2 = new TBBUTTONINFO(); //ボタン詳細情報
                                    //共有メモリにボタン情報読込メモリ確保
                                    var ptrLocal = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonLocal));
                                    if (ptrLocal.Equals(IntPtr.Zero))
                                    {
                                        return(false);                              //メモリ確保失敗
                                    }
                                    try
                                    {
                                        Marshal.StructureToPtr(tbButtonLocal, ptrLocal, true);   //共有メモリ初期化
                                        //ボタン情報取得(idCommandを取得するため)
                                        SendMessage(
                                            toolWin,
                                            (int)Sm_Message.TB_GETBUTTON,
                                            new IntPtr(i),
                                            ptbSysButton);
                                        //Explorer内のメモリを共有メモリに読み込み
                                        ReadProcessMemory(
                                            hProc,
                                            ptbSysButton,
                                            ptrLocal,
                                            (IntPtr)Marshal.SizeOf(tbButtonLocal),
                                            out dwBytes);
                                        //共有メモリの内容を構造体に変換
                                        tbButtonLocal2 = (TBBUTTON)Marshal.PtrToStructure(
                                            ptrLocal,
                                            typeof(TBBUTTON));
                                    }
                                    finally
                                    {
                                        Marshal.FreeCoTaskMem(ptrLocal); //共有メモリ解放
                                    }

                                    //ボタン詳細情報を取得するためのマスク等を設定
                                    tbButtonInfoLocal.cbSize  = Marshal.SizeOf(tbButtonInfoLocal);
                                    tbButtonInfoLocal.dwMask  = (int)(ToolbarButtonMask.TBIF_COMMAND | ToolbarButtonMask.TBIF_LPARAM | ToolbarButtonMask.TBIF_TEXT);
                                    tbButtonInfoLocal.pszText = pszSysTitle;     //Tooltip書き込み先領域
                                    tbButtonInfoLocal.cchText = titleSize;
                                    //マスク設定等をExplorerのメモリへ書き込み
                                    WriteProcessMemory(
                                        hProc,
                                        ptbSysInfo,
                                        ref tbButtonInfoLocal,
                                        (IntPtr)Marshal.SizeOf(tbButtonInfoLocal),
                                        out dwBytes);
                                    //ボタン詳細情報取得
                                    SendMessage(
                                        toolWin,
                                        (int)Sm_Message.TB_GETBUTTONINFO,
                                        tbButtonLocal2.idCommand,
                                        ptbSysInfo);
                                    //共有メモリにボタン詳細情報を読み込む領域確保
                                    var ptrInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonInfoLocal));
                                    if (ptrInfo.Equals(IntPtr.Zero))
                                    {
                                        return(false);                             //共有メモリ確保失敗
                                    }
                                    try
                                    {
                                        Marshal.StructureToPtr(tbButtonInfoLocal, ptrInfo, true);    //共有メモリ初期化
                                        //Explorer内のメモリを共有メモリに読み込み
                                        ReadProcessMemory(
                                            hProc,
                                            ptbSysInfo,
                                            ptrInfo,
                                            (IntPtr)Marshal.SizeOf(tbButtonInfoLocal),
                                            out dwBytes);
                                        //共有メモリの内容を構造体に変換
                                        tbButtonInfoLocal2 = (TBBUTTONINFO)Marshal.PtrToStructure(
                                            ptrInfo,
                                            typeof(TBBUTTONINFO));
                                    }
                                    finally
                                    {
                                        Marshal.FreeCoTaskMem(ptrInfo);  //共有メモリ解放
                                    }
                                    //Tooltipの内容をExplorer内のメモリから共有メモリへ読込
                                    ReadProcessMemory(hProc, pszSysTitle, pszTitle, (IntPtr)titleSize, out dwBytes);
                                    //ローカル変数へ変換
                                    title = Marshal.PtrToStringAnsi(pszTitle, titleSize);

                                    //Tooltipが指定文字列を含んでいればクリック
                                    if (title.Contains(tooltip))
                                    {
                                        //PostMessageでクリックを送るために、ボタン詳細情報のlParamでポイントされているTRAYNOTIFY情報が必要
                                        var tNotify  = new TRAYNOTIFY();
                                        var tNotify2 = new TRAYNOTIFY();
                                        //共有メモリ確保
                                        var ptNotify = Marshal.AllocCoTaskMem(Marshal.SizeOf(tNotify));
                                        if (ptNotify.Equals(IntPtr.Zero))
                                        {
                                            return(false);                              //メモリ確保失敗
                                        }
                                        try
                                        {
                                            Marshal.StructureToPtr(tNotify, ptNotify, true); //初期化
                                            //lParamのメモリを読込
                                            ReadProcessMemory(
                                                hProc,
                                                tbButtonInfoLocal2.lParam,
                                                ptNotify,
                                                (IntPtr)Marshal.SizeOf(tNotify),
                                                out dwBytes);
                                            //構造体へ変換
                                            tNotify2 = (TRAYNOTIFY)
                                                       Marshal.PtrToStructure(
                                                ptNotify,
                                                typeof(TRAYNOTIFY));
                                        }
                                        finally
                                        {
                                            Marshal.FreeCoTaskMem(ptNotify); //共有メモリ解放
                                        }
                                        //クリックするためには通知領域がアクティブでなければならない
                                        SetForegroundWindow(tNotify2.hWnd);
                                        //左クリック
                                        PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, (IntPtr)tNotify2.uID, (IntPtr)PM_Message.WM_LBUTTONDOWN);
                                        PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, (IntPtr)tNotify2.uID, (IntPtr)PM_Message.WM_LBUTTONUP);
                                        return(true);
                                    }
                                }
                                return(false);    //該当なし
                            }
                            finally
                            {
                                VirtualFreeEx(hProc, pszSysTitle, (IntPtr)titleSize, MemoryFreeTypes.Release);   //メモリ解放
                            }
                        }
                        finally
                        {
                            Marshal.FreeCoTaskMem(pszTitle);     //共有メモリ解放
                        }
                    }
                    finally
                    {
                        VirtualFreeEx(hProc, ptbSysInfo, (IntPtr)Marshal.SizeOf(tbButtonInfoLocal), MemoryFreeTypes.Release);    //メモリ解放
                    }
                }
                finally
                {
                    VirtualFreeEx(hProc, ptbSysButton, (IntPtr)Marshal.SizeOf(tbButtonLocal), MemoryFreeTypes.Release);      //メモリ解放
                }
            }
            finally
            {
                CloseHandle(hProc);  //Explorerのプロセス閉じる
            }
        }
Exemplo n.º 11
0
 private static extern bool WriteProcessMemory(
     IntPtr hProcess,
     IntPtr lpBaseAddress,
     ref TBBUTTONINFO lpBuffer,
     IntPtr nSize,
     out int lpNumberOfBytesWritten);
Exemplo n.º 12
0
		// TODO-Linux: Implement if needed
		public static int SendMessage(IntPtr hWnd, int msg, int wParam, ref TBBUTTONINFO lParam)
		{
			Console.WriteLine("Warning using unimplemented method SendMessage");
			return 0;
		}