WMI StdRegProv in a class.

B

Brx

I am having an issue using WMI StdRegProv in a class that I'm creating to
make it easer to access remote registries. It works great the first time I
create and access it, but if I create a second instance of the class I get
the following error.

An unhandled exception of type 'System.Runtime.InteropServices.COMException'
occurred in microsoft.visualbasic.dll

Additional information: Unspecified error

Here's what I have for my class file.

Public Enum HKey
CLASSES_ROOT = &H80000000
CURRENT_USER = &H80000001
LOCAL_MACHINE = &H80000002
USERS = &H80000003
CURRENT_CONFIG = &H80000005
DYN_DATA = &H80000006
End Enum
Public Class RemoteRegistry
Dim _Server As String
Dim _User As String
Dim _Password As String
Dim oWMIServer As New WbemScripting.SWbemLocator
Dim oReg As WbemScripting.SWbemObject
Dim oServer As WbemScripting.SWbemServices
Dim _Connected As Boolean = False
Property Server() As String
Get
Server = _Server
End Get
Set(ByVal Value As String)
_Server = Value
_Connected = False
End Set
End Property
Property UserName() As String
Get
UserName = _User
End Get
Set(ByVal Value As String)
If _User <> Nothing Then
If _User.ToLower <> Value.ToLower Then
_User = Value
_Password = Nothing
End If
Else
_User = Value
_Password = Nothing
End If
End Set
End Property
WriteOnly Property Password() As String
Set(ByVal Value As String)
_Password = Value
End Set
End Property
Public Function LookupValue(ByVal HKey As HKey, ByVal Path As String,
ByVal Value As String) As String
Dim sResult As String
Dim sResult2 As String

If Not _Connected Then Connect()
oReg.GetStringValue(HKey, Path, Value, sResult)
LookupValue = sResult
End Function
Private Sub Connect()
'On Error Resume Next
If _User = Nothing Or _User = "" Then
oServer = oWMIServer.ConnectServer(_Server, "root\default")
Else
oServer = oWMIServer.ConnectServer(_Server, "root\default",
_User, _Password)
If Err.Number <> 0 Then
Err.Clear()
oServer = oWMIServer.ConnectServer(_Server, "root\default")
End If
End If
oReg = oServer.Get("StdRegProv")
_Connected = True
End Sub
End Class


And here's what I'm using to test it with.


Private Sub ExecuteScript_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ExecuteScript.Click
Dim y As New RemoteRegistry
Dim Key As String = "SYSTEM\CurrentControlSet\Services\SNMP"

y.Server = "srvr-istest6"
y.UserName = UserName.Text
y.Password = Password.Text




label1.Text = y.LookupValue(HKey.LOCAL_MACHINE, Key, "DisplayName")
y = Nothing
End Sub

I'm wondering if anyone has any ideas as to why I can't run the
ExecuteScript twice without getting an error on the oReg.GetStringValue, I'm
all ears(well I don't look like dumbo, but do feel like one sometimes)...


Thanks,
kmurphy
roughlyat nexusinfosys and that's in the com domain.
 
B

Brx

I had thought about that, but it doesn't look like it supports supplying
authentication credentials, some of the machines we need to manipulate are
not in the same domain so we need to provide a username and password.
 

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