Skip to content

Commit e4f9037

Browse files
GurliGebisdonho
authored andcommitted
Fix icon missing issue due to file system permissions
Fix #2, Close #6
1 parent 5e8aac9 commit e4f9037

13 files changed

+135
-44
lines changed

AclHelper.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "pch.h"
2+
#include "AclHelper.h"
3+
4+
AclHelper::AclHelper()
5+
{
6+
emptyAcl = (PACL)malloc(sizeof(ACL));
7+
8+
if (emptyAcl)
9+
{
10+
InitializeAcl(emptyAcl, sizeof(ACL), ACL_REVISION);
11+
}
12+
}
13+
14+
AclHelper::~AclHelper()
15+
{
16+
if (emptyAcl)
17+
{
18+
free(emptyAcl);
19+
}
20+
}
21+
22+
DWORD AclHelper::ResetAcl(const wstring& path)
23+
{
24+
if (emptyAcl)
25+
{
26+
return SetNamedSecurityInfoW(const_cast<LPWSTR>(path.c_str()), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION, NULL, NULL, emptyAcl, NULL);
27+
}
28+
else
29+
{
30+
return ERROR_OUTOFMEMORY;
31+
}
32+
}

AclHelper.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
class AclHelper
4+
{
5+
public:
6+
AclHelper();
7+
~AclHelper();
8+
9+
DWORD ResetAcl(const wstring& path);
10+
11+
private:
12+
PACL emptyAcl;
13+
};

EditWithNppExplorerCommandHandler.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include "pch.h"
22
#include "EditWithNppExplorerCommandHandler.h"
33

4-
#include "Helpers.h"
4+
#include "PathHelper.h"
55

66
using namespace NppShell::CommandHandlers;
77
using namespace NppShell::Helpers;
88

99
const wstring EditWithNppExplorerCommandHandler::GetNppExecutableFullPath()
1010
{
11-
const wstring path = GetInstallationPath();
11+
const wstring path = GetApplicationPath();
1212
const wstring fileName = L"\\notepad++.exe";
1313

14-
return L"\"" + path + fileName + L"\"";
14+
return path + fileName;
1515
}
1616

1717
const wstring EditWithNppExplorerCommandHandler::Title()
@@ -26,12 +26,12 @@ const wstring EditWithNppExplorerCommandHandler::Icon()
2626
return fileName;
2727
}
2828

29-
const wstring EditWithNppExplorerCommandHandler::GetCommandLine()
29+
const wstring EditWithNppExplorerCommandHandler::GetCommandLine(const wstring& itemName)
3030
{
3131
const wstring fileName = GetNppExecutableFullPath();
32-
const wstring parameters = L"\"%1\"";
32+
const wstring parameters = L"\"" + itemName + L"\"";
3333

34-
return fileName + L" " + parameters;
34+
return L"\"" + fileName + L"\" " + parameters;
3535
}
3636

3737
IFACEMETHODIMP EditWithNppExplorerCommandHandler::Invoke(IShellItemArray* psiItemArray, IBindCtx* pbc) noexcept try
@@ -54,8 +54,8 @@ IFACEMETHODIMP EditWithNppExplorerCommandHandler::Invoke(IShellItemArray* psiIte
5454
psiItemArray->GetItemAt(i, &psi);
5555
RETURN_IF_FAILED(psi->GetDisplayName(SIGDN_FILESYSPATH, &itemName));
5656

57-
std::wstring cmdline = this->GetCommandLine();
58-
cmdline = cmdline.replace(cmdline.find(L"%1"), 2, itemName);
57+
const wstring applicationName = GetNppExecutableFullPath();
58+
const wstring commandLine = GetCommandLine(itemName);
5959

6060
STARTUPINFO si;
6161
PROCESS_INFORMATION pi;
@@ -64,9 +64,10 @@ IFACEMETHODIMP EditWithNppExplorerCommandHandler::Invoke(IShellItemArray* psiIte
6464
si.cb = sizeof(si);
6565
ZeroMemory(&pi, sizeof(pi));
6666

67-
wchar_t* command = (LPWSTR)cmdline.c_str();
67+
wchar_t* application = (LPWSTR)applicationName.c_str();
68+
wchar_t* command = (LPWSTR)commandLine.c_str();
6869

69-
if (!CreateProcess(nullptr, command, nullptr, nullptr, false, CREATE_NEW_PROCESS_GROUP, nullptr, nullptr, &si, &pi))
70+
if (!CreateProcessW(application, command, nullptr, nullptr, false, CREATE_NEW_PROCESS_GROUP, nullptr, nullptr, &si, &pi))
7071
{
7172
return S_OK;
7273
}

EditWithNppExplorerCommandHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ namespace NppShell::CommandHandlers
1919

2020
private:
2121
const wstring GetNppExecutableFullPath();
22-
const wstring GetCommandLine();
22+
const wstring GetCommandLine(const wstring& itemName);
2323
};
2424
}

Helpers.cpp

Lines changed: 0 additions & 13 deletions
This file was deleted.

Installer.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "Installer.h"
33

44
#include "EditWithNppExplorerCommandHandler.h"
5+
#include "PathHelper.h"
6+
#include "AclHelper.h"
57

68
#define GUID_STRING_SIZE 40
79

@@ -126,7 +128,7 @@ LRESULT CreateRegistryKey(const HKEY hive, const wstring& key, const wstring& na
126128
return lResult;
127129
}
128130

129-
LRESULT CleanupRegistry(const wstring guid)
131+
LRESULT CleanupRegistry(const wstring& guid)
130132
{
131133
constexpr int bufferSize = MAX_PATH + GUID_STRING_SIZE;
132134
WCHAR buffer[bufferSize];
@@ -233,12 +235,24 @@ HRESULT MoveFileToTempAndScheduleDeletion(const wstring& filePath)
233235
return S_OK;
234236
}
235237

238+
HRESULT ResetAclPermissionsOnApplicationFolder()
239+
{
240+
// First we get the path where Notepad++ is installed.
241+
const wstring applicationPath = GetApplicationPath();
242+
243+
// Create a new AclHelper
244+
AclHelper aclHelper;
245+
246+
// Reset the ACL of the folder where Notepad++ is installed.
247+
aclHelper.ResetAcl(applicationPath);
248+
}
249+
236250
HRESULT NppShell::Installer::RegisterSparsePackage()
237251
{
238252
PackageManager packageManager;
239253
AddPackageOptions options;
240254

241-
const wstring externalLocation = GetInstallationPath();
255+
const wstring externalLocation = GetContextMenuPath();
242256
const wstring sparsePkgPath = externalLocation + L"\\NppShell.msix";
243257

244258
Uri externalUri(externalLocation);
@@ -290,12 +304,15 @@ HRESULT NppShell::Installer::UnregisterSparsePackage()
290304
break;
291305
}
292306

307+
// After unregistering the sparse package, we reset the folder permissions of the folder where we are installed.
308+
ResetAclPermissionsOnApplicationFolder();
309+
293310
return S_OK;
294311
}
295312

296313
HRESULT NppShell::Installer::RegisterOldContextMenu()
297314
{
298-
const wstring installationPath = GetInstallationPath();
315+
const wstring installationPath = GetContextMenuPath();
299316
const wstring guid = GetCLSIDString();
300317

301318
CreateRegistryKey(HKEY_LOCAL_MACHINE, ShellKey, L"ExplorerCommandHandler", guid.c_str());
@@ -340,9 +357,11 @@ HRESULT NppShell::Installer::Install()
340357
result = RegisterOldContextMenu();
341358
}
342359

343-
// Ensure NppModernShell files have been moved away.
344-
MoveFileToTempAndScheduleDeletion(GetInstallationPath() + L"\\NppModernShell.dll");
345-
MoveFileToTempAndScheduleDeletion(GetInstallationPath() + L"\\NppModernShell.msix");
360+
// Ensure NppModernShell and NppShell files have been moved away from the main program directory.
361+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell.dll");
362+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell.msix");
363+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppModernShell.dll");
364+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppModernShell.msix");
346365

347366
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
348367

Installer.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#pragma once
22
#include "pch.h"
33

4-
#include "Helpers.h"
5-
64
namespace NppShell::Installer
75
{
86
HRESULT RegisterOldContextMenu();
@@ -13,6 +11,6 @@ namespace NppShell::Installer
1311

1412
HRESULT Install();
1513
HRESULT Uninstall();
14+
}
1615

17-
STDAPI CleanupDll();
18-
}
16+
STDAPI CleanupDll();

NppShell.vcxproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,21 @@
255255
</PostBuildEvent>
256256
</ItemDefinitionGroup>
257257
<ItemGroup>
258+
<ClInclude Include="AclHelper.h" />
258259
<ClInclude Include="EditWithNppExplorerCommandHandler.h" />
259260
<ClInclude Include="ExplorerCommandBase.h" />
260261
<ClInclude Include="framework.h" />
261-
<ClInclude Include="Helpers.h" />
262+
<ClInclude Include="PathHelper.h" />
262263
<ClInclude Include="Installer.h" />
263264
<ClInclude Include="pch.h" />
264265
<ClInclude Include="SimpleFactory.h" />
265266
</ItemGroup>
266267
<ItemGroup>
268+
<ClCompile Include="AclHelper.cpp" />
267269
<ClCompile Include="dllmain.cpp" />
268270
<ClCompile Include="EditWithNppExplorerCommandHandler.cpp" />
269271
<ClCompile Include="ExplorerCommandBase.cpp" />
270-
<ClCompile Include="Helpers.cpp" />
272+
<ClCompile Include="PathHelper.cpp" />
271273
<ClCompile Include="Installer.cpp" />
272274
<ClCompile Include="pch.cpp">
273275
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>

NppShell.vcxproj.filters

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<Filter Include="Factories">
1111
<UniqueIdentifier>{a0f353df-9c6f-4612-a2d1-98aa7a6d6893}</UniqueIdentifier>
1212
</Filter>
13+
<Filter Include="Helpers">
14+
<UniqueIdentifier>{9c186a78-bd74-4ac7-b560-e61aeb6d2350}</UniqueIdentifier>
15+
</Filter>
1316
</ItemGroup>
1417
<ItemGroup>
1518
<ClInclude Include="EditWithNppExplorerCommandHandler.h">
@@ -22,11 +25,16 @@
2225
<Filter>Installer</Filter>
2326
</ClInclude>
2427
<ClInclude Include="pch.h" />
25-
<ClInclude Include="Helpers.h" />
2628
<ClInclude Include="framework.h" />
2729
<ClInclude Include="SimpleFactory.h">
2830
<Filter>Factories</Filter>
2931
</ClInclude>
32+
<ClInclude Include="PathHelper.h">
33+
<Filter>Helpers</Filter>
34+
</ClInclude>
35+
<ClInclude Include="AclHelper.h">
36+
<Filter>Helpers</Filter>
37+
</ClInclude>
3038
</ItemGroup>
3139
<ItemGroup>
3240
<ClCompile Include="EditWithNppExplorerCommandHandler.cpp">
@@ -39,8 +47,13 @@
3947
<Filter>Installer</Filter>
4048
</ClCompile>
4149
<ClCompile Include="dllmain.cpp" />
42-
<ClCompile Include="Helpers.cpp" />
4350
<ClCompile Include="pch.cpp" />
51+
<ClCompile Include="PathHelper.cpp">
52+
<Filter>Helpers</Filter>
53+
</ClCompile>
54+
<ClCompile Include="AclHelper.cpp">
55+
<Filter>Helpers</Filter>
56+
</ClCompile>
4457
</ItemGroup>
4558
<ItemGroup>
4659
<None Include="packages.config" />

PathHelper.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "pch.h"
2+
#include "PathHelper.h"
3+
4+
using namespace NppShell::Helpers;
5+
using namespace std::filesystem;
6+
7+
extern HMODULE thisModule;
8+
9+
const path GetThisModulePath()
10+
{
11+
wchar_t pathBuffer[FILENAME_MAX] = { 0 };
12+
GetModuleFileName(thisModule, pathBuffer, FILENAME_MAX);
13+
return path(pathBuffer);
14+
}
15+
16+
const wstring NppShell::Helpers::GetApplicationPath()
17+
{
18+
path modulePath = GetThisModulePath();
19+
return modulePath.parent_path().parent_path().wstring();
20+
}
21+
22+
const wstring NppShell::Helpers::GetContextMenuPath()
23+
{
24+
path modulePath = GetThisModulePath();
25+
return modulePath.parent_path().wstring();
26+
}

0 commit comments

Comments
 (0)