Skip to content

Commit 11eeb9a

Browse files
committed
Fix regression of launching notepad++.exe with admin privilege
Fix notepad-plus-plus/notepad-plus-plus#17540 (comment)
1 parent 4ebb452 commit 11eeb9a

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/Common.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,23 @@ wstring stringReplace(wstring subject, const wstring& search, const wstring& rep
262262
}
263263
return subject;
264264
}
265+
266+
void safeLaunchAsUser(const std::wstring& prog2Launch)
267+
{
268+
wchar_t winDir[MAX_PATH];
269+
if (GetWindowsDirectory(winDir, MAX_PATH) == 0) return;
270+
std::wstring explorerPath = std::wstring(winDir) + L"\\explorer.exe";
271+
272+
wchar_t prog2LaunchDir[MAX_PATH];
273+
lstrcpy(prog2LaunchDir, prog2Launch.c_str());
274+
::PathRemoveFileSpec(prog2LaunchDir);
275+
276+
::ShellExecute(
277+
NULL,
278+
L"open",
279+
explorerPath.c_str(), // Trusted path
280+
prog2Launch.c_str(), // Target
281+
prog2LaunchDir,
282+
SW_SHOWNORMAL
283+
);
284+
}

src/Common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ std::wstring GetLastErrorAsString(DWORD errorCode);
6565
std::wstring stringToUpper(std::wstring strToConvert);
6666
std::wstring stringToLower(std::wstring strToConvert);
6767
std::wstring stringReplace(std::wstring subject, const std::wstring& search, const std::wstring& replace);
68+
void safeLaunchAsUser(const std::wstring& prog2Launch);

src/winmain.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,9 +1469,6 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR lpszCmdLine, int)
14691469
return -1;
14701470
}
14711471
wstring prog2Launch = params[0];
1472-
wchar_t prog2LaunchDir[MAX_PATH];
1473-
lstrcpy(prog2LaunchDir, prog2Launch.c_str());
1474-
::PathRemoveFileSpec(prog2LaunchDir);
14751472
wstring destPathRoot = params[1];
14761473

14771474
#ifdef _DEBUG
@@ -1502,7 +1499,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR lpszCmdLine, int)
15021499
deleteFileOrFolder(destPath);
15031500
}
15041501

1505-
::ShellExecute(NULL, L"open", prog2Launch.c_str(), NULL, prog2LaunchDir, SW_SHOWNORMAL);
1502+
safeLaunchAsUser(prog2Launch);
15061503

15071504
return 0;
15081505
}
@@ -1654,7 +1651,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR lpszCmdLine, int)
16541651
}
16551652
}
16561653

1657-
::ShellExecute(NULL, L"open", prog2Launch.c_str(), NULL, prog2LaunchDir, SW_SHOWNORMAL);
1654+
safeLaunchAsUser(prog2Launch);
16581655

16591656
return 0;
16601657
}

0 commit comments

Comments
 (0)