[mw_shl_code=css,true]BOOL CSkipUACDemoApp::InitInstance()
{
if (IsEnableUAC() && !IsProcessRunAsAdmin())
{
BOOL bSkipUAC = FALSE;
HINSTANCE hInstance = 0;
if(bSkipUAC )//跳过
{
MessageBox(NULL,_T("跳过UAC"),_T("提示"),MB_OK);
//跳过UAC弹窗关键部分,通过运行计划任务实现跳过UAC弹窗
hInstance = ShellExecute(NULL,_T("open"),_T("schtasks.exe"),_T(" /run /TN \"SkipUAC\""),NULL,SW_HIDE);
}
if (!bSkipUAC || ((int)hInstance <= 32))
{
MessageBox(NULL,_T("进行提权操作"),_T("提示"),MB_OK);
RunElevated(NULL,_T("进程全路径"));
}
return FALSE;
}
MessageBox(NULL,_T("高权限运行程序"),_T("提示"),MB_OK); [/mw_shl_code]
then
[mw_shl_code=css,true] BOOL
RunElevated( HWND hwnd,
LPCTSTR pszPath,
LPCTSTR pszParameters = NULL,
LPCTSTR pszDirectory = NULL )
{
SHELLEXECUTEINFO shex;
memset( &shex, 0, sizeof( shex) );
shex.cbSize = sizeof( SHELLEXECUTEINFO );
shex.fMask = 0;
shex.hwnd = hwnd;
shex.lpVerb = _T("runas");
shex.lpFile = pszPath;
shex.lpParameters = pszParameters;
shex.lpDirectory = pszDirectory;
shex.nShow = SW_NORMAL;
return ::ShellExecuteEx( &shex );
}
BOOL IsProcessRunAsAdmin()
{
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
BOOL bIsAdmin = AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup);
if (bIsAdmin)
{
CheckTokenMembership(NULL, AdministratorsGroup, &bIsAdmin);
FreeSid(AdministratorsGroup);
}
return bIsAdmin == TRUE ;
}
BOOL IsEnableUAC(void)
{
BOOL bEnableUAC = FALSE;
OSVERSIONINFO ovi = {0};
ovi.dwOSVersionInfoSize = sizeof(ovi);
if (::GetVersionEx(&ovi))
{
// window vista or windows server 2008 or later operating system
if ( ovi.dwMajorVersion > 5 )
{
HKEY hKey = NULL;
DWORD dwType = REG_DWORD;
DWORD dwEnableLUA = 0;
DWORD dwSize = sizeof(DWORD);
LSTATUS lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\\"),
0, KEY_READ, &hKey);
if( ERROR_SUCCESS == lRet )
{
lRet = ::RegQueryValueEx(hKey, _T("EnableLUA"), NULL, &dwType, (BYTE*)&dwEnableLUA, &dwSize);
::RegCloseKey(hKey);
if( ERROR_SUCCESS == lRet )
{
bEnableUAC = (dwEnableLUA) ? TRUE : FALSE;
}
}
}
}
return bEnableUAC;
} [/mw_shl_code]
|