Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Customizing Windows Installer


March 2001/Customizing Windows Installer/Listing 5

Listing 5: 1stfile.vbs — Inspecting file versions

Option Explicit

Public installer, database



Const msiOpenDatabaseModeReadOnly     = 0





' Connect to Windows Installer object

REM On Error Resume Next

Set installer = Nothing

Set installer = Wscript.CreateObject("WindowsInstaller.Installer")_

    : CheckError



' Open database

Dim databasePath:databasePath = "orca.msi"

Set database = installer.OpenDatabase(databasePath, _

    msiOpenDatabaseModeReadOnly) : CheckError



ListFiles



Wscript.Quit 0



' List all files in database

Sub ListFiles

Dim view, record, afile, aversion, fullmsg

    fullmsg = ""

    Set view = database.OpenView(_

        "SELECT `FileName`,`Version` FROM `File`") : CheckError

    view.Execute : CheckError

    Do

        Set record = view.Fetch : CheckError

        If record Is Nothing Then Exit Do

        afile = record.StringData(1)

        aversion = record.stringdata (2) 

        fullmsg = fullmsg & afile & " " & aversion & vbcrlf

    Loop

    msgbox fullmsg

End Sub







Sub CheckError

    Dim message, errRec

    If Err = 0 Then Exit Sub

    message = Err.Source & " " & Hex(Err) & ": " & Err.Description

    If Not installer Is Nothing Then

        Set errRec = installer.LastErrorRecord

        If Not errRec Is Nothing Then message = message & _

            vbLf & errRec.FormatText

    End If

    Fail message

End Sub



Sub Fail(message)

    Wscript.Echo message

    Wscript.Quit 2

End Sub

Return to article
 

Listing 2: manuf.vbs — Determining the BIOS version

dim Systemset, System, Bios, Manuf, Model, BiosVersion, BiosName



Set SystemSet = GetObject("winmgmts:").InstancesOf _

    ("Win32_ComputerSystem")



for each System in SystemSet

    Manuf = System.Manufacturer

    Model = System.Model

    exit for 

next



for each Bios in GetObject("winmgmts:").InstancesOf ("win32_bios")

        BiosName = Bios.name 

        BiosVersion = Bios.Version 

    exit for

next



if Manuf = "HP" and Left(Model, 9) = "NetServer" and _

        Left(BiosVersion, 9)="05/01/00" then

    Property("NEWBIOS") = "yes"

end if

Msgbox (Property("NEWBIOS")    )

Return to article
 

Listing 3: cadll.cpp — Finding shared files

#include "stdafx.h"

#include <stdio.h>

#include <MsiQuery.h>





BOOL APIENTRY DllMain( HANDLE hModule, 

                       DWORD  ul_reason_for_call, 

                       LPVOID lpReserved

                     )

{

    switch (ul_reason_for_call )

    {

     case DLL_PROCESS_ATTACH:

            break;

     case DLL_PROCESS_DETACH:



            break;

    }

    return TRUE;

}





UINT __stdcall FindShared(MSIHANDLE hInstall)

        

{

    

    HKEY hk;



    long res = RegOpenKeyEx (HKEY_LOCAL_MACHINE, 

        "Software\\MyCompany\\MyProduct\\MyFiles", 

        0, KEY_READ, &hk); 

    

    DWORD inx;

    char thevalue [MAX_PATH] = {0};

    DWORD buflen; 

    DWORD type;

    for (inx=0; 1; inx++ )

    {

    buflen = sizeof(thevalue);

    long regres = RegEnumValue(hk, inx, thevalue, 

        &buflen, NULL, &type, NULL, NULL);

    if (ERROR_SUCCESS==regres)

        {

            _strlwr (thevalue);

            char* pf = strstr (thevalue, "another.exe");

            if (pf!=NULL)

            {

                MsiSetProperty(hInstall, "FOUNDANOTHER", thevalue);

                break;

            }



        }

    else

        break;

    }





    RegCloseKey (hk);



    return 0;



}





//End of File

Return to article
 

Listing 4: findxl.vbs — Figuring out if Excel is installed

Option Explicit

Public installer, state, path

Const msiInstallStateDefault      =  5



' Connect to Windows Installer object

set installer = Wscript.CreateObject("WindowsInstaller.Installer")



state = installer.productstate _

    ("{00000409-78e1-11d2-b60f-006097c998e7}")



if state <> msiInstallStateDefault then 

   MsgBox ("Office not Installed " & state)

   Wscript.quit

end if



path = installer.ComponentPath _

    ("{00000409-78E1-11D2-B60F-006097C998E7}", _

    "{CC29E96F-7BC2-11D1-A921-00A0C91E2AA2}")

if path <> "" then

path = path & " Version " & installer.FileVersion (path, false)

end if

msgbox path

Return to article
 

Listing 5: 1stoff.vbs — Iterating through Installer collections

Option Explicit

Public installer, state, feature, product, component, fullmsg, num

Const msiInstallStateDefault      =  5



' Connect to Windows Installer object

set installer = Wscript.CreateObject("WindowsInstaller.Installer")





fullmsg = ""

for each product in installer.products

fullmsg = fullmsg & product & " " & installer.ProductInfo _

        (product, "InstalledProductName") & vbcrlf

next



msgbox fullmsg



state = installer.productstate _

    ("{00000409-78e1-11d2-b60f-006097c998e7}")



if state <> msiInstallStateDefault then 

   MsgBox ("Office not Installed " & state)

   Wscript.quit

end if

fullmsg = ""

for each feature in installer.features _

    ("{00000409-78e1-11d2-b60f-006097c998e7}")

   fullmsg = fullmsg & feature & " " & installer.FeatureState _

    ("{00000409-78e1-11d2-b60f-006097c998e7}", feature) & vbcrlf

next

msgbox fullmsg

Return to article
 

Listing 6: listobjs.cpp — Enumerating Platform SDK components

// ListObjs.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <windows.h>

#include <msi.h>

#include <stdio.h>

int main(int argc, char* argv[])

{

    FILE *listdata;

    listdata = fopen  ("lstobjs.txt" , "w"); 



    char cbuff [100];

    char pbuff[100];

    char pathbuff [MAX_PATH];

    char descbuff [500]; 

    const char psdkproduct [] = "{FB392771-9E4A-4DF8-9262-080BD65C0967}";



    INSTALLSTATE sval = MsiQueryProductState (psdkproduct); 

    if (sval != INSTALLSTATE_DEFAULT)

    {

        fprintf (listdata, "Platform SDK not installed\n");

        return 0;

    }

    DWORD dwdescbuff = sizeof(descbuff);

    UINT pi = MsiGetProductInfo (psdkproduct, 

        INSTALLPROPERTY_INSTALLEDPRODUCTNAME, 

        descbuff, &dwdescbuff);

    fprintf (listdata, "%s \n", descbuff);

    DWORD ix = 0; 

    for (ix=0; 1; ix++)

    {

        UINT urc;

        urc = MsiEnumComponents (ix, cbuff);

        if (ERROR_SUCCESS!=urc)

            break;

        UINT urp = MsiGetProductCode (cbuff, pbuff); 

        if (_stricmp (psdkproduct, pbuff) ==0)

        {

            DWORD dwpathbufflen = sizeof (pathbuff);

            INSTALLSTATE ival = MsiGetComponentPath (pbuff,

                cbuff, pathbuff, &dwpathbufflen);

            if (INSTALLSTATE_LOCAL==ival)

            {

                 // starts with a zero - it's a registry key 

                if (memcmp (pathbuff, "0", 1) ==0)

                {

                    char regname [MAX_PATH] = {0};

                    switch (pathbuff[1]) 

                    {

                    case '0': strcpy (regname, "HKCR"); break;

                    case '1': strcpy (regname, "HKCU"); break;

                    case '2': strcpy (regname, "HKLM"); break;

                    case '3': strcpy (regname, "HKUSERS"); break;

                    }

                    strcat (regname, pathbuff+3);

                    strcpy (pathbuff, regname);

                }



                fprintf(listdata, "Component %s installed at %s\n",

                        cbuff, pathbuff);

            }

        }





    }

    return 0;

}

//End of File


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.