QT使用UAC(经过验证)

网上有很多manifest的版本,mingw与vs系列也有不同的解决方案,不管那么多,我是使用这篇文章解决这个问题的:

So it turns out that I had another bug that caused the non-elevated running branch to run in all cases. The model I described in the post works. To avoid Windows infering the need for elevated permissions, you need to add a manifest resource. (for example, if the name of your application exe contains the word "updater" it will be triggered)

The contents of the manifest are the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="asInvoker" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>

Compiling it to your .exe depends on your compiler and environment, so I'm only showing mine: Qt Creator and mingw-gcc:

Create an rc file for the resources with the following content:

1 24 DISCARDABLE manifest.xml

Add this rc file to your .pro like this:

win32:RC_FILE = resources.rc

After this, ShellExecute without the verb paramter will run without elevation, and using "runas" will run it with elevation.

经过亲自验证,确实有效。我使用win7 x64, mingw, qt5.32,其中参数asInvoker有可能需要改成requireAdministrator

参考:http://stackoverflow.com/questions/7744410/how-to-execute-an-app-without-elevation

---------------------------------------------------------

另一篇探测UAC是否已经开启,有点意思:

http://www.qtcn.org/bbs/read-htm-tid-48310.html

---------------------------------------------------------

这篇比较详细,看上去很认真的样子,但是我没有成功,但是仍然记录一下:

http://blog.chinaunix.net/uid-14281179-id-3394558.html

posted @ 2015-05-30 00:14  findumars  Views(2803)  Comments(0Edit  收藏  举报