15

I need to run executable for custom setup/tear down, after install and before uninstall. It needs to run with elevated privileges. How to do this correctly?

3 Answers 3

20

Have a look at this blog at the section How to author custom actions that require administrative privileges

An other link that really explains all the types of Custom Actions. The CustomAction Element in Wix.

This should help you a bit more.

After looking at your Solution you seem to be doing a Type 18 CustomAction, here I pasted the contents of the previous Blog for those types:

Custom Action Type 18 Calls an executable which is installed with the application during current session. The Source column in the CustomAction table contains the key to the record in the File table.

The Target column in the CustomAction table contains the command line string for the executable. All return processing, execution scheduling, and in-script execution options apply.

Because file is installed with the application, there are sequencing restrictions on custom action Type 18:

If the source file is not already installed on the computer:
    Custom action must be sequenced after CostFinalize action because only after this action path to the file can be resolved.
If the source file is not already installed on the computer:
    Deferred custom actions of this type must be sequenced after the InstallFiles action.
    Non-deferred custom actions of this type must be sequenced after the InstallFinalize action.

Entry point to the custom action receives the handle to the installation session. During execution of deferred custom action, session may no longer exist. To get the value of properties use CustomActionData property.

Here is how to add Type 18 custom action in Wix:

<Directory Id="TARGETDIR" Name="SourceDir">
  <Component Id="Component1"
             Guid="*">
    <File Id="MyCA" Name="MyCA.exe" />
  </Component>
</Directory>

<CustomAction Id="DoSomething"
              FileKey="MyCA"
              ExeCommand="-switch"
              Execute="deferred"
              Return="check"
              HideTarget="no"
              Impersonate="no" />

<InstallExecuteSequence>
  <Custom Action="DoSomething" Before="InstallFinalize" />
</InstallExecuteSequence>

First, we add MyCA.exe to the File table.

We also add a custom action of Type 18 to the CustomAction table. FileKey attribute points to the element with the custom action dll. ExeCommand attribute specifies the command line string for the executable.

The last thing to do is to schedule our custom action in all required sequence tables.

This should help you out, sort what is missing, but I strongly suggest that you look at all the types of Custom Actions that will help you later on when making more installers

Sign up to request clarification or add additional context in comments.

4 Comments

This is not enough to make it working. It's necessary to install custom action into the right sequence point and use right conditions to deal with upgrade scenario, repair, etc. :(
But this answers your question. It gives a clear guideline of how to run CA elevated, and you are free to develop the idea further to build what you need.
@Yan Sklyarenko, it doesn't. This is 10% of real answer.
@user626528 What are the conditions and what custom actions are you trying to do?
14

So, the final solution was like this:

<CustomAction Id="Install" Directory="APPLICATIONROOTDIRECTORY"
              Execute="deferred" Impersonate="no" Return="ignore"
              ExeCommand="[APPLICATIONROOTDIRECTORY]MyExeName.exe -install" />

<CustomAction Id="Uninstall" Directory="APPLICATIONROOTDIRECTORY"
              Execute="deferred" Impersonate="no" Return="ignore"
              ExeCommand="[APPLICATIONROOTDIRECTORY]MyExeName.exe -uninstall" />

<InstallExecuteSequence>

  <Custom Action='Install' After='InstallFiles' >
    $ProductComponent = 3
  </Custom>

  <Custom Action='Uninstall' After='InstallInitialize' >
    ?ProductComponent = 3
  </Custom>

</InstallExecuteSequence>

Any advise to improve it?

2 Comments

what is ProductComponent??
It's an ID defined by the particular MSI that the snippet it from. Given the ID, $ProductComponent uses an action prefix to tell you the component's action state. I don't think that checking the installed state of the component will detect uninstall with no false positives, however.
5

You could add 'NOT REMOVE' for Install & Repair Sequence. And 'Installed AND (REMOVE = "ALL")' only for UnInstall sequence.

    <InstallExecuteSequence>
      <Custom Action='Install' After='InstallFiles' >
        NOT REMOVE
      </Custom>

      <Custom Action='Uninstall' After='InstallFiles' >
         Installed AND (REMOVE = "ALL")
      </Custom>

    </InstallExecuteSequence>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.