1)ShellExecuteEx启动进程提权限
#include <stdio.h>
#include<windows.h>
#include<tchar.h>
int _tmain(int argc,TCHAR* argv[])
{
SHELLEXECUTEINFO sei={sizeof(SHELLEXECUTEINFO)};
sei.lpVerb=TEXT("runas");
sei.lpFile=TEXT("cmd.exe"); //add application which you want to run as administrator here
sei.nShow=SW_SHOWNORMAL; //without this,the windows will be hiden
if(!ShellExecuteEx(&sei))
{
DWORD dwStatus=GetLastError();
if(dwStatus==ERROR_CANCELLED){
printf("提升权限被用户拒绝\n");
}
else if(dwStatus==ERROR_FILE_NOT_FOUND)
{
printf("所要执行的文件没有找到\n");
}
}
return 0;
}
2)工程加入清单 app.manifest(C++builder中 Application –> Runtime Themes –> Use custom mainfest),使用 Resource Hacker 软件打开 EXE 文件可以看到内嵌的 manifest (最后一栏)。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity type="win32" name="App" version="3.1.0.0" processorArchitecture="*"/>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" publicKeyToken="6595b64144ccf1df" language="*" processorArchitecture="*"/>
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!--The ID below indicates application support for Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
<!--The ID below indicates application support for Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<!--The ID below indicates application support for Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
<!--The ID below indicates application support for Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!--The ID below indicates application support for Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
</application>
</compatibility>
</assembly>
3)VC/S 中
1
找到VS2010的快捷方式:右击——“打开文件位置”
找到VS2010的启动项目devenv.exe:右击——属性——兼容性——特权等级,以管理员权限运行;如果需要每个用户都以管理员权限运行,还可以“更改所有用户的设置”——特权等级,以管理员权限运行。
然后在项目的打开方式中确保以VS2010为默认打开程序就好了。
2
属性–连接器–清单文件-》UAC执行级别-》requireAdministrator
(/level=’requireAdministrator’)