/// <summary> /// 获取托盘图标详细信息。 /// </summary> /// <returns></returns> public static TrayItemData[] GetTrayWndDetail() { System.Collections.Generic.SortedList <string, TrayItemData> stlTrayItems = new System.Collections.Generic.SortedList <string, TrayItemData>(); try { Thinksea.Windows.Win32API.TBBUTTON tbButtonInfo = new Thinksea.Windows.Win32API.TBBUTTON(); System.IntPtr hTrayWnd = System.IntPtr.Zero; System.IntPtr hTrayProcess = System.IntPtr.Zero; int iTrayProcessID = 0; int iAllocBaseAddress = 0; int iRet = 0; int iTrayItemCount = 0; hTrayWnd = GetTrayWnd(); Thinksea.Windows.Win32API.User32.GetWindowThreadProcessId(hTrayWnd, ref iTrayProcessID); //hTrayProcess = Thinksea.Windows.Win32API.Kernel32.OpenProcess( //(uint)Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_ALL_ACCESS | //(uint)Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_VM_OPERATION | //(uint)Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_VM_READ | //(uint)Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_VM_WRITE, 0, (uint)iTrayProcessID); hTrayProcess = Thinksea.Windows.Win32API.Kernel32.OpenProcess( Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_ALL_ACCESS | Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_VM_OPERATION | Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_VM_READ | Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_VM_WRITE, false, (uint)iTrayProcessID); iAllocBaseAddress = Thinksea.Windows.Win32API.Kernel32.VirtualAllocEx(hTrayProcess, 0, System.Runtime.InteropServices.Marshal.SizeOf(tbButtonInfo), (int)Thinksea.Windows.Win32API.WindowsNumber.MEM_COMMIT, (int)Thinksea.Windows.Win32API.WindowsNumber.PAGE_READWRITE); iTrayItemCount = Thinksea.Windows.Win32API.User32.SendMessage(hTrayWnd, (int)Thinksea.Windows.Win32API.WindowsNumber.TB_BUTTONCOUNT, 0, 0); for (int i = 0; i < iTrayItemCount; i++) { try { TrayItemData trayItem = new TrayItemData(); Thinksea.Windows.Win32API.TRAYDATA trayData = new Thinksea.Windows.Win32API.TRAYDATA(); int iOut = 0; int dwProcessID = 0; System.IntPtr hRelProcess = System.IntPtr.Zero; string strTrayToolTip = string.Empty; iRet = Thinksea.Windows.Win32API.User32.SendMessage(hTrayWnd, (int)Thinksea.Windows.Win32API.WindowsNumber.TB_GETBUTTON, i, iAllocBaseAddress); System.IntPtr hButtonInfo = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(tbButtonInfo)); System.IntPtr hTrayData = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(trayData)); iRet = Thinksea.Windows.Win32API.Kernel32.ReadProcessMemory(hTrayProcess, iAllocBaseAddress, hButtonInfo, System.Runtime.InteropServices.Marshal.SizeOf(tbButtonInfo), out iOut); System.Runtime.InteropServices.Marshal.PtrToStructure(hButtonInfo, tbButtonInfo); iRet = Thinksea.Windows.Win32API.Kernel32.ReadProcessMemory(hTrayProcess, tbButtonInfo.dwData, hTrayData, System.Runtime.InteropServices.Marshal.SizeOf(trayData), out iOut); System.Runtime.InteropServices.Marshal.PtrToStructure(hTrayData, trayData); byte[] bytTextData = new byte[1024]; iRet = Thinksea.Windows.Win32API.Kernel32.ReadProcessMemory(hTrayProcess, tbButtonInfo.iString, bytTextData, 1024, out iOut); strTrayToolTip = System.Text.Encoding.Unicode.GetString(bytTextData); if (!string.IsNullOrEmpty(strTrayToolTip)) { int iNullIndex = strTrayToolTip.IndexOf('\0'); strTrayToolTip = strTrayToolTip.Substring(0, iNullIndex); } Thinksea.Windows.Win32API.User32.GetWindowThreadProcessId(trayData.hwnd, ref dwProcessID); //hRelProcess = Thinksea.Windows.Win32API.Kernel32.OpenProcess((uint)Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_QUERY_INFORMATION, 0, (uint)dwProcessID); hRelProcess = Thinksea.Windows.Win32API.Kernel32.OpenProcess(Thinksea.Windows.Win32API.ProcessAccessFlags.PROCESS_QUERY_INFORMATION, false, (uint)dwProcessID); System.Text.StringBuilder sbProcImagePath = new System.Text.StringBuilder(256); if (hRelProcess != System.IntPtr.Zero) { Thinksea.Windows.Win32API.Kernel32.GetProcessImageFileName(hRelProcess, sbProcImagePath, sbProcImagePath.Capacity); } string strImageFilePath = string.Empty; if (sbProcImagePath.Length > 0) { int iDeviceIndex = sbProcImagePath.ToString().IndexOf("\\", "\\Device\\HarddiskVolume".Length); string strDevicePath = sbProcImagePath.ToString().Substring(0, iDeviceIndex); int iStartDisk = (int)'A'; while (iStartDisk <= (int)'Z') { System.Text.StringBuilder sbWindowImagePath = new System.Text.StringBuilder(256); iRet = Thinksea.Windows.Win32API.Kernel32.QueryDosDevice(((char)iStartDisk).ToString() + ":", sbWindowImagePath, sbWindowImagePath.Capacity); if (iRet != 0) { if (sbWindowImagePath.ToString() == strDevicePath) { strImageFilePath = ((char)iStartDisk).ToString() + ":" + sbProcImagePath.ToString().Replace(strDevicePath, ""); break; } } iStartDisk++; } } trayItem.dwProcessID = dwProcessID; trayItem.fsState = tbButtonInfo.fsState; trayItem.fsStyle = tbButtonInfo.fsStyle; trayItem.hIcon = trayData.hIcon; trayItem.hProcess = hRelProcess; trayItem.hWnd = trayData.hwnd; trayItem.idBitmap = tbButtonInfo.iBitmap; trayItem.idCommand = tbButtonInfo.idCommand; trayItem.lpProcImagePath = strImageFilePath; trayItem.lpTrayToolTip = strTrayToolTip; stlTrayItems[string.Format("{0:d8}", tbButtonInfo.idCommand)] = trayItem; } catch { continue; } } Thinksea.Windows.Win32API.Kernel32.VirtualFreeEx(hTrayProcess, iAllocBaseAddress, System.Runtime.InteropServices.Marshal.SizeOf(tbButtonInfo), (int)Thinksea.Windows.Win32API.WindowsNumber.MEM_RELEASE); Thinksea.Windows.Win32API.Kernel32.CloseHandle(hTrayProcess); TrayItemData[] trayItems = new TrayItemData[stlTrayItems.Count]; stlTrayItems.Values.CopyTo(trayItems, 0); return(trayItems); } catch (System.Runtime.InteropServices.SEHException ex) { throw ex; } catch (System.Exception ex) { throw ex; } }
public static TrayItemData[] GetTrayWndDetail() { SortedList <string, TrayItemData> stlTrayItems = new SortedList <string, TrayItemData>(); try { Win32.TBBUTTON tbButtonInfo = new Win32.TBBUTTON(); IntPtr hTrayWnd = IntPtr.Zero; IntPtr hTrayProcess = IntPtr.Zero; int iTrayProcessID = 0; int iAllocBaseAddress = 0; int iRet = 0; int iTrayItemCount = 0; hTrayWnd = GetTrayWnd(); Win32.GetWindowThreadProcessId(hTrayWnd, ref iTrayProcessID); hTrayProcess = Win32.OpenProcess( (uint)Win32.ProcessAccessFlags.All | (uint)Win32.ProcessAccessFlags.VMOperation | (uint)Win32.ProcessAccessFlags.VMRead | (uint)Win32.ProcessAccessFlags.VMWrite, 0, (uint)iTrayProcessID); iAllocBaseAddress = Win32.VirtualAllocEx(hTrayProcess, 0, Marshal.SizeOf(tbButtonInfo), Win32.MEM_COMMIT, Win32.PAGE_READWRITE); iTrayItemCount = Win32.SendMessage(hTrayWnd, Win32.TB_BUTTONCOUNT, 0, 0); for (int i = 0; i < iTrayItemCount; i++) { try { TrayItemData trayItem = new TrayItemData(); Win32.TRAYDATA trayData = new Win32.TRAYDATA(); int iOut = 0; int dwProcessID = 0; IntPtr hRelProcess = IntPtr.Zero; string strTrayToolTip = string.Empty; iRet = Win32.SendMessage(hTrayWnd, Win32.TB_GETBUTTON, i, iAllocBaseAddress); IntPtr hButtonInfo = Marshal.AllocHGlobal(Marshal.SizeOf(tbButtonInfo)); IntPtr hTrayData = Marshal.AllocHGlobal(Marshal.SizeOf(trayData)); iRet = Win32.ReadProcessMemory(hTrayProcess, iAllocBaseAddress, hButtonInfo, Marshal.SizeOf(tbButtonInfo), out iOut); Marshal.PtrToStructure(hButtonInfo, tbButtonInfo); iRet = Win32.ReadProcessMemory(hTrayProcess, tbButtonInfo.dwData, hTrayData, Marshal.SizeOf(trayData), out iOut); Marshal.PtrToStructure(hTrayData, trayData); byte[] bytTextData = new byte[1024]; iRet = Win32.ReadProcessMemory(hTrayProcess, tbButtonInfo.iString, bytTextData, 1024, out iOut); strTrayToolTip = Encoding.Unicode.GetString(bytTextData); if (!string.IsNullOrEmpty(strTrayToolTip)) { int iNullIndex = strTrayToolTip.IndexOf('\0'); strTrayToolTip = strTrayToolTip.Substring(0, iNullIndex); } Win32.GetWindowThreadProcessId(trayData.hwnd, ref dwProcessID); hRelProcess = Win32.OpenProcess((uint)Win32.ProcessAccessFlags.QueryInformation, 0, (uint)dwProcessID); StringBuilder sbProcImagePath = new StringBuilder(256); if (hRelProcess != IntPtr.Zero) { Win32.GetProcessImageFileName(hRelProcess, sbProcImagePath, sbProcImagePath.Capacity); } string strImageFilePath = string.Empty; if (sbProcImagePath.Length > 0) { int iDeviceIndex = sbProcImagePath.ToString().IndexOf("\\", "\\Device\\HarddiskVolume".Length); string strDevicePath = sbProcImagePath.ToString().Substring(0, iDeviceIndex); int iStartDisk = (int)'A'; while (iStartDisk <= (int)'Z') { StringBuilder sbWindowImagePath = new StringBuilder(256); iRet = Win32.QueryDosDevice(((char)iStartDisk).ToString() + ":", sbWindowImagePath, sbWindowImagePath.Capacity); if (iRet != 0) { if (sbWindowImagePath.ToString() == strDevicePath) { strImageFilePath = ((char)iStartDisk).ToString() + ":" + sbProcImagePath.ToString().Replace(strDevicePath, ""); break; } } iStartDisk++; } } trayItem.dwProcessID = dwProcessID; trayItem.fsState = tbButtonInfo.fsState; trayItem.fsStyle = tbButtonInfo.fsStyle; trayItem.hIcon = trayData.hIcon; trayItem.hProcess = hRelProcess; trayItem.hWnd = trayData.hwnd; trayItem.idBitmap = tbButtonInfo.iBitmap; trayItem.idCommand = tbButtonInfo.idCommand; trayItem.lpProcImagePath = strImageFilePath; trayItem.lpTrayToolTip = strTrayToolTip; stlTrayItems[string.Format("{0:d8}", tbButtonInfo.idCommand)] = trayItem; } catch { continue; } } Win32.VirtualFreeEx(hTrayProcess, iAllocBaseAddress, Marshal.SizeOf(tbButtonInfo), Win32.MEM_RELEASE); Win32.CloseHandle(hTrayProcess); TrayItemData[] trayItems = new TrayItemData[stlTrayItems.Count]; stlTrayItems.Values.CopyTo(trayItems, 0); return(trayItems); } catch (SEHException ex) { throw ex; } catch (Exception ex) { throw ex; } }