user489041
user489041

Reputation: 28294

Run executable as admin in InstallScript

I am using the following script to run an executable as an admin:



#include "ifx.h"
export prototype MyFunction(HWND);


///////////////////////////////////////////////////////////////////////////////
//                                                                           
// Function:  MyFunction
//                                                                           
//  Purpose:  Calls into Companion to execute the detect camera and all init setup 
//            code
//                                                                           
///////////////////////////////////////////////////////////////////////////////
function MyFunction(hMSI)

begin
    if REMOVEALLMODE ==0 then  //only run if they are installing the product
        LAAW_SHELLEXECUTEVERB = "runas"; 
        LaunchApplication (INSTALLDIR ^ "Companion.exe", 
                        "-detect -test -wsdl -exit -nimbus", 
                        "", 
                        SW_NORMAL,
                        0,
                        LAAW_OPTION_WAIT_INCL_CHILD | LAAW_OPTION_USE_SHELLEXECUTE);
    endif;
end;


On XP, the script above will open up a dialog box asking the user which user the executable should be run as. This allows them to select an admin to run the executable. However, on Windows 7, nothing happens. The installation doesn't ask the user for anything and the installation fails.

Any ideas on why this might be or any other suggestions as to how to run an executable as an admin?

Upvotes: 1

Views: 8170

Answers (2)

Nothing 2 Lose
Nothing 2 Lose

Reputation: 190

You can distinguish between XP and all non-XP operating systems like this:

if ( SYSINFO.WINNT.bWinXP ) then
    LAAW_SHELLEXECUTEVERB = "open"; // target PC is on Windows XP
else
    LAAW_SHELLEXECUTEVERB = "runas"; // Windows 7 (or Vista)
endif; 

Here's a Tip from the InstallShield Help Library:

If you are using LAAW_OPTION_USE_SHELLEXECUTE on systems running Windows Vista or later and you want to launch the application using the full administrator account (similar to right-clicking the executable file to be run and clicking Run as Administrator), set LAAW_SHELLEXECUTEVERB to runas before using LaunchApplication in your script:

LAAW_SHELLEXECUTEVERB = "runas";

This ensures that the application is always run with full administrator privileges regardless of whether the application to be launched has an application manifest with relevant settings. Note that this may trigger a User Account Control (UAC) prompt for consent or credentials.

On systems running operating systems earlier than Windows Vista, if runas is used, a Run As dialog box is displayed. The behavior is similar to right-clicking the executable file to be run and clicking Run As. This dialog box enables the end user to select the user account that should be used to run the application.

Upvotes: 0

Bart Gijssens
Bart Gijssens

Reputation: 1612

What happens when you launch the application yourself under Win7, I mean outside of InstallShield?

I am thinking it might be due to UAC. A program launched by an installscript doesn not automatically inherit admin privileges from the installshield script.

Upvotes: 0

Related Questions