Skip to content
This repository has been archived by the owner on Aug 12, 2018. It is now read-only.

Restart Notepad2 as Administrator, keeping the changes, when unable to save file #154

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ BOOL MRU_AddFile(LPMRULIST pmru,LPCWSTR pszFile,BOOL bRelativePath,BOOL bUnexpan

int i;
for (i = 0; i < pmru->iSize; i++) {
if (lstrcmpi(pmru->pszItems[i],pszFile) == 0) {
if (!pmru->pszItems[i] || lstrcmpi(pmru->pszItems[i],pszFile) == 0) {
LocalFree(pmru->pszItems[i]);
break;
}
Expand Down
106 changes: 87 additions & 19 deletions src/Notepad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ TBBUTTON tbbMainWnd[] = { {0,IDT_FILE_NEW,TBSTATE_ENABLED,TBSTYLE_BUTTON,0,0},

WCHAR szIniFile[MAX_PATH] = L"";
WCHAR szIniFile2[MAX_PATH] = L"";
WCHAR szBufferFile[MAX_PATH] = L"";
BOOL bSaveSettings;
BOOL bSaveRecentFiles;
BOOL bSaveFindReplace;
Expand Down Expand Up @@ -373,6 +374,7 @@ int flagQuietCreate = 0;
int flagUseSystemMRU = 0;
int flagRelaunchElevated = 0;
int flagDisplayHelp = 0;
int flagBufferFile = 0;



Expand Down Expand Up @@ -639,7 +641,7 @@ int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpCmdLine,int n
StrCat(wchWndClass,L"B");

// Relaunch with elevated privileges
if (RelaunchElevated())
if (RelaunchElevated(NULL))
return(0);

// Try to run multiple instances
Expand Down Expand Up @@ -875,8 +877,18 @@ HWND InitInstance(HINSTANCE hInstance,LPSTR pszCmdLine,int nCmdShow)
if (OpenFileDlg(hwndMain,tchFile,COUNTOF(tchFile),lpFileArg))
bOpened = FileLoad(FALSE,FALSE,FALSE,FALSE,tchFile);
}
else {
if (bOpened = FileLoad(FALSE,FALSE,FALSE,FALSE,lpFileArg)) {
else
{
LPCWSTR lpFileToOpen = flagBufferFile ? szBufferFile : lpFileArg;
if (bOpened = FileLoad(FALSE, FALSE, FALSE, FALSE, lpFileToOpen)) {
if (flagBufferFile) {
lstrcpy(szCurFile, lpFileArg);
if (!flagLexerSpecified)
Style_SetLexerFromFile(hwndEdit, szCurFile);
bModified = TRUE;
SetWindowTitle(hwndMain, uidsAppTitle, fIsElevated, IDS_UNTITLED, lpFileArg,
iPathNameFormat, bModified, IDS_READONLY, bReadOnly, szTitleExcerpt);
}
if (flagJumpTo) { // Jump to position
EditJumpTo(hwndEdit,iInitialLine,iInitialColumn);
EditEnsureSelectionVisible(hwndEdit);
Expand Down Expand Up @@ -6031,6 +6043,14 @@ void ParseCommandLine()
else
flagUseSystemMRU = 1;
}
else if (StrCmpNI(lp1,L"buffer",CSTRLEN(L"buffer")) == 0) {
if (ExtractFirstArgument(lp2, lp1, lp2)) {
StrCpyN(szBufferFile, lp1, COUNTOF(szBufferFile));
TrimString(szBufferFile);
PathUnquoteSpaces(szBufferFile);
flagBufferFile = 1;
}
}

else switch (*CharUpper(lp1))
{
Expand Down Expand Up @@ -7116,11 +7136,52 @@ BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy)
if (lstrlen(szCurFile) != 0)
lstrcpy(tchFile,szCurFile);

SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
IDS_READONLY,bReadOnly,szTitleExcerpt);
if (!fIsElevated && dwLastIOError == ERROR_ACCESS_DENIED)
{
if (IDYES == MsgBox(MBYESNOWARN, IDS_ERR_ACCESSDENIED, tchFile))
{
WCHAR lpTempPathBuffer[MAX_PATH];
WCHAR szTempFileName[MAX_PATH];

if (GetTempPath(MAX_PATH, lpTempPathBuffer) &&
GetTempFileName(lpTempPathBuffer, TEXT("N2"), 0, szTempFileName))
{
if (FileIO(FALSE, szTempFileName, FALSE, &iEncoding, &iEOLMode, NULL, NULL, &bCancelDataLoss, TRUE))
{
WCHAR szArguments[2 * MAX_PATH + 64] = L"";
LPWSTR lpCmdLine = GetCommandLine();
LPWSTR lpExe = LocalAlloc(LPTR, sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
LPWSTR lpArgs = LocalAlloc(LPTR, sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));

ExtractFirstArgument(lpCmdLine, lpExe, lpArgs);

wsprintf(szArguments, L"/buffer %s %s", szTempFileName, lpArgs);
if (lstrlen(tchFile))
{
if (!StrStrI(szArguments, tchFile)) {
wsprintf(szArguments, L"%s %s", szArguments, tchFile);
}
}

flagRelaunchElevated = 1;
if (RelaunchElevated(szArguments))
{
LocalFree(lpExe);
LocalFree(lpArgs);
exit(0);
}
}
}
}
}
else
{
SetWindowTitle(hwndMain,uidsAppTitle,fIsElevated,IDS_UNTITLED,szCurFile,
iPathNameFormat,bModified || iEncoding != iOriginalEncoding,
IDS_READONLY,bReadOnly,szTitleExcerpt);

MsgBox(MBWARN,IDS_ERR_SAVEFILE,tchFile);
MsgBox(MBWARN,IDS_ERR_SAVEFILE,tchFile);
}
}

return(fSuccess);
Expand Down Expand Up @@ -7572,43 +7633,50 @@ BOOL RelaunchMultiInst() {
// RelaunchElevated()
//
//
BOOL RelaunchElevated() {
BOOL RelaunchElevated(LPWSTR lpArgs) {

if (!IsVista() || fIsElevated || !flagRelaunchElevated || flagDisplayHelp)
return(FALSE);

else {

LPWSTR lpCmdLine;
LPWSTR lpArg1, lpArg2;
LPWSTR lpCmdLine, lpExe;
STARTUPINFO si;
SHELLEXECUTEINFO sei;
BOOL bShouldFree = FALSE;

si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);

lpCmdLine = GetCommandLine();
lpArg1 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
lpArg2 = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
ExtractFirstArgument(lpCmdLine,lpArg1,lpArg2);
lpExe = LocalAlloc(LPTR, sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
if (!lpArgs) {
lpArgs = LocalAlloc(LPTR,sizeof(WCHAR)*(lstrlen(lpCmdLine) + 1));
ExtractFirstArgument(lpCmdLine, lpExe, lpArgs);
bShouldFree = TRUE;
}
else {
ExtractFirstArgument(lpCmdLine, lpExe, NULL);
}

if (lstrlen(lpArg1)) {
if (lstrlen(lpArgs)) {

ZeroMemory(&sei,sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_FLAG_NO_UI | /*SEE_MASK_NOASYNC*/0x00000100 | /*SEE_MASK_NOZONECHECKS*/0x00800000;
sei.hwnd = GetForegroundWindow();
sei.lpVerb = L"runas";
sei.lpFile = lpArg1;
sei.lpParameters = lpArg2;
sei.lpFile = lpExe;
sei.lpParameters = lpArgs;
sei.lpDirectory = g_wchWorkingDirectory;
sei.nShow = si.wShowWindow;
sei.nShow = si.wShowWindow ? si.wShowWindow : SW_SHOWNORMAL;

ShellExecuteEx(&sei);
}

LocalFree(lpArg1);
LocalFree(lpArg2);
if (bShouldFree)
LocalFree(lpArgs);
LocalFree(lpExe);

return(TRUE);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notepad2.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ BOOL InitApplication(HINSTANCE);
HWND InitInstance(HINSTANCE,LPSTR,int);
BOOL ActivatePrevInst();
BOOL RelaunchMultiInst();
BOOL RelaunchElevated();
BOOL RelaunchElevated(LPWSTR);
void SnapToDefaultPos(HWND);
void ShowNotifyIcon(HWND,BOOL);
void SetNotifyIconTitle(HWND);
Expand Down
1 change: 1 addition & 0 deletions src/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,7 @@ BEGIN
IDS_ASK_ENCODING2 "You are about to change the encoding of an empty file from ANSI to non-ANSI. Note that this will clear the undo history, as it can't be synchronized with the new encoding. Continue?"
IDS_ERR_ENCODINGNA "Code page conversion tables for the selected encoding are not available on your system."
IDS_ERR_UNICODE "Error converting this Unicode file.\nData will be lost if the file is saved!"
IDS_ERR_ACCESSDENIED "The file ""%s"" cannot be saved and may be protected.\n\nDo you want to launch Notepad2-mod as Administrator?"
END

STRINGTABLE
Expand Down
1 change: 1 addition & 0 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@
#define IDS_WRITEINI_FAIL 50038
#define IDS_SETTINGSNOTSAVED 50039
#define IDS_EXPORT_FAIL 50040
#define IDS_ERR_ACCESSDENIED 50041
#define IDS_CMDLINEHELP 60000

// Next default values for new objects
Expand Down