python - 使用 PyInstaller onefile 选项和 list 将 UAC 设置为 requireAdministrator

标签 python windows visual-studio pyinstaller

好吧,我一直在四处寻找,试图找出这个问题。我正在构建一个名为 GraphicScriptWizard.exe 的应用程序使用 PyInstaller 2.0 版,使用 -i -F -w 和 -m 选项。

我定义为与 -m 选项一起使用的 list 文件称为 GraphicScriptWizard.exe.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="GraphicScriptWizard"
     type="win32"/> 

  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
        </requestedPrivileges>
       </security>
  </trustInfo>
</assembly>

使用此 list 和命令行选项,我没有得到提示提升的可执行文件。

为了完整起见,Pyinstaller 生成的规范文件是:

# -*- mode: python -*-
a = Analysis(['GraphicScriptWizard.py'],
             pathex=[<Full Development Path>],
             hiddenimports=[],
             hookspath=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name=os.path.join('dist', 'GraphicScriptWizard.exe'),
          debug=False,
          strip=None,
          upx=True,
          console=False , icon='SP.ico', manifest='GraphicScriptWizard.exe.manifest')
app = BUNDLE(exe,
             name=os.path.join('dist', 'GraphicScriptWizard.exe.app'))

我试过在没有 -m 选项的情况下使用 pyinstaller 进行编译,并使用以下命令嵌入 mt:

mt.exe -manifest GraphicScriptWizard.exe.manifest -outputresource:GraphicScriptWizard.exe;#1

当我这样做时,应用程序会提示我进行提升,但程序运行时出现错误:

"Cannot open self <Full path to exe>\GraphicScriptWizard.exe or archive..."

我真的无计可施,希望更熟悉 Windows 资源和 list 的人可以为我提供更多信息。我的 list XML 有误吗?我使用 Pyinstaller 的方法有误吗?

最佳答案

我自己刚刚走过这条路,以下是我的观察和经验。

首先要知道 list 有两个有效位置:

  1. 作为资源嵌入到可执行文件(或dll)中

  2. 在可执行文件(或 dll)旁边,就像您当前所做的那样。

在这里阅读 list :http://msdn.microsoft.com/en-us/library/windows/desktop/aa374191(v=vs.85).aspx

根据我的经验,您可以同时使用两者,但如果有任何重叠,嵌入式 list 优先。这就是把你搞砸的原因。 Pyinstaller 创建的可执行文件有一个嵌入式 list ,它将请求的执行级别设置为“asInvoker”,它会覆盖您正在使用的 list 中的级别。

--manifest(或 EXE() 的 list 参数)仅修改 Pyinstaller 放置在可执行文件/dll 旁边的 list ,而不是嵌入式 list 。

因此,我们可以求助于 mt.exe 来更改嵌入式 list ,但正如您所发现的,这会导致应用程序无法运行。这是因为 Pyinstaller 创建的应用程序实际上是两部分;一个提取存档的小型可执行文件,然后在其捆绑的 python 环境中设置并启动您的代码,以及可执行文件操作的存档。这是可行的,因为可执行文件的规范允许在可执行文件的末尾附加任意数据,但在可执行文件本身之外,如可执行文件头中记录的大小所定义的那样。当我们在 Pyinstaller 创建的可执行文件上运行 mt.exe 时,它​​会查看 header 以获取大小,并忽略除此之外的任何内容,从而在使用新 list 保存可执行文件时丢弃该存档数据,这会导致您遇到错误看到了。

我使用的解决方案是在存档数据附加到可执行文件之前修改 list ,这需要修改 Pyinstaller 源代码。 Pyinstaller 源具有实用程序来更新可执行文件/dll 中的资源,它用作构建过程的一部分。您需要查看 Pyinstaller 位置中的 build.pywinmanifest.pywinresource.py。我向 EXE 类添加了一个参数,然后在该类的 assemble 方法中添加了一个步骤来更新 list ,我在附加存档之前就这样做了。我添加的内容是这样的:

if self.manifest_override != None:
        print "Overriding default manifest"
        tmpnm = tempfile.mktemp()
        shutil.copy2(exe, tmpnm)
        os.chmod(tmpnm, 0755)
        winmanifest.UpdateManifestResourcesFromXMLFile(tmpnm, self.manifest_override, names=[1], languages=[1033])
        exe = tmpnm
        trash.append(tmpnm)

我把它放在一行之前:exe = checkCache(exe, ... 它应该在上面,但接近 print "Appending archive to EXE"...

这个解决方案对我很有效,但我不是很满意。我宁愿覆盖嵌入的默认 list ,而不是更新它,但到目前为止我的努力没有结果。

抱歉文字墙,但这里发生了很多事情。

关于python - 使用 PyInstaller onefile 选项和 list 将 UAC 设置为 requireAdministrator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13964909/

相关文章:

python - Pyaudio回调模式如何处理in_data?

linux - 如何将远程 Linux 服务器日志文件跟踪到本地 Windows 计算机

java - 重用 SQLite 连接还是每次都重新连接?

c++ - 更改项目以运行 VS2012 C++

visual-studio - Visual Studio 2017 Enterprise 脱机安装失败

python - 在 Python 正则表达式中捕获重复的子模式

python - Matplotlib:在对数对数图中绘制垂直箭头

python - 在 python 列表中抓取唯一的元组,不管顺序如何

windows - Windows 上的 emacs23 : set-face-foreground seems to get forgotten during load of emacs. el

visual-studio - 如何在 Visual Studio 中出现错误波形时显示类型信息 [fsharp]