Electron在Windows下以管理员运行

为了实现在windows的UAC控制下以管理员方式运行,通常有两种方式:

  • 注册表添加信息。这种方式通常是通过安装程序添加注册表信息。
  • exe文件中附加manifest文件。这种方式使得程序可以以免安装的方式运行。

由于electron通常开发的程序我们通常喜欢用免安装方式运行,下面就说说怎么修改electron中的manifest。
通过win sdk中的mt.exe工具可方便的对exe文件中附加的manifest进行导出和导入。由于electron中本身就已经附加了manifest文件,所以我们不能简单粗暴的创建一个manifest文件附加进去,需要先导出原来的manifest,修改后再导入。
导出manifest

mt.exe -inputresource:electron.exe;#1 -out:manifest.xml

修改manifest


<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <dependency>
        <dependentAssembly>
            <assemblyIdentity type="Win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*">assemblyIdentity>
        dependentAssembly>
    dependency>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                
                
                
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
                
            requestedPrivileges>
        security>
    trustInfo>
    <asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
        <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
            <dpiAware>truedpiAware>
                <disableWindowFiltering xmlns="http://schemas.microsoft.com/SMI/2011/WindowsSettings">truedisableWindowFiltering>
        asmv3:windowsSettings>
    asmv3:application>
    <ms_compatibility:compatibility xmlns:ms_compatibility="urn:schemas-microsoft-com:compatibility.v1" xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <ms_compatibility:application xmlns:ms_compatibility="urn:schemas-microsoft-com:compatibility.v1">
            <ms_compatibility:supportedOS xmlns:ms_compatibility="urn:schemas-microsoft-com:compatibility.v1" 
                Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}">ms_compatibility:supportedOS>
            <ms_compatibility:supportedOS xmlns:ms_compatibility="urn:schemas-microsoft-com:compatibility.v1" 
                Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}">ms_compatibility:supportedOS>
            <ms_compatibility:supportedOS xmlns:ms_compatibility="urn:schemas-microsoft-com:compatibility.v1" 
                Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}">ms_compatibility:supportedOS>
            <ms_compatibility:supportedOS xmlns:ms_compatibility="urn:schemas-microsoft-com:compatibility.v1" 
                Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}">ms_compatibility:supportedOS>
            <ms_compatibility:supportedOS xmlns:ms_compatibility="urn:schemas-microsoft-com:compatibility.v1" 
                Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}">ms_compatibility:supportedOS>
        ms_compatibility:application>
    ms_compatibility:compatibility>
assembly>

导入manifest

mt -manifest manifest.xml -outputresource:electron.exe;1

Ok,搞定收工!

注: mt.exe通常在C:\Program Files (x86)\Windows Kits\8.1\bin\x64\目录下。
参考: http://layer0.authentise.com/electron-and-uac-on-windows.html

你可能感兴趣的:(electron)