Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Elevated priviledges for setting system environment variable (Windows 10) does not work



  • I just have started using the Qt Installer Framework (current version 3.2 from Qt's online installer, Windows 10). I have spent hours trying to figure out why setting the PATH environment variable system wide fails when the installer was not started using "Run as administrator". Maybe someone can help me out.

    I have added <RequireAdminRights>true</RequireAdminRights> to my package.xml. I call installer.gainAdminRights() in my installer script – just in case. And I use component.addElevatedOperation("EnvironmentVariable", ...) to adjust the environment variable. Now, I am out of ideas how to get this to work when the user just double clicks the installer.

    Another approach would be if the installer launched as administrator automatically. From what I have read so far this is not possible. Or maybe there is a way to check if the installer has admin rights at the beginning in order to stop the installer with a message to run it as administrator. BTW, copying my files to C:\Program Files (which needs admin priviledges) works.

    Here is a minimal example to test this:
    config/config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Installer>
    	<Name>Test</Name>
    	<Version>1.0.0</Version>
    	<Title>Test Installer</Title>
    	<Publisher>me</Publisher>
    	<ProductUrl>https://example.com</ProductUrl>
    	<RunProgram>@TargetDir@/bin/MyApp</RunProgram>
    	<RunProgramDescription>Run MyApp now.</RunProgramDescription>
    	<MaintenanceToolName>MyAppMaintenanceTool</MaintenanceToolName>
    	<AllowNonAsciiCharacters>true</AllowNonAsciiCharacters>
    	<StartMenuDir>MyApp</StartMenuDir>
    	<TargetDir>@ApplicationsDirX64@/MyApp</TargetDir>
    	<Translations>
    	</Translations>
    </Installer>
    

    packages/com.example.test/meta/package.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Package>
    	<DisplayName>MyApp @ProductVersion@</DisplayName>
    	<Description>Install MyApp @ProductVersion@ with all necessary components.</Description>
    	<Version>1.0.0</Version>
    	<ReleaseDate>2020-08-18</ReleaseDate>
    	<Default>true</Default>
    	<Script>installscript.qs</Script>
    	<RequireAdminRights>true</RequireAdminRights>
    </Package>
    

    packages/com.example.test/meta/installscript.qs

    function Component()
    {
    }
    
    Component.prototype.createOperations = function()
    {
        // call default implementation
        installer.gainAdminRights();
        component.createOperations();
    
        var path = installer.environmentVariable("PATH");
        var mybin = installer.value("TargetDir") + "\\bin";
    
        if(installer.isInstaller())
        {
            // add mybin to the front
            path = path.replace(mybin,"");
            path = mybin + ";" + path;
    
            component.addElevatedOperation("EnvironmentVariable", "PATH",
                                                                  path,
                                                                  true,     // persistent
                                                                  true);    // system
        }
        else if(installer.isUninstaller())
        {
            // delete mybin from path
            path = path.replace(mybin,"");
    
            component.addElevatedOperation("EnvironmentVariable", "PATH",
                                                                  path,
                                                                  true,     // persistent
                                                                  true);    // system
        }
    }
    

    Create installer:

    C:\Qt\Tools\QtInstallerFramework\3.2\bin\binarycreator.exe -c .\config\config.xml -p packages  installer.exe
    

    Double-click installer (not running as administrator), click through it => error on install when trying to change the environment variable. Works when using "Run as administrator".