public string DoRightClickOnSysTray(string Process_Name, int processIndex) { try { if (Marshal.SizeOf(typeof(IntPtr)) == 8) { //if (Environment.GetEnvironmentVariable("ProgramFiles(x86)") != null) { TBBUTTON_64 tbButtonData = new TBBUTTON_64(); if (GetNotifyIcon_X64(Process_Name, tbButtonData, processIndex)) { return("Done"); } else { return("Process not found"); } } else { TBBUTTON_32 tbButtonData = new TBBUTTON_32(); if (GetNotifyIcon_X86(Process_Name, tbButtonData, processIndex)) { return("Done"); } else { return("Process not found"); } } } catch (IndexOutOfRangeException) { return("Process not found"); } }
private bool GetNotifyIcon_X86(string HP_Tray, TBBUTTON_32 tbButtonData, int processIndex) { Process[] Array_P = Process.GetProcessesByName(HP_Tray); ProcessThreadCollection ptc = Array_P[processIndex].Threads; int[] MSNID = new int[ptc.Count]; int index = 0; foreach (ProcessThread pt in ptc) { MSNID[index] = pt.Id; index++; } //int hWndNotifyIconHandle = MSNID[0]; IntPtr ptrTaskbar = FindWindow("Shell_TrayWnd", null); IntPtr ptrStartBtn = FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "TrayNotifyWnd", null); IntPtr traysyspager = FindWindowEx(new HandleRef(this, ptrStartBtn), new HandleRef(this, IntPtr.Zero), "syspager", null); IntPtr hWndTray = FindWindowEx(new HandleRef(this, traysyspager), new HandleRef(this, IntPtr.Zero), "toolbarwindow32", null); int dwTrayProcessID = -1; GetWindowThreadProcessId(hWndTray, out dwTrayProcessID); Process TrayP = Process.GetProcessById(dwTrayProcessID); if (dwTrayProcessID <= 0) { return(false); } IntPtr hTrayProc = OpenProcess(PROCESS_ALL_ACCESS, 0, dwTrayProcessID); if (hTrayProc == IntPtr.Zero) { return(false); } int intTrayButtonCount = (int)SendMessage(hWndTray, TB_BUTTONCOUNT, 0, 0); if (intTrayButtonCount <= 0) { return(false); } IntPtr lpData = VirtualAllocEx(hTrayProc, IntPtr.Zero, Marshal.SizeOf(tbButtonData.GetType()), MEM_COMMIT, PAGE_READWRITE); if (lpData == IntPtr.Zero) { CloseHandle(hTrayProc); return(false); } bool bIconFound = false; for (int intButton = 0; intButton < intTrayButtonCount; intButton++) { // Get button data int dwBytesRead = -1; byte[] byteBuffer = new byte[Marshal.SizeOf(tbButtonData.GetType())]; SendMessage(hWndTray, TB_GETBUTTON, intButton, lpData); ReadProcessMemory(hTrayProc, lpData, byteBuffer, Marshal.SizeOf(tbButtonData.GetType()), out dwBytesRead); if (dwBytesRead < Marshal.SizeOf(tbButtonData.GetType())) { continue; } IntPtr ptrOut = Marshal.AllocHGlobal(Marshal.SizeOf(tbButtonData.GetType())); Marshal.Copy(byteBuffer, 0, ptrOut, byteBuffer.Length); tbButtonData = (TBBUTTON_32)Marshal.PtrToStructure(ptrOut, typeof(TBBUTTON_32)); // Get extra button data from .dwData base address dwBytesRead = -1; int[] dwExtraData = new int[2] { 0, 0 }; byte[] byteBuffer2 = new byte[Marshal.SizeOf(dwExtraData[0].GetType()) * dwExtraData.Length]; int intRetval2 = ReadProcessMemory(hTrayProc, (IntPtr)tbButtonData.dwData, byteBuffer2, (Marshal.SizeOf(dwExtraData[0].GetType()) * dwExtraData.Length), out dwBytesRead); if (dwBytesRead < (Marshal.SizeOf(dwExtraData[0]) * dwExtraData.Length)) { continue; } dwExtraData[1] = BitConverter.ToInt32(byteBuffer2, (byteBuffer2.Length / 2)); Array.Resize(ref byteBuffer2, (byteBuffer2.Length / 2)); dwExtraData[0] = BitConverter.ToInt32(byteBuffer2, 0); IntPtr hWndCurrentIconHandle = (IntPtr)dwExtraData[0]; int intCurrentIconID = (int)dwExtraData[1]; // Spin if this is not the target tray icon int dwCurrentThreadIconId = -1; dwCurrentThreadIconId = GetWindowThreadProcessId(hWndCurrentIconHandle, out dwCurrentThreadIconId); //if (dwCurrentThreadIconId != hWndNotifyIconHandle) bool found = false; foreach (int i in MSNID) { if (dwCurrentThreadIconId == i) { found = true; break; } } if (!found) { continue; } // Get rectangle of tray icon dwBytesRead = -1; RECT rectNotifyIcon = new RECT(0, 0, 0, 0); byte[] byteBuffer3 = new byte[Marshal.SizeOf(rectNotifyIcon.GetType())]; SendMessage(hWndTray, TB_GETITEMRECT, intButton, lpData); ReadProcessMemory(hTrayProc, lpData, byteBuffer3, Marshal.SizeOf(rectNotifyIcon.GetType()), out dwBytesRead); if (dwBytesRead < Marshal.SizeOf(rectNotifyIcon.GetType())) { continue; } IntPtr ptrOut2 = Marshal.AllocHGlobal(Marshal.SizeOf(rectNotifyIcon.GetType())); Marshal.Copy(byteBuffer3, 0, ptrOut2, byteBuffer3.Length); rectNotifyIcon = (RECT)Marshal.PtrToStructure(ptrOut2, typeof(RECT)); MapWindowPoints(hWndTray, IntPtr.Zero, ref rectNotifyIcon, 2); endPosition.X = Convert.ToInt32((rectNotifyIcon.Left + rectNotifyIcon.Right) / 2); endPosition.Y = Convert.ToInt32((rectNotifyIcon.Top + rectNotifyIcon.Bottom) / 2); SetCursorPos(endPosition.X, endPosition.Y); mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero); mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero); bIconFound = true; break; } // Free memory in parent process for system tray and close handle VirtualFreeEx(hTrayProc, lpData, 0, MEM_RELEASE); CloseHandle(hTrayProc); return(bIconFound); }
//------------------------------------------------------ // // ToolBar Control Methods that support cross process / cross bitness // //------------------------------------------------------ #region ToolBar Control Methods // This overload method is used to get Toolbar Button Item data. internal static unsafe bool GetItem(IntPtr hwnd, int index, ref NativeMethods.TBBUTTON item) { ProcessorTypes localBitness; ProcessorTypes remoteBitness; GetProcessTypes(hwnd, out localBitness, out remoteBitness); if (localBitness == remoteBitness) { fixed (NativeMethods.TBBUTTON* pItem = &item) { return XSend(hwnd, NativeMethods.TB_GETBUTTON, new IntPtr(index), new IntPtr(pItem), Marshal.SizeOf(item.GetType()), ErrorValue.Zero); } } else if (remoteBitness == ProcessorTypes.Processor32Bit) { TBBUTTON_32 item32 = new TBBUTTON_32(item); bool result = XSend(hwnd, NativeMethods.TB_GETBUTTON, new IntPtr(index), new IntPtr(&item32), Marshal.SizeOf(item32.GetType()), ErrorValue.Zero); if (result) { item = (NativeMethods.TBBUTTON)item32; } return result; } else if (remoteBitness == ProcessorTypes.Processor64Bit) { TBBUTTON_64 item64 = new TBBUTTON_64(item); bool result = XSend(hwnd, NativeMethods.TB_GETBUTTON, new IntPtr(index), new IntPtr(&item64), Marshal.SizeOf(item64.GetType()), ErrorValue.Zero); if (result) { item = (NativeMethods.TBBUTTON)item64; } return result; } return false; }