Listing 1: 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
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") )
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
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
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
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