-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathExplorerTrayService.cs
More file actions
297 lines (252 loc) · 9.83 KB
/
ExplorerTrayService.cs
File metadata and controls
297 lines (252 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using ManagedShell.Common.Helpers;
using ManagedShell.Common.Logging;
using Microsoft.Win32;
using System;
using System.Runtime.InteropServices;
using static ManagedShell.Interop.NativeMethods;
namespace ManagedShell.WindowsTray
{
public class ExplorerTrayService
{
private SystrayDelegate trayDelegate;
public ExplorerTrayService()
{
}
internal void SetSystrayCallback(SystrayDelegate theDelegate)
{
trayDelegate = theDelegate;
}
internal void Run()
{
if (!EnvironmentHelper.IsAppRunningAsShell && trayDelegate != null)
{
bool autoTrayEnabled = GetAutoTrayEnabled();
if (autoTrayEnabled)
{
// we can't get tray icons that are in the hidden area, so disable that temporarily if enabled
try
{
TrayNotify trayNotify = new TrayNotify();
SetAutoTrayEnabled(trayNotify, false);
GetTrayItems();
SetAutoTrayEnabled(trayNotify, true);
Marshal.ReleaseComObject(trayNotify);
}
catch (Exception e)
{
ShellLogger.Debug($"ExplorerTrayService: Unable to get items using ITrayNotify: {e.Message}");
}
}
else
{
try
{
GetTrayItems();
}
catch (Exception e)
{
ShellLogger.Debug($"ExplorerTrayService: Unable to get items: {e.Message}");
}
}
}
}
private void GetTrayItems()
{
IntPtr toolbarHwnd = FindExplorerTrayToolbarHwnd();
if (toolbarHwnd == IntPtr.Zero)
{
return;
}
int count = GetNumTrayIcons(toolbarHwnd);
if (count < 1)
{
return;
}
GetWindowThreadProcessId(toolbarHwnd, out var processId);
IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, (int)processId);
IntPtr hBuffer = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)Marshal.SizeOf(new TBBUTTON()), AllocationType.Commit,
MemoryProtection.ReadWrite);
for (int i = 0; i < count; i++)
{
TrayItem trayItem = GetTrayItem(i, hBuffer, hProcess, toolbarHwnd);
if (trayItem.hWnd == IntPtr.Zero || !IsWindow(trayItem.hWnd))
{
ShellLogger.Debug($"ExplorerTrayService: Ignored notify icon {trayItem.szIconText} due to invalid handle");
continue;
}
SafeNotifyIconData nid = GetTrayItemIconData(trayItem);
if (trayDelegate != null)
{
if (!trayDelegate((uint)NIM.NIM_ADD, nid))
{
ShellLogger.Debug("ExplorerTrayService: Ignored notify icon message");
}
}
else
{
ShellLogger.Debug("ExplorerTrayService: trayDelegate is null");
}
}
VirtualFreeEx(hProcess, hBuffer, 0, AllocationType.Release);
CloseHandle(hProcess);
}
private IntPtr FindExplorerTrayToolbarHwnd()
{
IntPtr hwnd = FindWindow("Shell_TrayWnd", "");
if (hwnd != IntPtr.Zero)
{
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "TrayNotifyWnd", "");
if (hwnd != IntPtr.Zero)
{
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "SysPager", "");
if (hwnd != IntPtr.Zero)
{
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "ToolbarWindow32", IntPtr.Zero);
return hwnd;
}
}
}
return IntPtr.Zero;
}
private int GetNumTrayIcons(IntPtr toolbarHwnd)
{
return (int)SendMessage(toolbarHwnd, (int)TB.BUTTONCOUNT, IntPtr.Zero, IntPtr.Zero);
}
private TrayItem GetTrayItem(int i, IntPtr hBuffer, IntPtr hProcess, IntPtr toolbarHwnd)
{
TBBUTTON tbButton = new TBBUTTON();
TrayItem trayItem = new TrayItem();
IntPtr hTBButton = Marshal.AllocHGlobal(Marshal.SizeOf(tbButton));
IntPtr hTrayItem = Marshal.AllocHGlobal(Marshal.SizeOf(trayItem));
IntPtr msgSuccess = SendMessage(toolbarHwnd, (int)TB.GETBUTTON, (IntPtr)i, hBuffer);
if (ReadProcessMemory(hProcess, hBuffer, hTBButton, Marshal.SizeOf(tbButton), out _))
{
tbButton = (TBBUTTON)Marshal.PtrToStructure(hTBButton, typeof(TBBUTTON));
if (tbButton.dwData != UIntPtr.Zero)
{
if (ReadProcessMemory(hProcess, tbButton.dwData, hTrayItem, Marshal.SizeOf(trayItem), out _))
{
trayItem = (TrayItem)Marshal.PtrToStructure(hTrayItem, typeof(TrayItem));
if ((tbButton.fsState & TBSTATE_HIDDEN) != 0)
{
trayItem.dwState = 1;
}
else
{
trayItem.dwState = 0;
}
ShellLogger.Debug(
$"ExplorerTrayService: Got tray item: {trayItem.szIconText}");
}
}
}
return trayItem;
}
private SafeNotifyIconData GetTrayItemIconData(TrayItem trayItem)
{
SafeNotifyIconData nid = new SafeNotifyIconData();
nid.hWnd = trayItem.hWnd;
nid.uID = trayItem.uID;
nid.uCallbackMessage = trayItem.uCallbackMessage;
nid.szTip = trayItem.szIconText;
nid.hIcon = trayItem.hIcon;
nid.uVersion = trayItem.uVersion;
nid.guidItem = trayItem.guidItem;
nid.dwState = (int)trayItem.dwState;
nid.uFlags = NIF.GUID | NIF.MESSAGE | NIF.TIP | NIF.STATE;
if (nid.hIcon != IntPtr.Zero)
{
nid.uFlags |= NIF.ICON;
}
else
{
ShellLogger.Warning($"ExplorerTrayService: Unable to use {trayItem.szIconText} icon handle for NOTIFYICONDATA struct");
}
return nid;
}
private bool GetAutoTrayEnabled()
{
int enableAutoTray = 1;
try
{
RegistryKey explorerKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer", false);
if (explorerKey != null)
{
var enableAutoTrayValue = explorerKey.GetValue("EnableAutoTray");
if (enableAutoTrayValue != null)
{
enableAutoTray = Convert.ToInt32(enableAutoTrayValue);
}
}
}
catch (Exception e)
{
ShellLogger.Debug($"ExplorerTrayService: Unable to get EnableAutoTray setting: {e.Message}");
}
return enableAutoTray == 1;
}
private void SetAutoTrayEnabled(TrayNotify trayNotify, bool enabled)
{
try
{
if (EnvironmentHelper.IsWindows8OrBetter)
{
var trayNotifyInstance = (ITrayNotify)trayNotify;
trayNotifyInstance.EnableAutoTray(enabled);
}
else
{
var trayNotifyInstance = (ITrayNotifyLegacy)trayNotify;
trayNotifyInstance.EnableAutoTray(enabled);
}
}
catch (Exception e)
{
ShellLogger.Debug($"ExplorerTrayService: Unable to set EnableAutoTray setting: {e.Message}");
}
}
private const byte TBSTATE_HIDDEN = 8;
private enum TB : uint
{
GETBUTTON = WM.USER + 23,
BUTTONCOUNT = WM.USER + 24
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TrayItem
{
public IntPtr hWnd;
public uint uID;
public uint uCallbackMessage;
public uint dwState;
public uint uVersion;
public IntPtr hIcon;
public IntPtr uIconDemoteTimerID;
public uint dwUserPref;
public uint dwLastSoundTime;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szExeName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szIconText;
public uint uNumSeconds;
public Guid guidItem;
}
[StructLayout(LayoutKind.Sequential)]
public struct TBBUTTON
{
public int iBitmap;
public int idCommand;
[StructLayout(LayoutKind.Explicit)]
private struct TBBUTTON_U
{
[FieldOffset(0)] public byte fsState;
[FieldOffset(1)] public byte fsStyle;
[FieldOffset(0)] private IntPtr bReserved;
}
private TBBUTTON_U union;
public byte fsState { get { return union.fsState; } set { union.fsState = value; } }
public byte fsStyle { get { return union.fsStyle; } set { union.fsStyle = value; } }
public UIntPtr dwData;
public IntPtr iString;
}
}
}