方法1.右击程序-->以管理员模式运行程序.
方法2:打开vs2008选中项目右击-->属性-->配置属性-->连接器-->清单文件-->UAC 执行级别.改成requireAdministrator
但是一些盗版操作系统可能没有管理员模式.还是会有少数盗版系统出现此类问题.
方法3:使用manifest文件控制
方法4:创建一个新的使用管理员权限的进程
CreateProcessAsUser()和ShellExecuteEx() 参考http://wenku.baidu.com/view/942efc220722192e4536f63a.html
VC2005或2008可用上述2方法,而VC6可用以下方法:
(转自 http://www.slyar.com/blog/mfc-requireadministrator.html)
1、打开工程的资源视图
2、选择"Insert resource",选择"custom","resource type"填24,资源ID为1,然后把以下内容复制到资源内容中保存(看到的是16进制内容)
<?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="mulitray.exe.manifest" type="win32"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
3、编译连接,此时就可以看到程序图标上出现了小盾牌,运行程序时弹出UAC对话框,获得管理权限
注:
requestedExecutionLevel可以指定为三个值:
asInvoker:默认选项,新的进程将简单地继承其父进程的访问令牌。
highestAvailable:应用程序会选择该用户允许范围内尽可能宽松的安全上下文。对于标准用户来说,该选项与asInvoker一样,而对于管理员来说,这就意味着请求非限制访问令牌.
requireAdministrator:应用程序需要管理员的非限制访问令牌。运行该程序时,标准用户将要输入管理员的用户名和密码,而管理员则要在弹出的确认对话框中进行确认。
/判断是否有admin权限,vs2008以上才能运行
BOOL IsRunAsAdmin()
{
BOOL fIsRunAsAdmin = FALSE;
DWORD dwError = ERROR_SUCCESS;
PSID pAdministratorsGroup = NULL;
// Allocate and initialize a SID of the administrators group.
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
if (!AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pAdministratorsGroup))
{
dwError = GetLastError();
goto Cleanup;
}
// Determine whether the SID of administrators group is enabled in
// the primary access token of the process.
if (!CheckTokenMembership(NULL, pAdministratorsGroup, &fIsRunAsAdmin))
{
dwError = GetLastError();
goto Cleanup;
}
Cleanup:
// Centralized cleanup for all allocated resources.
if (pAdministratorsGroup)
{
FreeSid(pAdministratorsGroup);
pAdministratorsGroup = NULL;
}
// Throw the error if something failed in the function.
if (ERROR_SUCCESS != dwError)
{
throw dwError;
}
return fIsRunAsAdmin;
}