原文链接:http://blog.youkuaiyun.com/limiko/article/details/5713182
红色部分为转载时修改的。
在Win7系统下,如果需要进行读写删除C盘文件等操作,则需要应用程序以管理员身份运行(点击应用程序,右键,以管理员身份运行)。
而要从代码角度解决这个问题,则一般可以考虑添加一个manifest文件来解决。
VS2008前的版本需要添加一个manifest文件,内容如下:
<?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>
保存为UAC.manifest,在rc文件中添加以下内容
1 24 "UAC.manifest"
(VS2005可能会报错,在rc上清除以上代码,使用项目属性添加的方法项目属性->manifest tool->input and output)
VS2008中就相对简单的多了,只需要动手选择一下就行,项目属性->配置属性->连接器->manifest file : 将UAC Execution Level勾选为requireAdministrator
按照这个方法测试,表明:
(1)如果一个exe调用一个DLL,在DLL里面有对C盘文件的操作,则需要在exe上修改UAC Execution Level选项
(2)如果一个exe创建另外一个进程,则需要在父进程上修改UAC Execution Level选项,在子进程上修改不起作用