Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Elevated priviledges for setting system environment variable (Windows 10) does not work

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

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
qt installer frwindows 10elevatedadministratorsystem environm
1 Posts 1 Posters 616 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SimonSchroeder
    wrote on 20 Aug 2020, 08:00 last edited by SimonSchroeder
    #1

    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".

    1 Reply Last reply
    0

    1/1

    20 Aug 2020, 08:00

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved