How the boot program starts with administrator privileges when the system turns on UAC

Source: Internet
Author: User

Preface: This article explains that in the case of Windows system turned on UAC, the boot program needs to start with administrator privileges, the system pops up UAC dialog box, the user agrees to start the program

Process steps:

1. Determine the Windows System version (UAC is not included with Windows XP), and if the system version is higher than Windows XP, perform step 2;

2. Determine if the system turns on UAC, and if it is on, perform step 3;

3. Determine whether the process is currently started with administrator privileges, or if it is not administrator rights, perform step 4;

4. Call the ShellExecuteEx function with special parameters to restart the process.

The following is the reference code:

BOOL Isprocessrunasadmin ()
{
Sid_identifier_authority ntauthority = security_nt_authority;
PSID Administratorsgroup;

BOOL B = AllocateAndInitializeSid (
&ntauthority,
2,
Security_builtin_domain_rid,
Domain_alias_rid_admins,
0, 0, 0, 0, 0, 0,
&administratorsgroup);
if (b) {
CheckTokenMembership (NULL, Administratorsgroup, &b);
Freesid (Administratorsgroup);
}
return b = = TRUE;
}

int main (int argc, char* argv[])
{
Any action on the file is not allowed here, or it will cause the process to start failing
osVersionInfo osvi;
ZeroMemory (&OSVI, sizeof (OSVERSIONINFO));
osvi.dwosversioninfosize = sizeof (OSVERSIONINFO);
GetVersionEx (&OSVI);
if (5 < osvi.dwmajorversion) {
Std::wstring Wstrvalue;

The following function queries the registry, and the reader can implement it itself
int ireturn = Cutility::queryregvalue (HKEY_LOCAL_MACHINE, L "Software\\microsoft\\windows\\currentversion\\policies \\System ", L" EnableLUA ", Wstrvalue);
if (Success = = Ireturn) {
if (1 = = * (int*) wstrvalue.c_str () && false = = Isprocessrunasadmin ()) {
wchar_t Szpath[max_path] = {0};
GetModuleFileName (NULL, szpath, MAX_PATH);
Std::wcout << szpath << Std::endl;

Std::wstring Wstrparams;
for (int i = 1; i < argc; ++i) {
for (int j = 0; J < Std::strlen (Argv[i]); ++j) {
Wstrparams.push_back (static_cast<wchar_t> (argv[i][j));
}
}

Shellexecuteinfo sei = {0};
sei.cbsize = sizeof (SHELLEXECUTEINFOW);
Sei.fmask = See_mask_nocloseprocess | SEE_MASK_FLAG_NO_UI;
Sei.lpfile = szpath;
Sei.lpparameters = Wstrparams.c_str ();
Sei.nshow = Sw_show;
Sei.lpverb = TEXT ("runas");
Sei.lpdirectory = NULL;
BOOL IRet = ShellExecuteEx (&sei);
if (TRUE! = IRet) {
fprintf (stdout, "ShellExecuteEx Failure. Error Code is:%d ", GetLastError ());
}
return 0;
}
} else {
return 0;
}
}

}

How the boot program starts with administrator privileges when the system turns on UAC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.