Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to try administrator mode in case of UAC control #1

Merged
merged 2 commits into from
Aug 21, 2019
Merged
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
70 changes: 70 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,62 @@ 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);
return FALSE;
}
}
}

}

// 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