diff --git a/Library/DialogManage.cpp b/Library/DialogManage.cpp index f2c24b4c..0384090b 100644 --- a/Library/DialogManage.cpp +++ b/Library/DialogManage.cpp @@ -957,22 +957,14 @@ INT_PTR CDialogManage::CTabSkins::OnCommand(WPARAM wParam, LPARAM lParam) std::wstring command = Rainmeter->GetSkinPath() + m_SkinName; command += L"\\"; command += m_FileName; - HANDLE file = CreateFile(command.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + bool writable = CSystem::IsFileWritable(command.c_str()); command.insert(0, L" \""); command.insert(0, Rainmeter->GetConfigEditor()); command += L"\""; - if (file == INVALID_HANDLE_VALUE) - { - // File is in protected location, so execute as admin - RunCommand(NULL, command.c_str(), SW_SHOWNORMAL, L"runas"); - } - else - { - CloseHandle(file); - RunCommand(NULL, command.c_str(), SW_SHOWNORMAL); - } + // Execute as admin if in protected location + RunCommand(NULL, command.c_str(), SW_SHOWNORMAL, !writable); } break; @@ -1746,7 +1738,7 @@ INT_PTR CDialogManage::CTabSettings::OnCommand(WPARAM wParam, LPARAM lParam) { WCHAR buffer[16]; int sel = ComboBox_GetCurSel((HWND)lParam); - LCID lcid = ComboBox_GetItemData((HWND)lParam, sel); + LCID lcid = (LCID)ComboBox_GetItemData((HWND)lParam, sel); _ultow(lcid, buffer, 10); WritePrivateProfileString(L"Rainmeter", L"Language", buffer, Rainmeter->GetIniFile().c_str()); diff --git a/Library/Rainmeter.cpp b/Library/Rainmeter.cpp index 7e1e5aa8..0338b649 100644 --- a/Library/Rainmeter.cpp +++ b/Library/Rainmeter.cpp @@ -3143,17 +3143,7 @@ void CRainmeter::SetDisableVersionCheck(bool check) void CRainmeter::TestSettingsFile(bool bDefaultIniLocation) { - WritePrivateProfileString(L"Rainmeter", L"WriteTest", L"TRUE", m_IniFile.c_str()); - WritePrivateProfileString(NULL, NULL, NULL, m_IniFile.c_str()); // FLUSH - - WCHAR tmpSz[5]; - bool bSuccess = (GetPrivateProfileString(L"Rainmeter", L"WriteTest", L"", tmpSz, 5, m_IniFile.c_str()) > 0); - if (bSuccess) - { - bSuccess = (wcscmp(L"TRUE", tmpSz) == 0); - WritePrivateProfileString(L"Rainmeter", L"WriteTest", NULL, m_IniFile.c_str()); - } - if (!bSuccess) + if (!CSystem::IsFileWritable(m_IniFile.c_str())) { std::wstring error = GetString(ID_STR_SETTINGSNOTWRITABLE); diff --git a/Library/System.cpp b/Library/System.cpp index f16a605e..5c242c19 100644 --- a/Library/System.cpp +++ b/Library/System.cpp @@ -1034,6 +1034,24 @@ ULONGLONG CSystem::GetTickCount64() } } +/* +** IsFileWritable +** +** Checks if file is writable. +** +*/ +bool CSystem::IsFileWritable(LPCWSTR file) +{ + HANDLE hFile = CreateFile(file, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + if (hFile == INVALID_HANDLE_VALUE) + { + return false; + } + + CloseHandle(hFile); + return true; +} + /* ** RmLoadLibrary ** diff --git a/Library/System.h b/Library/System.h index bfe9147c..32c4f45a 100644 --- a/Library/System.h +++ b/Library/System.h @@ -76,6 +76,8 @@ public: static bool IsUNCPath(const std::wstring& path) { return (path.length() >= 2 && IsPathSeparator(path[0]) && IsPathSeparator(path[1])); } static bool IsAbsolutePath(const std::wstring& path) { return (path.find(L":\\") != std::wstring::npos || path.find(L":/") != std::wstring::npos || IsUNCPath(path)); } + static bool IsFileWritable(LPCWSTR file); + static HMODULE RmLoadLibrary(LPCWSTR lpLibFileName, DWORD* dwError = NULL, bool ignoreErrors = false); static void ResetWorkingDirectory();