VC program running under Win7 UAC permission issues

Win7 run in VC program tend to be insufficient UAC permission issues, the following are a few ways to get administrator privileges

1, through the code lifting user privileges , as follows:

BOOL UpgradeProcessPrivilege(HANDLE hProcess, LPCTSTR lpPrivilegeName )
{
   HANDLE hToken = NULL;
   if(OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken))
   {
       LUID Luid;
      if(LookupPrivilegeValue(NULL, lpPrivilegeName, &Luid))
      {
         TOKEN_PRIVILEGES tp;
         tp.PrivilegeCount = 1;
         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
         tp.Privileges[0].Luid = Luid;
         return( AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) ); 
      }
   }
   return FALSE;
}

// 使用
UpgradeProcessPrivilege(GetCurrentProcess(), SE_DEBUG_NAME);

2, import manifest file

If you are using VC6 or above IDE, resolved by importing the manifest file

<?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="IsUserAdmin"
     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>

Note: This file as long as the extension is .manifest file name can be arbitrary. The name attribute value of the contents of the file can also be arbitrary. (Tested under VC6.0)

The file "import" into the resource file, resource type is 24, the resource ID of 1, Note: The test must be 24 resource type, resource ID must be 1,

Re-edit it, under the windows7 the program icon has a small shield-shaped icon, double-click Run dialog box will appear requires elevated administrator privileges.

3, or if it is vs2008 2010, to execution level connector uac manifest file

Right --- --- direct project properties Connector --- --- uac manifest file execution level selected requireAdministrator recompile

So your program to have administrator privileges to run directly up. Program will be a shield.

Because I use VS2010, compiled using this method several procedures, no problem found

4, modify the registry :

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\Program Files\win7test2.exe"="RUNASADMIN"

Save the contents of the above as a .reg file, based in the C: \ Program Files \ win7test2.exe changed to address real-world applications, double-click to run.

After this method is the application icon does not have a shield-shaped icon, but will still run double-click pop-up dialog box requires elevated privileges.

Reference Links: Win7 run under UAC permission issues VC program

Reproduced in: https: //www.jianshu.com/p/015a5b9c5e28

Guess you like

Origin blog.csdn.net/weixin_34195364/article/details/91153253