Skip to content

Commit 704acb0

Browse files
committed
Added a file with process names to be excluded.
1 parent c54e16b commit 704acb0

File tree

6 files changed

+144
-47
lines changed

6 files changed

+144
-47
lines changed

Build/Build.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<SmartSystemMenuSourceFileWin64 Include="$(SmartSystemMenuProjectPath)\bin\x64\Release\SmartSystemMenu.exe" />
2020
<SmartSystemMenuDestFileWin32 Include="$(ApplicationPath)\SmartSystemMenu.exe" />
2121
<SmartSystemMenuDestFileWin64 Include="$(SmartSystemMenuProjectPath)\SmartSystemMenu64.exe" />
22+
<SmartSystemMenuProcessExclusionsSourceFile Include="$(SmartSystemMenuProjectPath)\SmartSystemMenuProcessExclusions.txt" />
23+
<SmartSystemMenuProcessExclusionsDestFile Include="$(ApplicationPath)\SmartSystemMenuProcessExclusions.txt" />
2224
<EmptyTextForFile Include="Empty File" />
2325
</ItemGroup>
2426

@@ -32,6 +34,7 @@
3234
<CallTarget Targets="Copy_Release_x64_SmartSystemMenu" />
3335
<CallTarget Targets="Build_Release_x32_SmartSystemMenu" />
3436
<CallTarget Targets="Copy_Release_x32_SmartSystemMenu" />
37+
<CallTarget Targets="CopySmartSystemMenuProcessExclusionsFile" />
3538
</Target>
3639

3740
<Target Name="Build_Release_x32_SmartSystemMenuHook">
@@ -86,9 +89,14 @@
8689
<Message Text="Copy Completed $(SmartSystemMenuProjectName) Realese x64" />
8790
</Target>
8891

89-
<Target Name="WriteEmptyTextToEmbeddedResourceInSmartSystemMenu">
92+
<Target Name="WriteEmptyTextToEmbeddedResourceInSmartSystemMenu">
9093
<Message Text="Write Empty Embedded Resource $(SmartSystemMenuProjectName)" />
9194
<WriteLinesToFile File="@(SmartSystemMenuDestFileWin64)" Lines="@(EmptyTextForFile)" Overwrite="true" />
9295
<Message Text="Write Empty Embedded Resource Completed $(SmartSystemMenuProjectName)" />
9396
</Target>
97+
98+
<Target Name="CopySmartSystemMenuProcessExclusionsFile">
99+
<Message Text="Copy $(SmartSystemMenuProcessExclusionsSourceFile) to $(SmartSystemMenuProjectName)" />
100+
<Copy SourceFiles="@(SmartSystemMenuProcessExclusionsSourceFile)" DestinationFiles="@(SmartSystemMenuProcessExclusionsDestFile)" />
101+
</Target>
94102
</Project>

SmartSystemMenu/Code/Common/Window.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ public Int32 ProcessId
100100
}
101101
}
102102

103+
public Process Process
104+
{
105+
get
106+
{
107+
return Process.GetProcessById(ProcessId);
108+
}
109+
}
110+
103111
public UInt32 ThreadId
104112
{
105113
get

SmartSystemMenu/Code/Forms/MainForm.cs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics;
88
using System.IO;
99
using System.Drawing.Imaging;
10+
using System.Text;
1011
using SmartSystemMenu.Code.Common;
1112
using SmartSystemMenu.Code.Common.Extensions;
1213
using SmartSystemMenu.Code.Hooks;
@@ -15,18 +16,47 @@ namespace SmartSystemMenu.Code.Forms
1516
{
1617
partial class MainForm : Form
1718
{
18-
private readonly String _shellWindowName = "Program Manager";
19+
private const String SHELL_WINDOW_NAME = "Program Manager";
1920
private List<Window> _windows;
2021
private GetMsgHook _getMsgHook;
2122
private ShellHook _shellHook;
2223
private CBTHook _cbtHook;
2324
private KeyboardHook _keyboardHook;
2425
private AboutForm _aboutForm;
26+
private List<String> _processExclusions;
2527

2628
#if WIN32
2729
private SystemTrayMenu _systemTrayMenu;
2830
private Process _64BitProcess;
2931
#endif
32+
33+
private List<String> ProcessExclusions
34+
{
35+
get
36+
{
37+
if (_processExclusions != null)
38+
{
39+
return _processExclusions;
40+
}
41+
42+
var proceesExclusionsFileName = Path.Combine(AssemblyUtility.AssemblyDirectory, "SmartSystemMenuProcessExclusions.txt");
43+
if (File.Exists(proceesExclusionsFileName))
44+
{
45+
_processExclusions = File
46+
.ReadAllLines(proceesExclusionsFileName, Encoding.UTF8)
47+
.Select(x => x.Trim().ToLower())
48+
.Where(x => !String.IsNullOrEmpty(x))
49+
.ToList();
50+
}
51+
else
52+
{
53+
_processExclusions = new List<String>();
54+
}
55+
56+
return _processExclusions;
57+
}
58+
}
59+
3060
public MainForm()
3161
{
3262
InitializeComponent();
@@ -68,9 +98,14 @@ protected override void OnLoad(EventArgs e)
6898
_systemTrayMenu.MenuItemExit.Click += MenuItemExitClick;
6999
_systemTrayMenu.MenuItemAutoStart.Checked = AutoStarter.IsAutoStartByRegisterEnabled(AssemblyUtility.AssemblyProductName, AssemblyUtility.AssemblyLocation);
70100
#endif
71-
_windows = EnumWindows.EnumAllWindows(new String[] { _shellWindowName }).ToList();
101+
_windows = EnumWindows.EnumAllWindows(new String[] { SHELL_WINDOW_NAME }).ToList();
72102
foreach (var window in _windows)
73103
{
104+
var processName = Path.GetFileName(window.Process.MainModule.FileName);
105+
if (ProcessExclusions.Contains(processName.ToLower()))
106+
{
107+
continue;
108+
}
74109
window.Menu.Create();
75110
Int32 menuItemId = window.ProcessPriority.GetMenuItemId();
76111
window.Menu.CheckMenuItem(menuItemId, true);
@@ -219,13 +254,18 @@ private void WindowCreated(object sender, WindowEventArgs e)
219254
IList<Window> windows = new List<Window>();
220255
try
221256
{
222-
windows = EnumWindows.EnumProcessWindows(processId, _windows.Select(w => w.Handle).ToArray(), new String[] { _shellWindowName });
257+
windows = EnumWindows.EnumProcessWindows(processId, _windows.Select(w => w.Handle).ToArray(), new String[] { SHELL_WINDOW_NAME });
223258
}
224259
catch
225260
{
226261
}
227262
foreach (var window in windows)
228263
{
264+
var processName = Path.GetFileName(window.Process.MainModule.FileName);
265+
if (ProcessExclusions.Contains(processName.ToLower()))
266+
{
267+
continue;
268+
}
229269
window.Menu.Create();
230270
Int32 menuItemId = window.ProcessPriority.GetMenuItemId();
231271
window.Menu.CheckMenuItem(menuItemId, true);

SmartSystemMenu/SmartSystemMenu.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@
190190
<ItemGroup>
191191
<EmbeddedResource Include="SmartSystemMenu64.exe" />
192192
<None Include="SmartSystemMenu.bmp" />
193+
<Content Include="SmartSystemMenuProcessExclusions.txt">
194+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
195+
</Content>
193196
<Content Include="SmartSystemMenu.ico" />
194197
</ItemGroup>
195198
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
discord.exe
2+
slack.exe
3+
some_other_process_name.exe

SmartSystemMenuHook/dllmain.cpp

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,80 @@
1-
// dllmain.cpp : Defines the entry point for the DLL application.
2-
#include "stdafx.h"
3-
#include <string>
4-
#include <vector>
5-
#include <algorithm>
6-
7-
extern HINSTANCE g_appInstance;
8-
9-
bool array_contains(const std::string &value, const std::vector<std::string> &array)
10-
{
11-
return std::find(array.begin(), array.end(), value) != array.end();
12-
}
13-
14-
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
15-
{
16-
switch (ul_reason_for_call)
17-
{
18-
case DLL_PROCESS_ATTACH:
19-
if (g_appInstance == NULL)
1+
// dllmain.cpp : Defines the entry point for the DLL application.
2+
#include "stdafx.h"
3+
#include <string>
4+
#include <vector>
5+
#include <algorithm>
6+
#include <fstream>
7+
8+
extern HINSTANCE g_appInstance;
9+
10+
bool array_contains(const std::string &value, const std::vector<std::string> &array)
11+
{
12+
return std::find(array.begin(), array.end(), value) != array.end();
13+
}
14+
15+
std::string get_file_name(const std::string &path)
16+
{
17+
std::size_t index = path.find_last_of("/\\");
18+
std::string fileName = path.substr(index + 1);
19+
return fileName;
20+
}
21+
22+
std::string get_directory_name(const std::string &path)
23+
{
24+
std::size_t index = path.find_last_of("/\\");
25+
std::string directoryName = path.substr(0, index);
26+
return directoryName;
27+
}
28+
29+
std::vector<std::string> read_process_exclusions(const std::string &path)
30+
{
31+
std::vector<std::string> result{};
32+
std::string str;
33+
std::ifstream file(path.c_str());
34+
if (!file.good())
35+
{
36+
return result;
37+
}
38+
while (std::getline(file, str))
39+
{
40+
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
41+
result.push_back(str);
42+
}
43+
return result;
44+
}
45+
46+
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
47+
{
48+
switch (ul_reason_for_call)
49+
{
50+
case DLL_PROCESS_ATTACH:
51+
if (g_appInstance == NULL)
52+
{
53+
g_appInstance = hModule;
54+
}
55+
WCHAR exePath[MAX_PATH], dllPath[MAX_PATH];
56+
if (GetModuleFileName(NULL, exePath, sizeof(exePath)) != 0 && GetModuleFileName(hModule, dllPath, sizeof(dllPath)) != 0)
57+
{
58+
std::wstring dllTempPath(&dllPath[0]);
59+
std::string dllFileName(dllTempPath.begin(), dllTempPath.end());
60+
std::string dllDirectoryName = get_directory_name(dllFileName);
61+
std::string exclusionsFileName = dllDirectoryName + "\\SmartSystemMenuProcessExclusions.txt";
62+
std::vector<std::string> excludedProcessNames = read_process_exclusions(exclusionsFileName);
63+
std::wstring exeTempPath(&exePath[0]);
64+
std::string exeFileName(exeTempPath.begin(), exeTempPath.end());
65+
exeFileName = get_file_name(exeFileName);
66+
std::transform(exeFileName.begin(), exeFileName.end(), exeFileName.begin(), ::tolower);
67+
if (array_contains(exeFileName, excludedProcessNames))
2068
{
21-
g_appInstance = hModule;
22-
}
23-
WCHAR path[MAX_PATH];
24-
if (GetModuleFileName(NULL, path, sizeof(path)) != 0)
25-
{
26-
std::vector<std::string> excludedProcessNames{ "discord.exe", "slack.exe" };
27-
std::wstring tempPath(&path[0]);
28-
std::string processPath(tempPath.begin(), tempPath.end());
29-
std::size_t index = processPath.find_last_of("/\\");
30-
std::string processName = processPath.substr(index + 1);
31-
std::transform(processName.begin(), processName.end(), processName.begin(), ::tolower);
32-
if (array_contains(processName, excludedProcessNames))
33-
{
34-
return FALSE;
35-
}
36-
}
37-
break;
38-
39-
case DLL_THREAD_ATTACH:
40-
case DLL_THREAD_DETACH:
41-
case DLL_PROCESS_DETACH:
42-
break;
43-
}
44-
return TRUE;
69+
return FALSE;
70+
}
71+
}
72+
break;
73+
74+
case DLL_THREAD_ATTACH:
75+
case DLL_THREAD_DETACH:
76+
case DLL_PROCESS_DETACH:
77+
break;
78+
}
79+
return TRUE;
4580
}

0 commit comments

Comments
 (0)