From 28ae17f59aec0c47e2f36d98ff28292a2141e742 Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Sun, 20 Apr 2014 21:23:48 +0200 Subject: [PATCH 1/2] Allows to run in administrator mode if file is read-only in case of UAC control --- src/Notepad2.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Notepad2.rc | 2 ++ src/resource.h | 1 + 3 files changed, 70 insertions(+) diff --git a/src/Notepad2.c b/src/Notepad2.c index 3615fa4b..7dbd9721 100644 --- a/src/Notepad2.c +++ b/src/Notepad2.c @@ -20,6 +20,9 @@ #if !defined(_WIN32_WINNT) #define _WIN32_WINNT 0x501 #endif + +#pragma comment(lib, "rpcrt4") + #include #include #include @@ -169,6 +172,7 @@ BOOL bTransparentMode; BOOL bTransparentModeAvailable; BOOL bShowToolbar; BOOL bShowStatusbar; +BOOL bCheckWritePermission; typedef struct _wi { @@ -2287,6 +2291,8 @@ void MsgInitMenu(HWND hwnd,WPARAM wParam,LPARAM lParam) EnableCmd(hmenu,IDM_VIEW_NOSAVEFINDREPL,i); EnableCmd(hmenu,IDM_VIEW_SAVESETTINGS,i); + CheckCmd(hmenu,IDM_VIEW_CHECKWRITEPERMISSION,bCheckWritePermission); + i = (lstrlen(szIniFile) > 0 || lstrlen(szIniFile2) > 0); EnableCmd(hmenu,IDM_VIEW_SAVESETTINGSNOW,i); @@ -4289,6 +4295,9 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) iEscFunction = 2; break; + case IDM_VIEW_CHECKWRITEPERMISSION: + bCheckWritePermission = (bCheckWritePermission) ? FALSE : TRUE; + break; case IDM_VIEW_SAVESETTINGS: bSaveSettings = (bSaveSettings) ? FALSE : TRUE; @@ -5482,6 +5491,10 @@ void LoadSettings() LoadIniSection(L"Settings",pIniSection,cchIniSection); + bCheckWritePermission = + IniSectionGetInt(pIniSection, L"CheckWritePermission",0); + if (bCheckWritePermission) bCheckWritePermission = 1; + bSaveSettings = IniSectionGetInt(pIniSection,L"SaveSettings",1); if (bSaveSettings) bSaveSettings = 1; @@ -5813,6 +5826,7 @@ void SaveSettings(BOOL bSaveSettingsNow) pIniSection = LocalAlloc(LPTR,sizeof(WCHAR)*32*1024); cchIniSection = (int)LocalSize(pIniSection)/sizeof(WCHAR); + IniSectionSetInt(pIniSection,L"CheckWritePermission", bCheckWritePermission); IniSectionSetInt(pIniSection,L"SaveSettings",bSaveSettings); IniSectionSetInt(pIniSection,L"SaveRecentFiles",bSaveRecentFiles); IniSectionSetInt(pIniSection,L"SaveFindReplace",bSaveFindReplace); @@ -6894,6 +6908,59 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp if (PathIsLnkFile(szFileName)) PathGetLnkPath(szFileName,szFileName,COUNTOF(szFileName)); + if (bCheckWritePermission) + { + UUID uuid; + UuidCreate(&uuid); + + WCHAR* pszUuid = NULL; + UuidToString(&uuid, &pszUuid); + + WCHAR szUuid[MAX_PATH] = L""; + lstrcpy(szUuid, pszUuid); + + WCHAR szTestFile[MAX_PATH] = L""; + + lstrcpy(szTestFile, szFileName); + PathRemoveFileSpec(szTestFile); + PathAppend(szTestFile, szUuid); + + HANDLE hTestFile = CreateFile(szTestFile, + GENERIC_WRITE, FILE_SHARE_WRITE, + NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); + if (hTestFile != INVALID_HANDLE_VALUE) + { + CloseHandle(hTestFile); + DeleteFile(szTestFile); + } + else + { + if (!fIsElevated) + { + WCHAR szExePath[MAX_PATH] = L""; + GetModuleFileName(NULL, szExePath, COUNTOF(szExePath)); + + WCHAR* exeName = PathFindFileName(szExePath); + + SHELLEXECUTEINFO shExecInfo; + + shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); + shExecInfo.fMask = NULL; + shExecInfo.hwnd = NULL; + shExecInfo.lpVerb = L"runas"; + shExecInfo.lpFile = exeName; + shExecInfo.lpParameters = szFileName; + shExecInfo.lpDirectory = NULL; + shExecInfo.nShow = SW_NORMAL; + shExecInfo.hInstApp = NULL; + + if(ShellExecuteEx(&shExecInfo)) + SendMessage(hwndMain, WM_DESTROY, 0, 0); + } + } + + } + // Ask to create a new file... if (!bReload && !PathFileExists(szFileName)) { diff --git a/src/Notepad2.rc b/src/Notepad2.rc index 7803b2a9..f7d1a207 100644 --- a/src/Notepad2.rc +++ b/src/Notepad2.rc @@ -348,6 +348,8 @@ BEGIN MENUITEM "C&ustomize Toolbar...", IDM_VIEW_CUSTOMIZETB MENUITEM "Sh&ow Statusbar", IDM_VIEW_STATUSBAR MENUITEM SEPARATOR + MENUITEM "Check Write Permission", IDM_VIEW_CHECKWRITEPERMISSION + MENUITEM SEPARATOR MENUITEM "Save Settings On E&xit", IDM_VIEW_SAVESETTINGS MENUITEM "Save Settings &Now\tF7", IDM_VIEW_SAVESETTINGSNOW END diff --git a/src/resource.h b/src/resource.h index 734421a3..d71dd6fa 100644 --- a/src/resource.h +++ b/src/resource.h @@ -355,6 +355,7 @@ #define IDM_HELP_ABOUT 40500 #define IDM_TRAY_RESTORE 40600 #define IDM_TRAY_EXIT 40601 +#define IDM_VIEW_CHECKWRITEPERMISSION 40602 #define IDT_FILE_NEW 40700 #define IDT_FILE_OPEN 40701 #define IDT_FILE_BROWSE 40702 From 7cd6d9e81319df113dfa0d784a742195fbedca74 Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Sun, 20 Apr 2014 21:57:40 +0200 Subject: [PATCH 2/2] Cancel loading if UAC successes --- src/Notepad2.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Notepad2.c b/src/Notepad2.c index 7dbd9721..c1a2469d 100644 --- a/src/Notepad2.c +++ b/src/Notepad2.c @@ -6955,7 +6955,10 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp shExecInfo.hInstApp = NULL; if(ShellExecuteEx(&shExecInfo)) + { SendMessage(hwndMain, WM_DESTROY, 0, 0); + return FALSE; + } } }