In VC, set the permission to open a file as administrator.

Source: Internet
Author: User

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.

 

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.