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:
{
std::wstring command = Rainmeter->GetConfigEditor() + L" \"";
command += Rainmeter->GetSkinPath();
command += m_SkinName;
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, FILE_ATTRIBUTE_NORMAL, NULL);
command.insert(0, L" \"");
command.insert(0, Rainmeter->GetConfigEditor());
command += L"\"";
// If the skins are in the program folder start the editor as admin
if (Rainmeter->GetPath() + L"Skins\\" == Rainmeter->GetSkinPath())
if (file == INVALID_HANDLE_VALUE)
{
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
{
CloseHandle(file);
LSExecute(NULL, command.c_str(), SW_SHOWNORMAL);
}
}

View File

@ -289,46 +289,46 @@ HRGN BitmapToRegion(HBITMAP hbm, COLORREF clrTransp, COLORREF clrTolerance, int
return hRgn;
}
HINSTANCE LSExecuteAsAdmin(HWND Owner, LPCTSTR szCommand, int nShowCmd)
{
BOOL IsInAdminGroup = FALSE;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
// Initialize SID.
if (!AllocateAndInitializeSid( &NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup))
{
// Initializing SID Failed.
IsInAdminGroup = FALSE;
}
else
{
// Check whether the token is present in admin group.
if (!CheckTokenMembership( NULL,
AdministratorsGroup,
&IsInAdminGroup ))
{
// Error occurred.
IsInAdminGroup = FALSE;
}
// Free SID and return.
FreeSid(AdministratorsGroup);
}
if (IsInAdminGroup)
{
return ExecuteCommand(Owner, szCommand, nShowCmd, L"open");
}
else
{
return ExecuteCommand(Owner, szCommand, nShowCmd, L"runas");
}
}
//HINSTANCE LSExecuteAsAdmin(HWND Owner, LPCTSTR szCommand, int nShowCmd)
//{
// BOOL IsInAdminGroup = FALSE;
//
// SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
// PSID AdministratorsGroup;
// // Initialize SID.
// if (!AllocateAndInitializeSid( &NtAuthority,
// 2,
// SECURITY_BUILTIN_DOMAIN_RID,
// DOMAIN_ALIAS_RID_ADMINS,
// 0, 0, 0, 0, 0, 0,
// &AdministratorsGroup))
// {
// // Initializing SID Failed.
// IsInAdminGroup = FALSE;
// }
// else
// {
// // Check whether the token is present in admin group.
// if (!CheckTokenMembership( NULL,
// AdministratorsGroup,
// &IsInAdminGroup ))
// {
// // Error occurred.
// IsInAdminGroup = FALSE;
// }
// // Free SID and return.
// FreeSid(AdministratorsGroup);
// }
//
// if (IsInAdminGroup)
// {
// return ExecuteCommand(Owner, szCommand, nShowCmd, L"open");
// }
// else
// {
// return ExecuteCommand(Owner, szCommand, nShowCmd, L"runas");
// }
//}
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);
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);
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)
{
std::wstring command = m_Rainmeter->GetConfigEditor() + L" \"";
command += m_SkinPath;
command += m_SkinName;
std::wstring command = m_SkinPath + m_SkinName;
command += L"\\";
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"\"";
// If the skins are in the program folder start the editor as admin
if (m_Rainmeter->GetPath() + L"Skins\\" == m_Rainmeter->GetSkinPath())
if (file == INVALID_HANDLE_VALUE)
{
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
{
CloseHandle(file);
LSExecute(NULL, command.c_str(), SW_SHOWNORMAL);
}
}