Remote Registry Access using VB.NET to enumerate Installed Software Applications

A

Alan King

To All:

I'm having difficulty getting the syntax correct for VB.NET to produce a
listing of all software applications on a remote PC listed in the registry
installed programs.

I'm able to get to the point where I enumerate the keys, but I can't figure
out the syntax for retrieving the values from those keys. Here's what I
have at this point:

////////////////////// BEGINNING OF CODE
/////////////////////////////////////////
Dim strComputerName As String = tbScope.Text
Dim strSubKeyName As String
Dim strValueName As String
Dim strStringValue As String = ""
Dim strValue As String
Dim strKeyPath As String =
"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
Dim objManagementScope As ManagementScope
Dim objManagementClass As ManagementClass
Dim objManagementBaseObject As ManagementBaseObject
Dim aSubKeys() As String


objManagementScope = New ManagementScope
objManagementScope.Path.Server = strComputerName
objManagementScope.Path.NamespacePath = "root\default"
objManagementScope.Options.EnablePrivileges = True
objManagementScope.Options.Impersonation =
ImpersonationLevel.Impersonate
objManagementScope.Connect()



objManagementClass = New ManagementClass("stdRegProv")
objManagementClass.Scope = objManagementScope
objManagementBaseObject =
objManagementClass.GetMethodParameters("EnumKey")
objManagementBaseObject.SetPropertyValue("hDefKey", CType("&H" &
Hex(RegistryHive.LocalMachine), Long))
objManagementBaseObject.SetPropertyValue("sSubKeyName", strKeyPath)
aSubKeys = CType(objManagementClass.InvokeMethod("EnumKey",
objManagementBaseObject, Nothing).Properties.Item("sNames").Value, String())


Dim strkey As String

For Each strkey In aSubKeys
lbResults.Items.Add(strkey)
Next
////////////////////// END OF CODE /////////////////////////////////////////



In VBScript, I could use the GetStringValue as shown below to get the
Display name of a application listed in the registry:

"GetStringValue(HKLM, BASE_KEY & strKey, "DisplayName", strDisplayName)"

However, I don't know the .NET version (System.Management) of getting these
values.

If anyone has a recommendation or a code snippet to show how I can do this
in VB.NET it would be greatly welcomed.

Alan
 
P

Peter Falz

Hi Alan King,


a snip of my code:


Dim mcWMIRegistry As New ManagementClass( mngScopeReg , New ManagementPath( "StdRegProv" ) , Nothing )

Dim mboParams As ManagementBaseObject
Dim mboResult As ManagementBaseObject

mboParams = mcWMIRegistry.GetMethodParameters( "GetStringValue" )

mboParams("hDefKey") = regHKEY_LOCAL_MACHINE
mboParams("sSubKeyName") = "Software\Microsoft\Windows\CurrentVersion"
mboParams("sValueName") = "ProductId"

Try
mboResult = mcWMIRegistry.InvokeMethod( "GetStringValue" , mboParams , Nothing )

' "ReturnValue:" & Convert.ToUInt32( mboResult( "ReturnValue" ) ).ToString() & vbCrLf
' "RegValue___:" & mboResult( "sValue" ) & vbCrLf
Catch
End Try


HTH

Ciao
Peter
 
A

Alan King

Peter Falz,

Thanks so much for taking the time to reply to my message. Your code
snippit did not quite address my needed goal, however parts of it did help
me to understand where I was going wrong. Your code addresses just the
"ProductID" extraction from the registry's "CurrentVersion" key and I needed
something that would take my enumerated keys from the "Uninstall" key and
translate their values into strings that indicate software titles.

The way you structured your code however did help me to understand some
structural issues that were a problem with my code and I was able to figure
out my problem by reviewing your snippit.

Here's my corrected and working code:

|||||||||||||||||||||||| BEGIN CODE |||||||||||||||||||||||||||||||
Dim strComputerName As String = tbScope.Text
Dim strSubKeyName As String
Dim strValueName As String
Dim strStringValue As String = ""
Dim strValue As String
Dim strKeyPath As String =
"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
Dim objManagementScope As ManagementScope
Dim objManagementClass As ManagementClass
Dim objManagementBaseObject As ManagementBaseObject
Dim strkey As String
Dim myKey As String
Dim myValue As String
Dim aSubKeys() As String


objManagementScope = New ManagementScope
objManagementScope.Path.Server = strComputerName
objManagementScope.Path.NamespacePath = "root\default"
objManagementScope.Options.EnablePrivileges = True
objManagementScope.Options.Impersonation =
ImpersonationLevel.Impersonate
objManagementScope.Connect()


objManagementClass = New ManagementClass("stdRegProv")
objManagementClass.Scope = objManagementScope
objManagementBaseObject =
objManagementClass.GetMethodParameters("EnumKey")
objManagementBaseObject.SetPropertyValue("hDefKey", CType("&H" &
Hex(RegistryHive.LocalMachine), Long))
objManagementBaseObject.SetPropertyValue("sSubKeyName", strKeyPath)
aSubKeys = CType(objManagementClass.InvokeMethod("EnumKey",
objManagementBaseObject, Nothing).Properties.Item("sNames").Value, String())



For Each strkey In aSubKeys
myKey = strkey
objManagementBaseObject =
objManagementClass.GetMethodParameters("GetStringValue")
objManagementBaseObject.SetPropertyValue("hDefKey", CType("&H" &
Hex(RegistryHive.LocalMachine), Long))
objManagementBaseObject.SetPropertyValue("sSubKeyName",
strKeyPath & strkey)
objManagementBaseObject.SetPropertyValue("sValueName",
"DisplayName")
objManagementBaseObject =
objManagementClass.InvokeMethod("GetStringValue", objManagementBaseObject,
Nothing)

myValue = objManagementBaseObject("sValue")
If Not myValue = "" Then
lbResults.Items.Add(myValue)
End If
Next

|||||||||||||||||||||||| END CODE |||||||||||||||||||||||||||||||

Thanks again for your help!

AK
 
Joined
Oct 13, 2005
Messages
4
Reaction score
0
WMI Registry

this is my code ... i m having problem in enumerating the Main key like (HKEY_CLASSES_ROOT)..

error messeg "Invalid Parameters"



objManagementBaseObject = objManagementClass.GetMethodParameters("EnumKey")
objManagementBaseObject.SetPropertyValue("hDefKey", CType("&H" & Hex(RegistryHive.LocalMachine), Long))
aSubKeys = CType(objManagementClass.InvokeMethod("EnumKey", objManagementBaseObject, Nothing).Properties.Item("sNames").Value, String())
For Each strkey In aSubKeys
myKey = strkey
ListView1.Items.Add(myKey)
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top