Set permissions for the current process
Bool cmainframe: enabledebugprivilege (bool fenable)
{
Bool Fok = false;
Handle htoken = NULL;
If (openprocesstoken (getcurrentprocess (), token_adjust_privileges, & htoken ))
{
Token_privileges TP;
TP. privilegecount = 1;
Lookupprivilegevalue (null, se_debug_name, & TP. Privileges [0]. luid );
TP. Privileges [0]. Attributes = fenable? Se_privilege_enabled: 0;
Adjusttokenprivileges (htoken, false, & TP, sizeof (TP), null, null );
Fok = (getlasterror () = error_success );
Closehandle (htoken );
}
Return Fok;
}
The following test is feasible in vs2005:
Generally, an application requires administrator privilege. In XP, no special restriction is required. You only need to log on to the system as an administrator to use the application normally. Because of the unique UAC verification in win7, application development requires special processing. The process is as follows:
1. Create the admin. manifest file in the application source code directory and paste it into the following content for saving:
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<Assembly xmlns = "urn: Schemas-Microsoft-com: ASM. V1" manifestversion = "1.0">
<Assemblyidentity version = "1.0.0.0"
Processorarchitecture = "x86"
Name = "win7privilege"
Type = "Win32"/>
<Description> description of your application </description>
<! -- Identify the application security requirements. -->
<Trustinfo xmlns = "urn: Schemas-Microsoft-com: ASM. V2">
<Security>
<Requestedprivileges>
<Requestedexecutionlevel
Level = "requireadministrator"
UIAccess = "false"/>
</Requestedprivileges>
</Security>
</Trustinfo>
</Assembly>
Change the red part to the corresponding content:
Win7privilege => your application name, for example, myprocedure
Description of your application => your application description, such as this is myprocedure
2. In the application properties-Configuration tool-"Input and Output" option, set "add configuration file" to "Admin. manifest" and click "OK ".
3. regenerate the application.
When an application that has been processed in this way runs under a non-Administrator account under win7, a prompt box is displayed, prompting you to require administrator permissions for the program and whether to continue running. If you select no, the program exits. If you select Yes, the running program is granted administrator privileges.