Results 1 to 14 of 14

Thread: Cant seem to get this code to work

  1. #1

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Cant seem to get this code to work

    Hey, Ive been trying to search the registry for all keys called test and return their value, but im not sure how to do it using this example, if anyone could explain id appreicate it

    thanks

    macuiare

    code:
    Code:
    Imports System
    Imports Microsoft.Win32
    Imports System.Threading
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Public Shared ProcessThread As Thread
        Dim Path As String = "System\\ControlSet001\\Control\\Network\\"
        Dim SearchStr As String = "Name = Local Area Connection"
    
      Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            ProcessThread = New Thread(AddressOf SearchThread)
            ProcessThread.Start()
        End Sub
    
        Sub SearchThread()
            Search(Path, SearchStr)
        End Sub
    
        Public Sub Search(ByVal Path As String, ByVal SearchStr As String)
            Dim ParentKey As RegistryKey = Registry.LocalMachine.OpenSubKey(Path, True)
            ' Loop through values in the subkey
            For Each valueName As String In ParentKey.GetValueNames()
                On Error Resume Next
                'Create String to Compare against
                Dim CurStr As String = valueName + " = " + ParentKey.GetValue(valueName)
                ListBox1.Items.Insert(0, valueName + " = " + ParentKey.GetValue(valueName))
                If CurStr = SearchStr Then
                    MsgBox(CurStr)
                    CurStr = ""
                End If
            Next
            'if there are sub keys loop through and be recursive
            If ParentKey.SubKeyCount > 0 Then
                For Each subKeyName As String In ParentKey.GetSubKeyNames()
                    ListBox1.Items.Insert(0, "")
                    ListBox1.Items.Insert(0, Path + subKeyName) ' Writing the Keyname
                    'This is what makes me recursive!
                    Dim Thispath As String = Path + subKeyName + "\\"
                    Search(Thispath, SearchStr)
                Next
            End If
    
        End Sub
    
    End Class

  2. #2
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Re: Cant seem to get this code to work

    I was able to get it to run. What is the error you are running into?

    I did add this to the top of the code behind:

    Code:
    Imports System.Threading.Thread
    Imports Microsoft.Win32
    And if your using VISTA you have to give your application admin privileges.

    Right click on your project and press the button for "View UAC Settings"

    Change the Level to:

    level="requireAdministrator"
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  3. #3

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Re: Cant seem to get this code to work

    hmm, how would i get it to find "test" then? I dont quite understand D:

    Thanks

    Macuiare

  4. #4
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Re: Cant seem to get this code to work

    I think you just need to change your SerachStr to "test"
    You also need to change your Path to "System\\" so it searches everything in System. You may need to update it to search the other registry roots. As this is just looking stuff up inside HKLM\System


    Code:
        Dim Path As String = "System"
        Dim SearchStr As String = "test"
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  5. #5
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Re: Cant seem to get this code to work

    You might want to get rid of the threading stuff to make it eaiser to debug

    Code:
        'ProcessThread = New System.Threading.Thread(AddressOf SearchThread)
        'ProcessThread.Start()
        SearchThread()
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  6. #6

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Re: Cant seem to get this code to work

    Thanks for the advice, Im seaching software for VenderID to get the values of all VenderID named keys but it just errors and spams

    "A first chance execption of type 'system.Nullreferenceexception' occurred in regsearchtest.exe"

    thanks

    macuiare

  7. #7
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Re: Cant seem to get this code to work

    Here I wrote this. I added two DataGridViews to the form, one for displaying results and the other to display what it is working on. I also made it 'thread safe' and I made it search all of HKLM

    Code:
    Imports System.Threading.Thread
    Imports Microsoft.Win32
    
    
    Public Class Form1
    
      Dim dtResults As DataTable
      Dim dtFound As DataTable
    
      Public Shared ProcessThread As System.Threading.Thread
      Dim Path As String = ""
      Dim SearchStr As String = "test"
      Dim WaitRequest As Boolean
      Dim TS As ThreadStatus
    
      Enum ThreadStatus
        Running
        Wating
      End Enum
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dtResults = New DataTable
        dtResults.Columns.Add("Name", GetType(String))
        dtResults.Columns.Add("Value", GetType(String))
    
    
        dtFound = New DataTable
        dtFound.Columns.Add("Name", GetType(String))
        dtFound.Columns.Add("Value", GetType(String))
    
        Timer1.Enabled = True
    
        TS = ThreadStatus.Running
        ProcessThread = New System.Threading.Thread(AddressOf SearchThread)
        ProcessThread.Start()
    
        '    SearchThread()
      End Sub
    
      Sub SearchThread()
        Search(Path, SearchStr)
      End Sub
    
      Public Sub Search(ByVal Path As String, ByVal SearchStr As String)
    
        If WaitRequest = True Then
          WaitRequest = False
          TS = ThreadStatus.Wating
    
          While TS = ThreadStatus.Wating
            System.Threading.Thread.Sleep(10)
          End While
    
        End If
    
        Dim dRow As DataRow
    
        Dim ParentKey As RegistryKey = Registry.LocalMachine.OpenSubKey(Path, True)
        ' Loop through values in the subkey
        For Each valueName As String In ParentKey.GetValueNames()
          On Error Resume Next
          'Create String to Compare against
          Dim CurStr As String = valueName + " = " + ParentKey.GetValue(valueName)
    
          dRow = dtResults.NewRow
          dRow("Name") = valueName
          dRow("Value") = ParentKey.GetValue(valueName)
          dtResults.Rows.Add(dRow)
    
          'ListBox1.Items.Insert(0, valueName + " = " + ParentKey.GetValue(valueName))
          If CurStr = SearchStr Then
            Dim dRowF As DataRow = dtFound.NewRow()
            dRowF("Name") = valueName
            dRowF("Value") = CurStr
            dtFound.Rows.Add(dRowF)
            CurStr = ""
          End If
        Next
        'if there are sub keys loop through and be recursive
        If ParentKey.SubKeyCount > 0 Then
          For Each subKeyName As String In ParentKey.GetSubKeyNames()
    
            dRow = dtResults.NewRow
            dRow("Name") = Path + subKeyName
            dRow("Value") = ""
            dtResults.Rows.Add(dRow)
    
            'ListBox1.Items.Insert(0, "")
            'ListBox1.Items.Insert(0, Path + subKeyName) ' Writing the Keyname
            'This is what makes me recursive!
            Dim Thispath As String = Path + subKeyName + "\\"
            Search(Thispath, SearchStr)
          Next
    
        End If
    
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
        WaitRequest = True
    
        Timer1.Stop()
    
        While TS = ThreadStatus.Running
          System.Threading.Thread.Sleep(10)
          Application.DoEvents()
        End While
    
        Dim dtTemp As DataTable = dtResults.Copy
        Dim dtTempF As DataTable = dtFound.Copy
    
        Me.DataGridView1.DataSource = dtTemp
        Me.DataGridView2.DataSource = dtTempF
    
        TS = ThreadStatus.Running
    
        Timer1.Start()
    
      End Sub
    End Class
    Last edited by Dmyze; Nov 27th, 2009 at 03:36 AM.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  8. #8

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Re: Cant seem to get this code to work

    thankyou for all the time and effort you have put into it, but there is still problems

    Requested registry access is not allowed.

    becuase of

    Dim ParentKey As RegistryKey = Registry.LocalMachine.OpenSubKey(Path, True)

    how can i get it to add it to a listbox or two?

    I appreicate it thanks man

    thankyou

  9. #9
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Re: Cant seem to get this code to work

    I added a timer that ran every 5 seconds, when it fired it pauses the search thread, grabs the current values and puts them into a datagrid, but you could easily change it to a listbox.

    Look at the code in Timer1_Tick

    I also added a label that reports how many keys it has searched.

    I also made some changes this morning to test for the right value, as the last code set will never match your search term.

    Are you getting the registry error early on or after it runs for a while? If it's happing the first time it tries to read from the registry then you need to upgrade your projects UAC permissions:

    Right click on your project and press the button for "View UAC Settings"

    Change the Level to:

    level="requireAdministrator"


    Code:
    Imports System.Threading.Thread
    Imports Microsoft.Win32
    
    
    Public Class Form1
    
      Dim dtResults As DataTable
      Dim dtFound As DataTable
    
      Public Shared ProcessThread As System.Threading.Thread
      Dim Path As String = "SYSTEM\\"
      Dim SearchStr As String = "USERNAME"
      Dim WaitRequest As Boolean
      Dim TS As ThreadStatus
    
      Enum ThreadStatus
        Running
        Wating
      End Enum
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dtResults = New DataTable
        dtResults.Columns.Add("Name", GetType(String))
        dtResults.Columns.Add("Value", GetType(String))
    
    
        dtFound = New DataTable
        dtFound.Columns.Add("Name", GetType(String))
        dtFound.Columns.Add("Value", GetType(String))
    
        Timer1.Enabled = True
    
        TS = ThreadStatus.Running
        ProcessThread = New System.Threading.Thread(AddressOf SearchThread)
        ProcessThread.Start()
    
        '    SearchThread()
      End Sub
    
      Sub SearchThread()
        Search(Path, SearchStr)
        MsgBox("Search Complete")
      End Sub
    
      Public Sub Search(ByVal Path As String, ByVal SearchStr As String)
    
        If WaitRequest = True Then
          WaitRequest = False
          TS = ThreadStatus.Wating
    
          While TS = ThreadStatus.Wating
            System.Threading.Thread.Sleep(10)
          End While
    
        End If
    
        Dim dRow As DataRow
    
        Dim ParentKey As RegistryKey = Registry.LocalMachine.OpenSubKey(Path, True)
        ' Loop through values in the subkey
        For Each valueName As String In ParentKey.GetValueNames()
          On Error Resume Next
          'Create String to Compare against
          Dim CurStr As String = valueName + " = " + ParentKey.GetValue(valueName)
          Dim CurValue As String = ParentKey.GetValue(valueName)
          dRow = dtResults.NewRow
          dRow("Name") = valueName
          dRow("Value") = ParentKey.GetValue(valueName)
          dtResults.Rows.Add(dRow)
    
          'ListBox1.Items.Insert(0, valueName + " = " + ParentKey.GetValue(valueName))
          If CurValue = SearchStr Then
            Dim dRowF As DataRow = dtFound.NewRow()
            dRowF("Name") = valueName
            dRowF("Value") = CurValue
            dtFound.Rows.Add(dRowF)
            CurStr = ""
            CurValue = ""
          End If
        Next
        'if there are sub keys loop through and be recursive
        If ParentKey.SubKeyCount > 0 Then
          For Each subKeyName As String In ParentKey.GetSubKeyNames()
    
            dRow = dtResults.NewRow
            dRow("Name") = Path + subKeyName
            dRow("Value") = ""
            dtResults.Rows.Add(dRow)
    
            'ListBox1.Items.Insert(0, "")
            'ListBox1.Items.Insert(0, Path + subKeyName) ' Writing the Keyname
            'This is what makes me recursive!
            Dim Thispath As String = Path + subKeyName + "\\"
            Search(Thispath, SearchStr)
          Next
    
        End If
    
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
        WaitRequest = True
    
        Timer1.Stop()
    
        While TS = ThreadStatus.Running
          System.Threading.Thread.Sleep(10)
          Application.DoEvents()
        End While
    
        Me.Label1.Text = "Checked " & dtResults.Rows.Count & " Rows"
        Dim dtTemp As DataTable = dtResults.Copy
        Dim dtTempF As DataTable = dtFound.Copy
    
        Me.DataGridView1.DataSource = dtTemp
        Me.DataGridView2.DataSource = dtTempF
    
        TS = ThreadStatus.Running
    
        Timer1.Start()
    
      End Sub
    
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ProcessThread.Abort()
        ProcessThread = Nothing
      End Sub
    End Class
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  10. #10

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Re: Cant seem to get this code to work

    It works, nice job, thankyou very much your example works perfectly just had to addin controls and such

    I juset have one more question

    how would iget it to search for a keycalled Codkey and show its value just as an example

    Dim Path As String = "SOFTWARE\\"
    Dim SearchStr As String = "codkey" ?

  11. #11
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Re: Cant seem to get this code to work

    You could Change the SerachStr to the key you are looking for and change in inner IF statement to this:

    Code:
          If valueName= SearchStr Then
            Dim dRowF As DataRow = dtFound.NewRow()
            dRowF("Name") = valueName
            dRowF("Value") = CurValue
            dtFound.Rows.Add(dRowF)
            CurStr = ""
            CurValue = ""
          End If
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  12. #12

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Re: Cant seem to get this code to work

    Thankyou works found my codkey, um still it searches on though and doesnt seem to stop, after 90k it still keeps going and erroring this over and over

    A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll


    even though i set it to


    <requestedExecutionLevel level="requireAdministrator" uiAccess="true" />

    thanks again

  13. #13
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Re: Cant seem to get this code to work

    Do you want it to stop once it finds the code key? You can call the .Abort method on the thread once it finds something.

    Yeah I'm not sure what causes the exception that happens later, I was getting it too if I left it running too long. maybe it's trying to access some OS protected registry key.
    Last edited by Dmyze; Nov 28th, 2009 at 04:49 PM.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  14. #14

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Re: Cant seem to get this code to work

    hmm odd isnt it, i might just have it run till a certain number, maybe 20000?

    Anyways i appriate all your help means alot,


    Thankyou

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width