Trying to read IP Address information from the registry

L

Larry Powell

I'm attempting to read IP addresses from the registry and have hit a
stumbling block.

While I have no difficulty moving through the registry values finding
strings of the REG_SZ type when I try to get information form the
REG_MULTI_SZ - I get errors.

The following code works just fine. It is from a Windows .NET app

Private Sub Get_StringInfoFromRegistry()
Dim regKey As RegistryKey
Dim strTCPIPAdapterInfoKey As String =
"System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"
Dim strIPAddresses() As String
Dim strNicCards() As String
Dim strIPAddress As String
Dim strNetworkInterface As String
Dim strDomainAddress As String
Dim bFound As Boolean = False
Dim addressinfo As String = "0.0.0.0"

Try
regKey = Registry.LocalMachine.OpenSubKey(strTCPIPAdapterInfoKey)
strNicCards = regKey.GetSubKeyNames
For Each strNetworkInterface In strNicCards
Dim newKey As String = strTCPIPAdapterInfoKey & "\" & strNetworkInterface
If Not bFound Then
regKey = Registry.LocalMachine.OpenSubKey(newKey, False)
'MsgBox(newKey)
If Equals(regKey.GetValue("test".ToString), "Chas") Then
strDomainAddress = regKey.GetValue("Test".ToString)
MsgBox(strDomainAddress)
End If
End If
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

The following code errors when the values are a multi String valued key:
Private Sub Get_StringInfoFromRegistry()
Dim regKey As RegistryKey
Dim strTCPIPAdapterInfoKey As String =
"System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"
Dim strIPAddresses() As String
Dim strNicCards() As String
Dim strIPAddress As String
Dim strNetworkInterface As String
Dim strDomainAddress As String
Dim bFound As Boolean = False
Dim addressinfo As String = "0.0.0.0"

Try
regKey = Registry.LocalMachine.OpenSubKey(strTCPIPAdapterInfoKey)
strNicCards = regKey.GetSubKeyNames
For Each strNetworkInterface In strNicCards
Dim newKey As String = strTCPIPAdapterInfoKey & "\" & strNetworkInterface
If Not bFound Then
regKey = Registry.LocalMachine.OpenSubKey(newKey, False)
'MsgBox(newKey)
If Equals(regKey.GetValue("IPAddress".ToString), "0.0.0.0") Then
strDomainAddress = regKey.GetValue("IPAddress".ToString)
MsgBox(strDomainAddress)
End If
End If
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Any help is appreciated

LP
 
L

LP

Well John,

In the line below strDomainAddress is Notheing and the exception is Null
exception that strDomain need to be set to an instance of an object.

strDomainAddress = regKey.GetValue("IPAddress".ToString)
 
J

John Saunders

Well John,

In the line below strDomainAddress is Notheing and the exception is Null
exception that strDomain need to be set to an instance of an object.

strDomainAddress = regKey.GetValue("IPAddress".ToString)

Please show us the stack trace. In the line above, I can't see how you'd get
a NullReferenceException if strDomainAddress were Nothing.

On the other hand, perhaps you have the "ToString" in the wrong place.
"IPAddress" is already a string, so doesn't need "ToString". Perhaps you
want:

strDomainAddress = regKey.GetValue("IPAddress").ToString()

I also suggest you try compiling with Options Strict turned on.
 
L

LP

How about this option. It doesn't read that Multi valued registry but
is does bring back you IP Address
' Code up a windows form using this
' One issue it is apparently obsolete in the Framework 1.1 but it still
functions correctly
Imports System
Imports System.Net
Imports System.Net.Dns

Public Class IPAddress
Inherits System.Windows.Forms.Form
Dim strHostName As String

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 64)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(88, 144)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 1
Me.Button2.Text = "Button2"
'
'IPAddress
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.Button2, Me.Button1})
Me.Name = "IPAddress"
Me.Text = "IPAddress"
Me.ResumeLayout(False)

End Sub

#End Region

Shared Function GetIPAddress() As String
Dim oAddr As System.Net.IPAddress
Dim sAddr As String
With System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())
oAddr = New System.Net.IPAddress(.AddressList(0).Address)
sAddr = oAddr.ToString
End With
GetIPAddress = sAddr
End Function

'Shared Sub main()
' Dim shostname As String
' shostname = System.Net.Dns.GetHostName
' Console.WriteLine("Your Machine Name = " & shostname)
' 'Call Get IPAddress
' Console.WriteLine("Your IP = " & GetIPAddress())
'End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
strHostName = System.Net.Dns.GetHostByName("LocalHost").HostName
MsgBox("Your Machine name is: " & strHostName)
Call GetIPAddress()
MsgBox("Your IP Address is: " & GetIPAddress())
End Sub

End Class
 

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