Running compiled python (py2exe) as administrator in Vista

帅比萌擦擦* 提交于 2019-11-27 01:46:46

Do you mean that you want Windows to prompt for elevation when your program is run? This is controlled by adding a UAC manifest to the EXE's resources. This blog entry explains how to create the manifest and how to compile it into a .RES file.

I don't know what facilities py2exe has for embedding custom .RES files, so you might need to use the MT.EXE tool from the Platform SDK to embed the manifest in your program. MT.EXE doesn't need .RES files; it can merge the .manifest file directly.

Ivaylo

Following the examples from Python2x\Lib\site-packages\py2exe\samples\user_access_control just add uac_info="requireAdministrator" to console or windows dict:

windows = [{
    'script': "admin.py",
    'uac_info': "requireAdministrator",
},]

Following up Roger Lipscombe's comment, I've used a manifest file in py2exe without any real knowledge of what I was doing. So this might work:

# in setup.py
# manifest copied from http://blogs.msdn.com/shawnfa/archive/2006/04/06/568563.aspx
manifest = '''
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
       <asmv3:trustInfo xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
         <asmv3:security>
           <asmv3:requestedPrivileges>
             <asmv3:requestedExecutionLevel
               level="asInvoker"
               uiAccess="false" />
           </asmv3:requestedPrivileges>
         </asmv3:security>
       </asmv3:trustInfo>
     </assembly>
'''

setup(name='MyApp',
      #...
      windows=[ { #...
                  'other_resources':[(24, 1, manifest)],
                 }]
     )

You may need to do some fiddling though..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!