Link to home
Start Free TrialLog in
Avatar of Phreak3eb
Phreak3eb

asked on

Retrieve Installed Software 64-bit Windows Using WMI

I'm trying to retrieve all installed software on our Windows machines using C#.  I can't seem to find a way to do this on 64-bit versions of Windows.  All the examples I've found so far use VBScript, which, generally, I can convert them to C#, however, I'm having a heck of a time converting this script I've found by David Gardiner (http://davidgardiner.blogspot.com/2007/03/listing-installed-applications-on-vista.html)  that DOES work.  Can anyone help me convert this script to C# or point me in the direction to do this from scratch in C#?
strComputer = "."
Const HKLM = &h80000002
Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
objCtx.Add "__ProviderArchitecture", 32
objCtx.Add "__RequiredArchitecture", TRUE
Set objLocator = CreateObject("Wbemscripting.SWbemLocator")
Set objServices = objLocator.ConnectServer("","root\default","","",,,,objCtx)
Set objStdRegProv = objServices.Get("StdRegProv") 
WScript.Echo "32-bit Applications"
WScript.echo "-------------------" 
Call GetApplications 
objCtx.Add "__ProviderArchitecture", 64
objCtx.Add "__RequiredArchitecture", TRUE
Set objLocator = CreateObject("Wbemscripting.SWbemLocator")
Set objServices = objLocator.ConnectServer("","root\default","","",,,,objCtx)
Set objStdRegProv = objServices.Get("StdRegProv") 
WScript.Echo "64-bit Applications"
WScript.echo "-------------------" 
 
Call GetApplications 
 
Sub GetApplications 
	' Use ExecMethod to call the GetStringValue method
	Set Inparams = objStdRegProv.Methods_("EnumKey").Inparameters
	Inparams.Hdefkey = HKLM
	Inparams.Ssubkeyname = "Software\Microsoft\Windows\CurrentVersion\Uninstall\" 
	Set Outparams = objStdRegProv.ExecMethod_("EnumKey", Inparams,,objCtx) 
	For Each strSubKey In Outparams.snames 
		Set Inparams = objStdRegProv.Methods_("GetStringValue").Inparameters
		Inparams.Hdefkey = HKLM
		Inparams.Ssubkeyname = "Software\Microsoft\Windows\CurrentVersion\Uninstall\" & strSubKey
		Inparams.Svaluename = "DisplayName"
		set Outparams = objStdRegProv.ExecMethod_("GetStringValue", Inparams,,objCtx) 
		if ("" & Outparams.sValue) = "" then
			'wscript.echo strSubKey
		Else
			wscript.echo Outparams.SValue
		End iF 
		'Inparams.Svaluename = "QuietDisplayName"
		'set Outparams = objStdRegProv.ExecMethod_("GetStringValue", Inparams,,objCtx)
		'wscript.echo Outparams.SValue 
	Next 
End Sub

Open in new window

Avatar of Phreak3eb
Phreak3eb

ASKER

Ok.  I've got it converted to C# and it runs.  However, it isn't returning the 32-bit software, only the 64-bit.  Anyone have any ideas?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Runtime.InteropServices;
 
namespace GetInstalledSoftwareTest
{
    class Program
    {        
        const uint HKEY_LOCAL_MACHINE = unchecked((uint)0x80000002);
        static string strComputer = ".";
        //static ManagementObject classInstance = new ManagementObject();
        static ManagementNamedValueCollection objCtx = new ManagementNamedValueCollection();
 
        static void Main(string[] args)
        {            
            try
            {
                objCtx.Add("__ProviderArchitecture", 32);
                objCtx.Add("__RequiredArchitecture", true);
                ObjectGetOptions options = new ObjectGetOptions(objCtx);
                ManagementScope test = new ManagementScope("\\\\.\\root\\DEFAULT");
                ManagementPath test2 = new ManagementPath("StdRegProv");
                ManagementClass classInstance = new ManagementClass(test, test2, options);
                Console.WriteLine("32-bit Applications");
                Console.WriteLine("-------------------");
                GetApplications(classInstance);                
 
                objCtx.Add("__ProviderArchitecture", 64);
                objCtx.Add("__RequiredArchitecture", true);
                options = new ObjectGetOptions(objCtx);
                classInstance = new ManagementClass(test, test2, options);
                Console.WriteLine("64-bit Applications");
                Console.WriteLine("-------------------");
                GetApplications(classInstance);
            }
            catch (ManagementException err)
            {
                Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.Message);
            }
            
            Console.ReadLine();
        }
 
        private static void GetApplications(ManagementObject classInstance)
        {
            // Obtain in-parameters for the method
            ManagementBaseObject inParams = classInstance.GetMethodParameters("EnumKey");
            
            // Add the input parameters.
            inParams["hDefKey"] = HKEY_LOCAL_MACHINE;
            inParams["sSubKeyName"] = @"Software\Microsoft\Windows\CurrentVersion\Uninstall\";
 
            // Execute the method and obtain the return values.
            ManagementBaseObject outParams = classInstance.InvokeMethod("EnumKey", inParams, null);
            
            foreach (string property in (string[])outParams["sNames"])
            {
                inParams = classInstance.GetMethodParameters("GetStringValue");
                inParams["hDefKey"] = HKEY_LOCAL_MACHINE;
                inParams["sSubKeyName"] = @"Software\Microsoft\Windows\CurrentVersion\Uninstall\" + property;
                inParams["sValueName"] = "DisplayName";
                ManagementBaseObject OutParams = classInstance.InvokeMethod("GetStringValue", inParams, null);
                
                if ((String)OutParams["sValue"] != "" && OutParams["sValue"] != null)
                {
                    Console.WriteLine((String)OutParams["sValue"]);
                }
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Phreak3eb
Phreak3eb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator