Friday, July 13, 2012

How to Quit Installation from VbScript Custom Action?

If you are using VBScript Custom Action and want to Quit installation, here is a little trick:

Simply return 3 from your method/function and let your Custom Action "check" the return code. Whenever your Custom Action would get 3 from method/function, it would immediately Quit. Here is some sample code:

In WIX, declare your Custom Action like this: Note the value for “Return” attribute:
WIX: Custom Action Declaration
  1.  <CustomAction Id="CheckAutoCAD"
  2.    BinaryKey="MyCustomActions.vbs"
  3.    VBScriptCall="GetInstalledAutoCAD"
  4.    Execute="immediate"
  5.    Return="check"
  6. />


Here is the VbScript function which would return 3 if it fails. And as Custom Action would ‘check’ for return code, it will read the return code:
VBScript Function
  1. Public Function GetInstalledAutoCAD()
  2.     Const IDABORT = 3'If function returned this, MSI installation would be terminated immediately
  3.      'statements
  4.      'statements
  5.      'statements
  6.      if Not AutoCADFound then
  7.         Msgbox "Setup cannot find any required AutoCAD installation." & VbCrlf & _
  8.             "Please install AutoCAD before installing this product",
  9.             16,
  10.             "Missing Prerequiste"
  11.     
  12.         GetInstalledAutoCAD=IDABORT 'this would return 3 and may cause msi installer to quite immediately.
  13.      else
  14.         GetInstalledAutoCAD=0
  15.      end if
  16. End Function

BTW, I never tried same with any JScript, but I think, if same would be returned by JScript, installer would quit as well.

No comments: