Skip to content

Commit f32097a

Browse files
author
Light
authored
Merge pull request #1 from AlexanderPro/master
upeated 1.7.4 from AlexanderPro
2 parents 6317145 + c1fd68b commit f32097a

File tree

13 files changed

+365
-35
lines changed

13 files changed

+365
-35
lines changed

SmartSystemMenu/EnumWindows.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ public static IList<Window> EnumProcessWindows(int processId, IntPtr[] filterHan
2929
_filterHandles = filterHandles ?? new IntPtr[0];
3030
_windows = new List<Window>();
3131
_menuItems = menuItems;
32-
foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)
32+
var process = SystemUtils.GetProcessByIdSafely(processId);
33+
if (process != null)
3334
{
34-
NativeMethods.EnumThreadWindows(thread.Id, EnumWindowCallback, 0);
35+
foreach (ProcessThread thread in process.Threads)
36+
{
37+
NativeMethods.EnumThreadWindows(thread.Id, EnumWindowCallback, 0);
38+
}
3539
}
3640
return _windows;
3741
}

SmartSystemMenu/Forms/AboutForm.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ private void CloseClick(object sender, EventArgs e)
2323

2424
private void LinkClick(object sender, EventArgs e)
2525
{
26-
System.Diagnostics.Process.Start(URL);
26+
try
27+
{
28+
SystemUtils.RunAsDesktopUser(SystemUtils.GetDefaultBrowserModuleName(), URL);
29+
}
30+
catch
31+
{
32+
}
2733
}
2834

2935
private void KeyDownClick(object sender, KeyEventArgs e)

SmartSystemMenu/Forms/InfoForm.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ private void InitializeControls()
2424
lblRectangleValue.Text = string.Format("({0},{1}) - ({2},{3}) - {4}x{5}", _window.Size.Left, _window.Size.Top, _window.Size.Right, _window.Size.Bottom, _window.Size.Width, _window.Size.Height);
2525
lblProcessIdValue.Text = string.Format("{0:X8} ({0})", _window.ProcessId);
2626
lblThreadIdValue.Text = string.Format("{0:X8} ({0})", _window.ThreadId);
27-
var process = Process.GetProcessById(_window.ProcessId);
28-
txtModuleNameValue.Text = Path.GetFileName(process.MainModule.FileName);
29-
txtModulePathValue.Text = process.MainModule.FileName;
27+
var process = SystemUtils.GetProcessByIdSafely(_window.ProcessId);
28+
if (process != null)
29+
{
30+
txtModuleNameValue.Text = Path.GetFileName(process.MainModule.FileName);
31+
txtModulePathValue.Text = process.MainModule.FileName;
32+
}
3033
}
3134

3235
private void FormKeyDown(object sender, KeyEventArgs e)

SmartSystemMenu/Forms/MainForm.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected override void OnLoad(EventArgs e)
7272
return;
7373
}
7474
}
75-
_systemTrayMenu = new SystemTrayMenu();
75+
_systemTrayMenu = new SystemTrayMenu(_settings.ShowSystemTrayIcon);
7676
_systemTrayMenu.MenuItemAutoStart.Click += MenuItemAutoStartClick;
7777
_systemTrayMenu.MenuItemSettings.Click += MenuItemSettingsClick;
7878
_systemTrayMenu.MenuItemAbout.Click += MenuItemAboutClick;
@@ -437,8 +437,7 @@ private void WindowGetMsg(object sender, WndProcEventArgs e)
437437
{
438438
try
439439
{
440-
var process = Process.Start("explorer.exe", "/select, " + window.Process.GetMainModuleFileName());
441-
Window.ForceForegroundWindow(process.MainWindowHandle);
440+
SystemUtils.RunAsDesktopUser("explorer.exe", "/select, " + window.Process.GetMainModuleFileName());
442441
}
443442
catch
444443
{
@@ -660,11 +659,13 @@ private void WindowGetMsg(object sender, WndProcEventArgs e)
660659
{
661660
if (lowOrder - SystemMenu.SC_START_PROGRAM == i)
662661
{
663-
var processStartInfo = new ProcessStartInfo();
664-
processStartInfo.FileName = _settings.MenuItems.StartProgramItems[i].FileName;
665-
processStartInfo.WorkingDirectory = Path.GetDirectoryName(_settings.MenuItems.StartProgramItems[i].FileName);
666-
processStartInfo.Arguments = _settings.MenuItems.StartProgramItems[i].Arguments;
667-
Process.Start(processStartInfo);
662+
try
663+
{
664+
SystemUtils.RunAsDesktopUser(_settings.MenuItems.StartProgramItems[i].FileName, _settings.MenuItems.StartProgramItems[i].Arguments);
665+
}
666+
catch
667+
{
668+
}
668669
break;
669670
}
670671
}

SmartSystemMenu/NativeMethods.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,29 @@ public static IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex)
227227

228228
[DllImport("user32.dll")]
229229
public static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
230+
231+
[DllImport("kernel32.dll")]
232+
public static extern IntPtr GetCurrentProcess();
233+
234+
[DllImport("advapi32.dll")]
235+
public static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
236+
237+
[DllImport("advapi32.dll")]
238+
public static extern bool LookupPrivilegeValue(string host, string name, ref LUID pluid);
239+
240+
[DllImport("advapi32.dll")]
241+
public static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TOKEN_PRIVILEGES newst, int len, IntPtr prev, IntPtr relen);
242+
243+
[DllImport("user32.dll")]
244+
public static extern IntPtr GetShellWindow();
245+
246+
[DllImport("kernel32.dll")]
247+
public static extern IntPtr OpenProcess(ProcessAccessFlags processAccess, bool bInheritHandle, int processId);
248+
249+
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
250+
public static extern bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, SECURITY_IMPERSONATION_LEVEL impersonationLevel, TOKEN_TYPE tokenType, out IntPtr phNewToken);
251+
252+
[DllImport("advapi32", CharSet = CharSet.Unicode)]
253+
public static extern bool CreateProcessWithTokenW(IntPtr hToken, int dwLogonFlags, string lpApplicationName, string lpCommandLine, int dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
230254
}
231255
}

SmartSystemMenu/NativeTypes.cs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Text;
32
using System.Runtime.InteropServices;
43

54
namespace SmartSystemMenu
@@ -175,4 +174,90 @@ public void Init()
175174
cbSize = (uint)Marshal.SizeOf(this);
176175
}
177176
}
177+
178+
struct TOKEN_PRIVILEGES
179+
{
180+
public uint PrivilegeCount;
181+
182+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
183+
public LUID_AND_ATTRIBUTES[] Privileges;
184+
}
185+
186+
[StructLayout(LayoutKind.Sequential, Pack = 4)]
187+
struct LUID_AND_ATTRIBUTES
188+
{
189+
public LUID Luid;
190+
public uint Attributes;
191+
}
192+
193+
[StructLayout(LayoutKind.Sequential)]
194+
struct LUID
195+
{
196+
public uint LowPart;
197+
public int HighPart;
198+
}
199+
200+
[Flags]
201+
enum ProcessAccessFlags : uint
202+
{
203+
All = 0x001F0FFF,
204+
Terminate = 0x00000001,
205+
CreateThread = 0x00000002,
206+
VirtualMemoryOperation = 0x00000008,
207+
VirtualMemoryRead = 0x00000010,
208+
VirtualMemoryWrite = 0x00000020,
209+
DuplicateHandle = 0x00000040,
210+
CreateProcess = 0x000000080,
211+
SetQuota = 0x00000100,
212+
SetInformation = 0x00000200,
213+
QueryInformation = 0x00000400,
214+
QueryLimitedInformation = 0x00001000,
215+
Synchronize = 0x00100000
216+
}
217+
218+
enum SECURITY_IMPERSONATION_LEVEL
219+
{
220+
SecurityAnonymous,
221+
SecurityIdentification,
222+
SecurityImpersonation,
223+
SecurityDelegation
224+
}
225+
226+
enum TOKEN_TYPE
227+
{
228+
TokenPrimary = 1,
229+
TokenImpersonation
230+
}
231+
232+
[StructLayout(LayoutKind.Sequential)]
233+
struct PROCESS_INFORMATION
234+
{
235+
public IntPtr hProcess;
236+
public IntPtr hThread;
237+
public int dwProcessId;
238+
public int dwThreadId;
239+
}
240+
241+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
242+
struct STARTUPINFO
243+
{
244+
public int cb;
245+
public string lpReserved;
246+
public string lpDesktop;
247+
public string lpTitle;
248+
public int dwX;
249+
public int dwY;
250+
public int dwXSize;
251+
public int dwYSize;
252+
public int dwXCountChars;
253+
public int dwYCountChars;
254+
public int dwFillAttribute;
255+
public int dwFlags;
256+
public short wShowWindow;
257+
public short cbReserved2;
258+
public IntPtr lpReserved2;
259+
public IntPtr hStdInput;
260+
public IntPtr hStdOutput;
261+
public IntPtr hStdError;
262+
}
178263
}

SmartSystemMenu/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.7.2")]
35-
[assembly: AssemblyFileVersion("1.7.2")]
34+
[assembly: AssemblyVersion("1.7.4")]
35+
[assembly: AssemblyFileVersion("1.7.4")]

SmartSystemMenu/Settings/SmartSystemMenuSettings.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ public class SmartSystemMenuSettings : ICloneable
1212
{
1313
public IList<string> ProcessExclusions { get; private set; }
1414

15-
public MenuItems MenuItems { get; set; }
15+
public MenuItems MenuItems { get; private set; }
16+
17+
public bool ShowSystemTrayIcon { get; private set; }
1618

1719
public SmartSystemMenuSettings()
1820
{
1921
ProcessExclusions = new List<string>();
2022
MenuItems = new MenuItems();
23+
ShowSystemTrayIcon = true;
2124
}
2225

2326
public object Clone()
@@ -130,6 +133,12 @@ public static SmartSystemMenuSettings Read(string fileName)
130133
})
131134
.ToList();
132135

136+
var systemTrayIconElement = document.XPathSelectElement("/smartSystemMenu/systemTrayIcon");
137+
if (systemTrayIconElement != null && systemTrayIconElement.Attribute("show") != null && systemTrayIconElement.Attribute("show").Value != null && systemTrayIconElement.Attribute("show").Value.ToLower() == "false")
138+
{
139+
settings.ShowSystemTrayIcon = false;
140+
}
141+
133142
return settings;
134143
}
135144

@@ -142,7 +151,10 @@ public static void Save(string fileName, SmartSystemMenuSettings settings)
142151
new XElement("startProgramItem", settings.MenuItems.StartProgramItems.Select(x => new XElement("item",
143152
new XAttribute("title", x.Title),
144153
new XAttribute("fileName", x.FileName),
145-
new XAttribute("arguments", x.Arguments)))))));
154+
new XAttribute("arguments", x.Arguments))))),
155+
new XElement("systemTrayIcon",
156+
new XAttribute("show", settings.ShowSystemTrayIcon.ToString().ToLower())
157+
)));
146158
Save(fileName, document);
147159
}
148160

SmartSystemMenu/SmartSystemMenu.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<item title="Calculator" fileName="calc.exe" arguments="" />
1515
<item title="Paint" fileName="mspaint.exe" arguments="" />
1616
<item title="Explorer" fileName="explorer.exe" arguments="" />
17-
<item title="Registry" fileName="regedit.exe" arguments="" />
1817
</startProgramItem>
1918
</menuItems>
19+
<systemTrayIcon show="true" />
2020
</smartSystemMenu>
4.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)