How to get local ip addresses

Thanks to the example given by @John Hansen I was able to write a program to change the IP address of an adapter to static or revert it back to DHCP. Much appreciated.

I thought I would share, I’ll add the full source to the program I wrote to download from my blog eventually.

Change IP address (static IP):

[code] // WMI Win32_NetworkAdapterConfiguration
//
//
//https://msdn.microsoft.com/en-us/library/aa394217%28v=vs.85%29.aspx

	Dim locator, objWMIService, objNetAdapters, objProperty As OLEOBJECT
	Dim nobjs As Integer
	
	//  Connect to WMI
	locator = New oleObject("WbemScripting.SWbemlocator", True)
	
	Dim wmiServiceParams(0) As Variant
	wmiServiceParams(0) = "winmgmts:\\\\.\\root\\cimv2"
	
	Dim strResult As String
	Dim strSelect As String
	Dim lngEnable As Int32
	
	Dim strIP As String
	Dim strSubnet As String
	Dim strGateway As String
	Dim strDNS1 As String
	Dim strDNS2 As String
	
	strIP = Trim(txtIP.Text)
	strSubnet = Trim(txtSubnet.Text)
	strGateway = Trim(txtGateway.Text)
	strDNS1 = Trim(txtDNS1.Text)
	strDNS2 = Trim(txtDNS2.Text)
	
	Dim strIPArr() As String
	Dim strSubnetArr() As String
	Dim strGatewayArr() As String
	Dim strDNSArr() As String
	
	Dim isDNS As Boolean = False
	
	if strIP <> "" then
	strIPArr.Append(strIP)
	else
	txtInfo.Text = "Please enter IP Address to continue..."
	end
	
	if strSubnet <> "" then
	strSubnetArr.Append(strSubnet)
	else
	txtInfo.Text = "Please enter subnet to continue..."
	end
	
	if strGateway <> "" then
	strGatewayArr.Append(strGateway)
	else
	txtInfo.Text = "Please enter gateway to continue..."
	end
	
	if strDNS1 <> "" then
	strDNSArr.Append(strDNS1)
	isDNS = True
	end
	
	if strDNS2 <> "" then
	strDNSArr.Append(strDNS2)
	end
	
	objWMIService = locator.invoke("ConnectServer", wmiServiceParams)
	strSelect = "Select * from Win32_NetworkAdapterConfiguration where Index="+strIdx
	objNetAdapters = objWMIService.ExecQuery(strSelect)
	
	objProperty = objNetAdapters.ItemIndex(0)
	
	lngEnable = objProperty.EnableStatic(strIPArr, strSubnetArr)
	strResult = GetResult(lngEnable)
	txtInfo.AppendText("IP Result: " + str(lngEnable)) + EndOfLine
	txtInfo.AppendText(strResult) + EndOfLine
	
	lngEnable = objProperty.SetGateways(strGatewayArr)
	strResult = GetResult(lngEnable)
	txtInfo.AppendText("Gateway Result: " + str(lngEnable)) + EndOfLine
	txtInfo.AppendText(strResult) + EndOfLine
	
	if isDNS = True then
			lngEnable = objProperty.SetDNSServerSearchOrder(strDNSArr)
			strResult = GetResult(lngEnable)
			txtInfo.AppendText("DNS Result: " + str(lngEnable)) + EndOfLine
			txtInfo.AppendText(strResult) + EndOfLine
	end
	
	locator = Nil
	
	Exception err As oleexception
			msgbox err.message[/code]

Change IP address(static to DHCP):

[code] // WMI Win32_NetworkAdapterConfiguration
//
//
//https://msdn.microsoft.com/en-us/library/aa394217%28v=vs.85%29.aspx

	Dim locator, objWMIService, objNetAdapters, objProperty As OLEOBJECT
	Dim nobjs As Integer
	
	//  Connect to WMI
	locator = New oleObject("WbemScripting.SWbemlocator", True)
	
	Dim wmiServiceParams(0) As Variant
	wmiServiceParams(0) = "winmgmts:\\\\.\\root\\cimv2"
	
	Dim strResult As String
	Dim strSelect As String
	Dim lngEnable As Int32
	
	objWMIService = locator.invoke("ConnectServer", wmiServiceParams)
	strSelect = "Select * from Win32_NetworkAdapterConfiguration where Index="+strIdx
	objNetAdapters = objWMIService.ExecQuery(strSelect)
	
	objProperty = objNetAdapters.ItemIndex(0)
	
	lngEnable = objProperty.EnableDHCP()
	strResult = GetResult(lngEnable)
	txtInfo.AppendText("DHCP IP Result: " + str(lngEnable)) + EndOfLine
	txtInfo.AppendText(strResult) + EndOfLine
	
	lngEnable = objProperty.SetDNSServerSearchOrder(nil)
	strResult = GetResult(lngEnable)
	txtInfo.AppendText("DHCP DNS Result: " + str(lngEnable)) + EndOfLine
	txtInfo.AppendText(strResult) + EndOfLine
	
	locator = Nil

            Exception err As oleexception
	msgbox err.message[/code]

strIdx is the adapter index which you can get like this:

strIdx = objProperty.Value("Index")

:slight_smile: