Skip to content

Commit

Permalink
Allows to run in administrator mode if file is read-only in case of U…
Browse files Browse the repository at this point in the history
…AC control
  • Loading branch information
xupefei committed Apr 20, 2014
1 parent 8017b94 commit 28ae17f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Notepad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#if !defined(_WIN32_WINNT)
#define _WIN32_WINNT 0x501
#endif

#pragma comment(lib, "rpcrt4")

#include <windows.h>
#include <commctrl.h>
#include <shlobj.h>
Expand Down Expand Up @@ -169,6 +172,7 @@ BOOL bTransparentMode;
BOOL bTransparentModeAvailable;
BOOL bShowToolbar;
BOOL bShowStatusbar;
BOOL bCheckWritePermission;

typedef struct _wi
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
{
Expand Down
2 changes: 2 additions & 0 deletions src/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 28ae17f

Please sign in to comment.