diff options
author | George Hazan <ghazan@miranda.im> | 2022-01-10 18:12:15 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2022-01-10 18:12:15 +0300 |
commit | dcaae5dea7a22183576ee6ce3045cacab085a56c (patch) | |
tree | 8b5edf03e7a8c8fa2e3d74ab54de2932fb6566d8 /src | |
parent | 26b0b0cc55d29b549bd364e622e95c55ef6ea24d (diff) |
PU::IsMirandaFolderWritable - core function to detect if need to draw a shield on buttons that require escalation
Diffstat (limited to 'src')
-rw-r--r-- | src/mir_app/src/db_ini.cpp | 8 | ||||
-rw-r--r-- | src/mir_app/src/mir_app.def | 1 | ||||
-rw-r--r-- | src/mir_app/src/mir_app64.def | 1 | ||||
-rw-r--r-- | src/mir_app/src/pu_utils.cpp | 38 |
4 files changed, 35 insertions, 13 deletions
diff --git a/src/mir_app/src/db_ini.cpp b/src/mir_app/src/db_ini.cpp index e4eb35c047..978c802679 100644 --- a/src/mir_app/src/db_ini.cpp +++ b/src/mir_app/src/db_ini.cpp @@ -227,6 +227,14 @@ public: {
m_iniPath.SetText(m_path);
m_newPath.SetText(m_path);
+
+ if (!PU::IsMirandaFolderWritable()) {
+ bool bIsElevated = PU::IsProcessElevated();
+ Button_SetElevationRequiredState(btnMove.GetHwnd(), !bIsElevated);
+ Button_SetElevationRequiredState(btnDelete.GetHwnd(), !bIsElevated);
+ Button_SetElevationRequiredState(btnRecycle.GetHwnd(), !bIsElevated);
+ }
+
return true;
}
diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index 575fe053e1..174144ffcc 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -790,3 +790,4 @@ _Netlib_SslWrite@12 @877 NONAME ?AllowTyping@CSrmmBaseDialog@@IBE_NXZ @878 NONAME
?setTyping@CSrmmBaseDialog@@QAEXHPBUUSERINFO@@@Z @879 NONAME
?SafeRecycleBin@PU@@YGHPB_W@Z @880 NONAME
+?IsMirandaFolderWritable@PU@@YG_NXZ @881 NONAME
diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index f867e78aa7..52a1e3b701 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -790,3 +790,4 @@ Netlib_SslWrite @877 NONAME ?AllowTyping@CSrmmBaseDialog@@IEBA_NXZ @878 NONAME
?setTyping@CSrmmBaseDialog@@QEAAXHPEBUUSERINFO@@@Z @879 NONAME
?SafeRecycleBin@PU@@YAHPEB_W@Z @880 NONAME
+?IsMirandaFolderWritable@PU@@YA_NXZ @881 NONAME
diff --git a/src/mir_app/src/pu_utils.cpp b/src/mir_app/src/pu_utils.cpp index 3851b4d949..87a78ac374 100644 --- a/src/mir_app/src/pu_utils.cpp +++ b/src/mir_app/src/pu_utils.cpp @@ -75,19 +75,39 @@ MIR_APP_DLL(bool) PU::IsDirect() } ///////////////////////////////////////////////////////////////////////////////////////// +// Checks if Miranda's folder is writeable + +MIR_APP_DLL(bool) PU::IsMirandaFolderWritable() +{ + if (!IsWinVerVistaPlus()) + return true; + + wchar_t wszPath[MAX_PATH]; + GetModuleFileNameW(nullptr, wszPath, _countof(wszPath)); + wchar_t *ext = wcsrchr(wszPath, '.'); + if (ext != nullptr) + *ext = '\0'; + wcscat(wszPath, L".test"); + HANDLE hFile = CreateFileW(wszPath, GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + if (hFile == INVALID_HANDLE_VALUE) + return false; + + CloseHandle(hFile); + DeleteFileW(wszPath); + return true; +} + +///////////////////////////////////////////////////////////////////////////////////////// // Checks if a process has enough rights to write into Miranda's folder MIR_APP_DLL(bool) PU::IsProcessElevated() { bool bIsElevated = false; - uint32_t dwError = ERROR_SUCCESS; HANDLE hToken = nullptr; // Open the primary access token of the process with TOKEN_QUERY. - if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) { - dwError = GetLastError(); + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) goto Cleanup; - } // Retrieve token elevation information. TOKEN_ELEVATION elevation; @@ -97,7 +117,6 @@ MIR_APP_DLL(bool) PU::IsProcessElevated() // Vista, GetTokenInformation returns FALSE with the // ERROR_INVALID_PARAMETER error code because TokenElevation is // not supported on those operating systems. - dwError = GetLastError(); goto Cleanup; } @@ -105,15 +124,8 @@ MIR_APP_DLL(bool) PU::IsProcessElevated() Cleanup: // Centralized cleanup for all allocated resources. - if (hToken) { + if (hToken) CloseHandle(hToken); - hToken = nullptr; - } - - // Throw the error if something failed in the function. - if (ERROR_SUCCESS != dwError) { - throw dwError; - } return bIsElevated; } |