Imports System
Imports Microsoft.Win32
Imports System.Management

''' -----------------------------------------------------------------------------
''' Project	 : RemoteIE
''' Class	 : RemoteHostSettings
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' In this class you can find different functions to edit the registry
''' </summary>
''' <remarks>
''' In this class you can find different functions to edit the registry
''' </remarks>
''' <history>
''' 	[Administrator]	09.06.2004	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class RemoteHostSettings
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' With this function you can read a value from a registrypath.
    ''' </summary>
    ''' <param name="Hive">RegistryHive (for example CurrentUser)</param>
    ''' <param name="Key">Registry Key (for example SOFTWARE\Microsoft\Internet Explorer\Main)</param>
    ''' <param name="ValueName">Value (for example NoUpdateCheck)</param>
    ''' <param name="machineName">the remote machine name</param>
    ''' <param name="Fehler">get False - no error, true - error</param>
    ''' <returns>the value in this registrypath</returns>
    ''' <remarks>
    ''' Aufruf Read_RegValue(Microsoft.Win32.RegistryHive.Users, "SOFTWARE\Microsoft\Internet Explorer\Main", "NoUpdateCheck", "s59xx01", m_Fehler)
    ''' </remarks>
    ''' <history>
    ''' 	[Administrator]	09.06.2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function Read_RegValue(ByVal Hive As RegistryHive, _
                                  ByVal Key As String, ByVal ValueName As String, _
                                  ByVal machineName As String) As Object

        Dim objParent As RegistryKey
        Dim objSubkey As RegistryKey
        Dim sAns As Object
        Select Case Hive
            Case RegistryHive.ClassesRoot
                objParent = Registry.ClassesRoot
            Case RegistryHive.CurrentConfig
                objParent = Registry.CurrentConfig
            Case RegistryHive.CurrentUser
                objParent = Registry.CurrentUser
            Case RegistryHive.DynData
                objParent = Registry.DynData
            Case RegistryHive.LocalMachine
                objParent = Registry.LocalMachine
            Case RegistryHive.PerformanceData
                objParent = Registry.PerformanceData
            Case RegistryHive.Users
                objParent = Registry.Users
        End Select

        Try
            objSubkey = objParent.OpenRemoteBaseKey(Hive, machineName).OpenSubKey(Key, False)
            If Not objSubkey Is Nothing Then sAns = (objSubkey.GetValue(ValueName, Nothing))
            If Not objSubkey Is Nothing Then objSubkey.Close()
        Catch ex As Exception
            Throw ex
        End Try
        Return sAns
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' With this function you can get all Subkeys
    ''' </summary>
    ''' <param name="Hive"></param>
    ''' <param name="Key"></param>
    ''' <param name="machineName"></param>
    ''' <returns>String Array</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Administrator]	21.09.2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function getAllSubKeys(ByVal Hive As RegistryHive, _
                                  ByVal Key As String, _
                                  ByVal machineName As String) As String()

        Dim objParent As RegistryKey
        Dim objSubkey As RegistryKey
        Dim varReturn As String()
        Select Case Hive
            Case RegistryHive.ClassesRoot
                objParent = Registry.ClassesRoot
            Case RegistryHive.CurrentConfig
                objParent = Registry.CurrentConfig
            Case RegistryHive.CurrentUser
                objParent = Registry.CurrentUser
            Case RegistryHive.DynData
                objParent = Registry.DynData
            Case RegistryHive.LocalMachine
                objParent = Registry.LocalMachine
            Case RegistryHive.PerformanceData
                objParent = Registry.PerformanceData
            Case RegistryHive.Users
                objParent = Registry.Users
        End Select
        Try
            objSubkey = objParent.OpenRemoteBaseKey(Hive, machineName).OpenSubKey(Key, False)
            varReturn = objSubkey.GetSubKeyNames
        Catch ex As Exception
            'Throw ex
        Finally
            getAllSubKeys = varReturn
            Try
                objSubkey.Close()
            Catch ex As Exception

            End Try
        End Try

    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' With this function you can check if a RegistryValue exists
    ''' </summary>
    ''' <param name="Hive">RegistryHive (for example CurrentUser)</param>
    ''' <param name="Key">Registry Key (for example SOFTWARE\Microsoft\Internet Explorer\Main)</param>
    ''' <param name="ValueName">Value (for example NoUpdateCheck)</param>
    ''' <param name="machineName">the remote machine name</param>
    ''' <returns>True or False</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Administrator]	09.06.2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function IfExist_RegValue(ByVal Hive As RegistryHive, _
                                  ByVal Key As String, ByVal ValueName As String, _
                                  ByVal machineName As String) As Boolean

        Dim objParent As RegistryKey
        Dim objSubkey As RegistryKey
        Dim blReturn As Boolean
        Dim StrA() As String
        Dim Str As String
        Select Case Hive
            Case RegistryHive.ClassesRoot
                objParent = Registry.ClassesRoot
            Case RegistryHive.CurrentConfig
                objParent = Registry.CurrentConfig
            Case RegistryHive.CurrentUser
                objParent = Registry.CurrentUser
            Case RegistryHive.DynData
                objParent = Registry.DynData
            Case RegistryHive.LocalMachine
                objParent = Registry.LocalMachine
            Case RegistryHive.PerformanceData
                objParent = Registry.PerformanceData
            Case RegistryHive.Users
                objParent = Registry.Users
        End Select

        Try
            objSubkey = objParent.OpenRemoteBaseKey(Hive, machineName).OpenSubKey(Key, False)
            If Not objSubkey Is Nothing Then
                StrA = objSubkey.GetValueNames
            End If
            objSubkey.Close()
            For Each Str In StrA
                If Str = ValueName Then blReturn = True : Exit For
            Next
        Catch ex As Exception
            blReturn = False
        End Try
        Return blReturn
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' With this function you can write a value to the registry
    ''' </summary>
    ''' <param name="ParentKeyHive">RegistryHive (for example CurrentUser)</param>
    ''' <param name="SubKeyName">Registry Key (for example SOFTWARE\Microsoft\Internet Explorer\Main)</param>
    ''' <param name="ValueName">Value (for example NoUpdateCheck)</param>
    ''' <param name="machineName">the remote machine name</param>
    ''' <param name="Value">the value which would be set</param>
    ''' <returns>true or false</returns>
    ''' <remarks>
    ''' if the registrykey is not available it would be create
    ''' </remarks>
    ''' <history>
    ''' 	[Administrator]	09.06.2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub WriteToRegistry(ByVal ParentKeyHive As RegistryHive, _
                                    ByVal SubKeyName As String, _
                                    ByVal ValueName As String, _
                                    ByVal machineName As String, _
                                    ByVal Value As Object)

        Dim objSubKey As RegistryKey
        Dim sException As String
        Dim objParentKey As RegistryKey
        Try
            Select Case ParentKeyHive
                Case RegistryHive.ClassesRoot
                    objParentKey = Registry.ClassesRoot
                Case RegistryHive.CurrentConfig
                    objParentKey = Registry.CurrentConfig
                Case RegistryHive.CurrentUser
                    objParentKey = Registry.CurrentUser
                Case RegistryHive.DynData
                    objParentKey = Registry.DynData
                Case RegistryHive.LocalMachine
                    objParentKey = Registry.LocalMachine
                Case RegistryHive.PerformanceData
                    objParentKey = Registry.PerformanceData
                Case RegistryHive.Users
                    objParentKey = Registry.Users
            End Select
            'Open 
            objSubKey = objParentKey.OpenRemoteBaseKey(ParentKeyHive, machineName).OpenSubKey(SubKeyName, True)
            'create if doesn't exist
            If objSubKey Is Nothing Then
                objSubKey = objParentKey.OpenRemoteBaseKey(ParentKeyHive, machineName).CreateSubKey(SubKeyName)
            End If
            objSubKey.SetValue(ValueName, Value)
            objSubKey.Close()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' With this function you can delete a SubkeyTree
    ''' </summary>
    ''' <param name="ParentKeyHive">RegistryHive (for example CurrentUser)</param>
    ''' <param name="SubKeyName">Registry Key (for example SOFTWARE\Microsoft\Internet Explorer\Main)</param>
    ''' <param name="machineName">the remote machine name</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Administrator]	09.06.2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub DeleteRegistrySubKeyTree(ByVal ParentKeyHive As RegistryHive, _
                                    ByVal SubKeyName As String, _
                                    ByVal machineName As String)

        Dim objSubKey As RegistryKey
        Dim sException As String
        Dim objParentKey As RegistryKey
        Try
            Select Case ParentKeyHive
                Case RegistryHive.ClassesRoot
                    objParentKey = Registry.ClassesRoot
                Case RegistryHive.CurrentConfig
                    objParentKey = Registry.CurrentConfig
                Case RegistryHive.CurrentUser
                    objParentKey = Registry.CurrentUser
                Case RegistryHive.DynData
                    objParentKey = Registry.DynData
                Case RegistryHive.LocalMachine
                    objParentKey = Registry.LocalMachine
                Case RegistryHive.PerformanceData
                    objParentKey = Registry.PerformanceData
                Case RegistryHive.Users
                    objParentKey = Registry.Users
            End Select
            'delete
            objParentKey.OpenRemoteBaseKey(ParentKeyHive, machineName).DeleteSubKeyTree(SubKeyName)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' With this function you can delete a value from the registry
    ''' </summary>
    ''' <param name="ParentKeyHive">RegistryHive (for example CurrentUser)</param>
    ''' <param name="SubKeyName">Registry Key (for example SOFTWARE\Microsoft\Internet Explorer\Main)</param>
    ''' <param name="ValueName">Value (for example NoUpdateCheck)</param>
    ''' <param name="machineName">the remote machine name</param>
    ''' <returns>true or false</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Administrator]	09.06.2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub DeleteRegistryKeyValue(ByVal ParentKeyHive As RegistryHive, _
                                    ByVal SubKeyName As String, _
                                    ByVal ValueName As String, _
                                    ByVal machineName As String)

        Dim objSubKey As RegistryKey
        Dim sException As String
        Dim objParentKey As RegistryKey
        Try
            Select Case ParentKeyHive
                Case RegistryHive.ClassesRoot
                    objParentKey = Registry.ClassesRoot
                Case RegistryHive.CurrentConfig
                    objParentKey = Registry.CurrentConfig
                Case RegistryHive.CurrentUser
                    objParentKey = Registry.CurrentUser
                Case RegistryHive.DynData
                    objParentKey = Registry.DynData
                Case RegistryHive.LocalMachine
                    objParentKey = Registry.LocalMachine
                Case RegistryHive.PerformanceData
                    objParentKey = Registry.PerformanceData
                Case RegistryHive.Users
                    objParentKey = Registry.Users
            End Select
            'Open 
            objSubKey = objParentKey.OpenRemoteBaseKey(ParentKeyHive, machineName).OpenSubKey(SubKeyName, True)
            'delete if exist
            If Not objSubKey Is Nothing Then
                objSubKey.DeleteValue(ValueName, False)
            End If
            objSubKey.Close()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' With this Function you gets the local logged-on UserID (Remote)
    ''' </summary>
    ''' <param name="m_Machine">The Hostname or IP-Adress of the Computer</param>
    ''' <returns>the logged-on UserID</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Administrator]	14.06.2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function getUserRemote(ByVal m_Machine As String) As String
        Dim wmi As ManagementClass
        Dim objs As ManagementObjectCollection
        Dim obj As ManagementObject
        Dim MgmtPath, UserName, temp As String
        Dim cnt As Integer
        Try
            MgmtPath = "\\" & m_Machine & "\root\cimv2:"
            wmi = New ManagementClass(MgmtPath & "Win32_ComputerSystem")
            objs = wmi.GetInstances()
            For Each obj In objs
                temp = obj("UserName")
            Next
            cnt = InStr(temp, "\")
            UserName = temp
        Catch ex As Exception
            Throw ex
        Finally
            If Not wmi Is Nothing Then wmi.Dispose()
            If Not wmi Is Nothing Then objs.Dispose()
        End Try
        Return UserName
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Gets the remote logged-on SID from the User
    ''' </summary>
    ''' <param name="m_Machine">Remote Computer (Hostname or IP-Adress)</param>
    ''' <param name="m_User">UserID from the User</param>
    ''' <returns>The SID from the logged-on User</returns>
    ''' <remarks>
    ''' To get the Information we use the registry
    ''' </remarks>
    ''' <history>
    ''' 	[Administrator]	14.06.2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function getSIDUserRemote(ByVal m_Machine As String, ByVal m_User As String) As String
        Const m_Pfad As String = "SYSTEM\CurrentControlSet\Control\hivelist"
        Dim objSubKeys, objSearcher As RegistryKey
        Dim merkestr As String = Nothing
        Dim obj As Object
        Dim bolValueFound As Boolean
        Dim ADomUser As String() = Split(m_User, "\")
        Dim varUser As String = ADomUser(1)
        Dim varDom As String = ADomUser(0)
        Try
            objSubKeys = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, m_Machine).OpenSubKey(m_Pfad)
            For Each objSubKey As String In objSubKeys.GetValueNames
                obj = objSearcher.OpenRemoteBaseKey(RegistryHive.LocalMachine, m_Machine).OpenSubKey(m_Pfad).GetValue(objSubKey)
                If InStr(obj.toUpper, varUser.ToUpper & "." & varDom.ToUpper & "\NTUSER.DAT") > 0 Then
                    merkestr = objSubKey
                    bolValueFound = True
                    Exit For
                End If
            Next
            If bolValueFound = False Then
                For Each objSubKey As String In objSubKeys.GetValueNames
                    obj = objSearcher.OpenRemoteBaseKey(RegistryHive.LocalMachine, m_Machine).OpenSubKey(m_Pfad).GetValue(objSubKey)
                    If InStr(obj.toUpper, varUser.ToUpper & "\NTUSER.DAT") > 0 Then
                        merkestr = objSubKey
                        bolValueFound = True
                        Exit For
                    End If
                Next
            End If
            merkestr = merkestr.Substring(merkestr.LastIndexOf("\") + 1)
        Catch ex As Exception
            Throw ex
        Finally
            Try
                objSubKeys.Close()
                objSearcher.Close()
            Catch ex As Exception
                'no error
            End Try
        End Try
        Return merkestr
    End Function
End Class
