Search This Blog

Showing posts with label InstallShield. Show all posts
Showing posts with label InstallShield. Show all posts

Saturday, October 24, 2009

Using VB to show IIS sites within Installshield 2010

Quite often I have worked on projects where an installer is needed.  So far I am quite familiar with Installshield and its inner workings so this blog describes a simple piece of code which solves a generic issue I had.

More often than not we program in a web based world, a web based installer is needed and the obvious incline is to use IIS, well at least for me considering I am mainly of a .Net developer.  In Installshield everything is easy when it comes to installing something on the local IIS but it gets complicated when you want to customise this process and allow users to choose the website within which the application will be installed or even the name of the application.

Customising the name of the application is easy and can be done simply by using what Installshield refers to as “properties”.  Expose a simple edit box and then in your “Internet Information Services” node simply change the “Name” to point to the property mapped to the edit box (e.g. “[MY_VIRTUAL_DIRECTORY]”).

Things get complicated when you want to show the user a list of websites to which your application can be added to, by default it goes to the “Default Web Site” or at least that’s what I assume but not always as stated by this table here.

Saying that, there is no easy way of showing the user a list of already existing websites on the local computer so after a bit of hunting around I found several solutions on the Installshield forums which advised me of a particular VB script which does what we need.

Dim objViewDim objDB
Dim objInstaller
Dim objRecord
Dim objW3SVC, objIisWebSite
Dim lngOrder
Set objDB = Session.DatabaseSet
objInstaller = Session.Installer

'Delete any records that may exist
Set viewList = Database.OpenView("SELECT * FROM ComboBox")
viewList.ExecuteSet recList = viewList.Fetch

'delete any existing records
While Not (recList Is Nothing)
viewList.Modify 6, recList ' 6 = deleteSet
recList = viewList.Fetch
Wend
viewList.CloseSet

objView = objDB.OpenView("select * from ComboBox")

' enumerate webservers on localhost and populate ComboBox tableSet
objW3SVC = GetObject("IIS://localhost/W3SVC")
lngOrder = 1


For Each objIisWebSite In objW3SVC
If objIisWebSite.Class = "IIsWebServer" Then
If LCase(objIisWebSite.ServerComment) <> "administration web site" Then
' add site name to ComboBox table (which is used by our dialog ComboBox)
Set objRecord = objInstaller.CreateRecord(4)
objRecord.StringData(1) = "WEBSITE_DROPDOWN" ' property name
objRecord.IntegerData(2) = lngOrder ' order
objRecord.StringData(3) = objIisWebSite.Name ' value
objRecord.StringData(4) = objIisWebSite.ServerComment ' text
Call objView.Execute(objRecord) ' now add the record to the table
Call objView.Modify(7, objRecord) ' (7 = msiViewModifyInsertTemporary)

lngOrder = lngOrder + 1
End If
End If
Next
Call objView.Close

Note that the value of the combobox is “objIisWebSite.Name” and you might think this is the human readable string which appears as the website name, but its not.  The name of the website is a unique number (ID) assigned to it as shown in the figure below:

image

Therefore when you want to specify where your application will be added to, you have to modify the “Default Web Site” node (which you will add as the default behaviour and put all your applications under it).  A bit counter intuitive as earlier I simply thought that instead of naming it “Default Web Site” I have to map it to my property which corresponds to the drop down list, i.e. "WEBSITE_DROPDOWN”.  But the trick is to associate the “WEBSITE_DROPDOWN” property to the “Site Number” setting in the Default Web Site node.

Monday, March 30, 2009

Common InstallShield functions and their Usage

Getting a property string

   1: NUMBER nvBufferLen;
   2: STRING svInstallDir;
   3: MsiGetProperty(hMSI, "INSTALLDIR", svInstallDir, nvBufferLen);

Where:
hMSI = handle of the MSI (gets automatically passed into functions)
INSTALLDIR = The name of the property to get
svInstallDir = The variable in which to store the returned property value
nvBufferLen = The length of the buffer, often set to 255.


Setting a property string 

   1: MsiSetProperty(hMSI, "INSTALLDIR", "C:\Program Files\InstallationDirectory");

Where:
hMSI = handle of the MSI (gets automatically passed into functions)
INSTALLDIR = The name of the property to set
Value = The value of the property to set to


Converting a number to string and vice versa

   1: number nTemp;
   2: string svTemp; 
   3: nTemp = 25;
   4:  
   5: NumToStr(svTemp, nTemp); // 25 -> "25" 
   6: StrToNum(nTemp, svTemp); // "25" -> 25

String comparison

   1: if (StrCompare("String A", "String B") == 0) then
   2:     // do something here
   3: endif;

String concatenation

   1: STRING svTemp;
   2: Sprintf(svTemp,  
   3:     "A string taking two parameters %s %s",
   4:     "Parameter 1", "Parameter 2");

String Trim(…) (Extracted from http://community.installshield.com/showthread.php?t=24423)

   1: function Trim(aString)   
   2:     BOOL done;    
   3:     NUMBER startIndex;   
   4:     NUMBER endIndex;    
   5:     NUMBER aByte;
   6: begin    
   7:     startIndex = 0;  
   8:     endIndex = StrLength( aString ) - 1;
   9:     done = ( startIndex > endIndex );
  10:     while ( !done )
  11:         GetByte( aByte, aString, startIndex );        
  12:         if ( aByte = ASCII_SPACE ) then
  13:             startIndex = startIndex + 1;
  14:             done = ( startIndex > endIndex );
  15:         else
  16:             done = TRUE;
  17:         endif;
  18:     endwhile;
  19:     done = ( startIndex > endIndex );
  20:  
  21:     while ( !done )
  22:         GetByte( aByte, aString, endIndex );
  23:         if ( aByte = ASCII_SPACE ) then
  24:             endIndex = endIndex - 1;
  25:             done = ( startIndex > endIndex );
  26:         else
  27:             done = TRUE;
  28:         endif;
  29:     endwhile;
  30:  
  31:     if ( startIndex > endIndex ) then
  32:         aString = "";
  33:     else
  34:         StrSub( aString, aString, startIndex, endIndex - startIndex + 1 );    
  35:     endif;
  36:     return 0;
  37: end;


Reading XML using MS XML DOM 6 (Inspired by http://community.installshield.com/showthread.php?t=101600&page=2)


   1: OBJECT oDoc, oNode, oNodeList;
   2: NUMBER nvBufferLen, nTemp;
   3: STRING svXPathQuery, svValue, svTemp, svMessage, svError, svLine, svLinePos;
   4:  
   5: set oDoc = CreateObject("Msxml2.DOMDocument.6.0");  
   6: if (IsObject(oDoc) = FALSE) then
   7:     MessageBox("MSXML DOM-Document Connection Failed", 0);
   8:     return -1;
   9: endif;
  10:       
  11: oDoc.async = FALSE;  
  12: oDoc.setProperty("SelectionLanguage", "XPath");
  13: oDoc.load( svInstallDir ^ UI_APP_CONFIG );
  14:  
  15: if ( oDoc.parseError.errorCode != 0 ) then
  16:     NumToStr ( svError , oDoc.parseError.errorCode );
  17:     NumToStr ( svLine , oDoc.parseError.line );
  18:     NumToStr ( svLinePos , oDoc.parseError.linepos );
  19:     MessageBox ( "The XML File did not load correctly.\n" + 
  20:                  "Error Code: " + svError + "\n" +
  21:                  "Error Line: " + svLine + "\n" + 
  22:                  "Error Line Position: " + svLinePos + "\n" +
  23:                  oDoc.parseError.reason , 0 );
  24: else 
  25:     svXPathQuery = "//XPathQuery";
  26:  
  27:     try 
  28:         set oNodeList = objDOMDOC.selectNodes( strXPathQuery );
  29:     catch
  30:         nTemp = Err.Number; 
  31:         NumToStr(svTemp, nTemp);
  32:         MessageBox("Error while selecting " + strXPathQuery + " (Error + " + svTemp + ")", 0);
  33:     endcatch;
  34:     
  35:     if (oNodeList.length == 0) then
  36:        MessageBox( "No " + strXPathQuery + " elements found.", 0 );
  37:     else 
  38:         for i = 0 to ( oNodeList.length - 1 );
  39:             set oNode = oNodeList.nextNode;
  40:             try
  41:                 svValue= oNode.text;
  42:                 //svValue = oNode.attributes.getNamedItem( strAttributeName ).value; // to get attribute value
  43:             catch
  44:                 MessageBox("No value found for " + strXPathQuery, 0);
  45:             endcatch;
  46:         endfor;
  47:     endif;
  48: endif;