Skip to content

Commit 9efecd7

Browse files
GurliGebisdonho
authored andcommitted
Fix contextmenu needs restarting os to appear issue under Win 8/10
Improved #14, to not move the file away, but just schedule them for deletion on next reboot. Fix #13
1 parent e982670 commit 9efecd7

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

Installer.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -206,27 +206,35 @@ LRESULT CleanupHack()
206206
return result;
207207
}
208208

209-
HRESULT MoveFileToTempAndScheduleDeletion(const wstring& filePath)
209+
HRESULT MoveFileToTempAndScheduleDeletion(const wstring& filePath, bool moveToTempDirectory)
210210
{
211211
wstring tempPath(MAX_PATH, L'\0');
212212
wstring tempFileName(MAX_PATH, L'\0');
213213

214214
BOOL moveResult;
215215

216-
GetTempPath(MAX_PATH, &tempPath[0]);
217-
GetTempFileName(tempPath.c_str(), L"tempFileName", 0, &tempFileName[0]);
216+
if (moveToTempDirectory)
217+
{
218+
GetTempPath(MAX_PATH, &tempPath[0]);
219+
GetTempFileName(tempPath.c_str(), L"tempFileName", 0, &tempFileName[0]);
218220

219-
// Move the file into the temp directory - it can be moved even when it is loaded into memory and locked.
220-
moveResult = MoveFileEx(filePath.c_str(), tempFileName.c_str(), MOVEFILE_REPLACE_EXISTING);
221+
// Move the file into the temp directory - it can be moved even when it is loaded into memory and locked.
222+
moveResult = MoveFileEx(filePath.c_str(), tempFileName.c_str(), MOVEFILE_REPLACE_EXISTING);
221223

222-
if (!moveResult)
224+
if (!moveResult)
225+
{
226+
return S_FALSE;
227+
}
228+
229+
// Schedule it to be deleted from the temp directory on the next reboot.
230+
moveResult = MoveFileExW(tempFileName.c_str(), NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
231+
}
232+
else
223233
{
224-
return S_FALSE;
234+
// Schedule it to be deleted on the next reboot, without moving it.
235+
moveResult = MoveFileExW(filePath.c_str(), NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
225236
}
226237

227-
// Schedule it to be deleted from the temp directory on the next reboot.
228-
moveResult = MoveFileExW(tempFileName.c_str(), NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
229-
230238
if (!moveResult)
231239
{
232240
return S_FALSE;
@@ -332,9 +340,6 @@ HRESULT NppShell::Installer::UnregisterOldContextMenu()
332340
// Clean up registry entries.
333341
CleanupRegistry(guid);
334342

335-
// Clean up the 8.5 Windows 11 hack if present.
336-
CleanupHack();
337-
338343
return S_OK;
339344
}
340345

@@ -344,21 +349,13 @@ HRESULT NppShell::Installer::Install()
344349

345350
HRESULT result;
346351

347-
UnregisterOldContextMenu();
348-
349-
// Ensure we have removed any old files that might be left behind.
350-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_01.dll");
351-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_02.dll");
352-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_03.dll");
353-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_04.dll");
354-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_05.dll");
355-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_06.dll");
356-
357-
// Since we have unregistered the old context menu, we refresh the shell, just to be sure.
358-
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
352+
// Clean up the 8.5 Windows 11 hack if present.
353+
CleanupHack();
359354

360355
if (isWindows11)
361356
{
357+
// We need to unregister the old menu on Windows 11 to prevent double entries in the old menu.
358+
UnregisterOldContextMenu();
362359
UnregisterSparsePackage();
363360

364361
result = RegisterSparsePackage();
@@ -368,11 +365,19 @@ HRESULT NppShell::Installer::Install()
368365
result = RegisterOldContextMenu();
369366
}
370367

371-
// Ensure NppModernShell and NppShell files have been moved away from the main program directory.
372-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell.dll");
373-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell.msix");
374-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppModernShell.dll");
375-
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppModernShell.msix");
368+
// Ensure we schedule old files for removal on next reboot.
369+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_01.dll", false);
370+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_02.dll", false);
371+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_03.dll", false);
372+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_04.dll", false);
373+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_05.dll", false);
374+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell_06.dll", false);
375+
376+
// This include the old NppModernShell and NppShell files from the main program directory.
377+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell.dll", false);
378+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppShell.msix", false);
379+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppModernShell.dll", false);
380+
MoveFileToTempAndScheduleDeletion(GetApplicationPath() + L"\\NppModernShell.msix", false);
376381

377382
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
378383

@@ -411,5 +416,5 @@ STDAPI CleanupDll()
411416
wstring currentFilePath(MAX_PATH, L'\0');
412417
GetModuleFileName(thisModule, &currentFilePath[0], MAX_PATH);
413418

414-
return MoveFileToTempAndScheduleDeletion(currentFilePath);
419+
return MoveFileToTempAndScheduleDeletion(currentFilePath, true);
415420
}

0 commit comments

Comments
 (0)