Fixed that "Edit skin" causes a needless UAC elevation prompt in some cases.

This commit is contained in:
Birunthan Mohanathas 2011-09-24 13:43:14 +00:00
parent 834bcd7bcd
commit e3bde6879b
4 changed files with 59 additions and 53 deletions

View File

@ -922,20 +922,23 @@ INT_PTR CDialogManage::CTabSkins::OnCommand(WPARAM wParam, LPARAM lParam)
case IDC_MANAGESKINS_EDIT_BUTTON: case IDC_MANAGESKINS_EDIT_BUTTON:
{ {
std::wstring command = Rainmeter->GetConfigEditor() + L" \""; std::wstring command = Rainmeter->GetSkinPath() + m_SkinName;
command += Rainmeter->GetSkinPath();
command += m_SkinName;
command += L"\\"; command += L"\\";
command += m_FileName; command += m_FileName;
HANDLE file = CreateFile(command.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
command.insert(0, L" \"");
command.insert(0, Rainmeter->GetConfigEditor());
command += L"\""; command += L"\"";
// If the skins are in the program folder start the editor as admin if (file == INVALID_HANDLE_VALUE)
if (Rainmeter->GetPath() + L"Skins\\" == Rainmeter->GetSkinPath())
{ {
LSExecuteAsAdmin(NULL, command.c_str(), SW_SHOWNORMAL); // File is in protected location, so execute as admin
ExecuteCommand(NULL, command.c_str(), SW_SHOWNORMAL, L"runas");
} }
else else
{ {
CloseHandle(file);
LSExecute(NULL, command.c_str(), SW_SHOWNORMAL); LSExecute(NULL, command.c_str(), SW_SHOWNORMAL);
} }
} }

View File

@ -289,46 +289,46 @@ HRGN BitmapToRegion(HBITMAP hbm, COLORREF clrTransp, COLORREF clrTolerance, int
return hRgn; return hRgn;
} }
HINSTANCE LSExecuteAsAdmin(HWND Owner, LPCTSTR szCommand, int nShowCmd) //HINSTANCE LSExecuteAsAdmin(HWND Owner, LPCTSTR szCommand, int nShowCmd)
{ //{
BOOL IsInAdminGroup = FALSE; // BOOL IsInAdminGroup = FALSE;
//
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; // SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup; // PSID AdministratorsGroup;
// Initialize SID. // // Initialize SID.
if (!AllocateAndInitializeSid( &NtAuthority, // if (!AllocateAndInitializeSid( &NtAuthority,
2, // 2,
SECURITY_BUILTIN_DOMAIN_RID, // SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS, // DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, // 0, 0, 0, 0, 0, 0,
&AdministratorsGroup)) // &AdministratorsGroup))
{ // {
// Initializing SID Failed. // // Initializing SID Failed.
IsInAdminGroup = FALSE; // IsInAdminGroup = FALSE;
} // }
else // else
{ // {
// Check whether the token is present in admin group. // // Check whether the token is present in admin group.
if (!CheckTokenMembership( NULL, // if (!CheckTokenMembership( NULL,
AdministratorsGroup, // AdministratorsGroup,
&IsInAdminGroup )) // &IsInAdminGroup ))
{ // {
// Error occurred. // // Error occurred.
IsInAdminGroup = FALSE; // IsInAdminGroup = FALSE;
} // }
// Free SID and return. // // Free SID and return.
FreeSid(AdministratorsGroup); // FreeSid(AdministratorsGroup);
} // }
//
if (IsInAdminGroup) // if (IsInAdminGroup)
{ // {
return ExecuteCommand(Owner, szCommand, nShowCmd, L"open"); // return ExecuteCommand(Owner, szCommand, nShowCmd, L"open");
} // }
else // else
{ // {
return ExecuteCommand(Owner, szCommand, nShowCmd, L"runas"); // return ExecuteCommand(Owner, szCommand, nShowCmd, L"runas");
} // }
} //}
HINSTANCE LSExecute(HWND Owner, LPCTSTR szCommand, int nShowCmd) HINSTANCE LSExecute(HWND Owner, LPCTSTR szCommand, int nShowCmd)
{ {

View File

@ -60,7 +60,7 @@ void LogWithArgs(int nLevel, const WCHAR* format, ... ); // Replacement for Debu
void LogError(CError& error); void LogError(CError& error);
HINSTANCE LSExecute(HWND Owner, LPCTSTR szCommand, int nShowCmd); HINSTANCE LSExecute(HWND Owner, LPCTSTR szCommand, int nShowCmd);
HINSTANCE LSExecuteAsAdmin(HWND Owner, LPCTSTR szCommand, int nShowCmd); //HINSTANCE LSExecuteAsAdmin(HWND Owner, LPCTSTR szCommand, int nShowCmd);
HINSTANCE ExecuteCommand(HWND Owner, LPCTSTR szCommand, int nShowCmd, LPCTSTR szVerb); HINSTANCE ExecuteCommand(HWND Owner, LPCTSTR szCommand, int nShowCmd, LPCTSTR szVerb);
WCHAR* GetString(UINT id); WCHAR* GetString(UINT id);

View File

@ -3621,20 +3621,23 @@ LRESULT CMeterWindow::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam)
{ {
if (wParam == ID_CONTEXT_SKINMENU_EDITSKIN) if (wParam == ID_CONTEXT_SKINMENU_EDITSKIN)
{ {
std::wstring command = m_Rainmeter->GetConfigEditor() + L" \""; std::wstring command = m_SkinPath + m_SkinName;
command += m_SkinPath;
command += m_SkinName;
command += L"\\"; command += L"\\";
command += m_SkinIniFile; command += m_SkinIniFile;
HANDLE file = CreateFile(command.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
command.insert(0, L" \"");
command.insert(0, m_Rainmeter->GetConfigEditor());
command += L"\""; command += L"\"";
// If the skins are in the program folder start the editor as admin if (file == INVALID_HANDLE_VALUE)
if (m_Rainmeter->GetPath() + L"Skins\\" == m_Rainmeter->GetSkinPath())
{ {
LSExecuteAsAdmin(NULL, command.c_str(), SW_SHOWNORMAL); // File is in protected location, so execute as admin
ExecuteCommand(NULL, command.c_str(), SW_SHOWNORMAL, L"runas");
} }
else else
{ {
CloseHandle(file);
LSExecute(NULL, command.c_str(), SW_SHOWNORMAL); LSExecute(NULL, command.c_str(), SW_SHOWNORMAL);
} }
} }