|
-
Feb 11th, 2007, 06:41 PM
#1
Thread Starter
Addicted Member
-
Feb 11th, 2007, 09:18 PM
#2
Re: Changing IP address with VB
-
Feb 12th, 2007, 02:01 AM
#3
PowerPoster
Re: Changing IP address with VB
using WMI..
also see the attached app i made for another program ..
i have a newer version cleaned up and in modules etc but not finished.
VB Code:
Private Sub Command1_Click()
Dim ret As String
ret = SetStaticIp("192.168.0.2", "255.255.255.0", "192.168.0.1")
Debug.Print ret
End Sub
Public Function SetStaticIp(ByVal pIPAddress As String, _
ByVal pSubnet As String, _
ByVal pGateway As String, _
Optional ByVal pDNS1 As String, _
Optional ByVal pDNS2 As String) _
As String
Dim objWMIService As Object
Dim colNetAdapters As Object
Dim objNetAdapter As Object
Dim strSelect As String
Dim strIPAddress As String
Dim strSubnet As String
Dim strGateway As String
Dim strDns1 As String
Dim strDns2 As String
Dim varIPAddress As Variant
Dim varSubnet As Variant
Dim varGateway As Variant
Dim varGatewaymetric As Variant
Dim varDns As Variant
Dim lngEnable As Long
Dim lngGateways As Long
Dim lngDns As Long
Dim blnDns As Boolean
On Error GoTo Error_Handler
blnDns = True
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
strSelect = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
Set colNetAdapters = objWMIService.ExecQuery(strSelect)
strIPAddress = Trim$(pIPAddress)
strSubnet = Trim$(pSubnet)
strGateway = Trim$(pGateway)
If Not IsMissing(pDNS1) Then
strDns1 = Trim$(pDNS1)
End If
If Not IsMissing(pDNS2) Then
strDns2 = Trim$(pDNS2)
End If
varIPAddress = Array("" & strIPAddress & "")
varSubnet = Array("" & strSubnet & "")
varGateway = Array("" & strGateway & "")
varGatewaymetric = Array(1)
If Len(strDns1) = 0 Then
If Len(strDns2) = 0 Then
blnDns = False
varDns = ""
Else
varDns = Array("" & strDns2 & "")
End If
Else
If Len(strDns2) = 0 Then
varDns = Array("" & strDns1 & "")
Else
varDns = Array("" & strDns1 & "", "" & strDns2 & "")
End If
End If
For Each objNetAdapter In colNetAdapters
lngEnable = objNetAdapter.EnableStatic(varIPAddress, varSubnet)
lngGateways = objNetAdapter.SetGateways(varGateway, varGatewaymetric)
If blnDns = True Then
lngDns = objNetAdapter.SetDNSServerSearchOrder(varDns)
Else
lngDns = objNetAdapter.SetDNSServerSearchOrder()
End If
Next
If lngEnable > 1 Then
SetStaticIp = NetworkResults(lngEnable)
ElseIf lngGateways > 1 Then
SetStaticIp = NetworkResults(lngGateways)
ElseIf lngDns > 1 Then
If lngDns = 70 Then
SetStaticIp = "Invalid DNS Address"
Else
SetStaticIp = NetworkResults(lngDns)
End If
Else
SetStaticIp = NetworkResults(lngEnable)
End If
Error_Handler:
Set colNetAdapters = Nothing
Set objWMIService = Nothing
Set objNetAdapter = Nothing
If Err <> 0 Then _
SetStaticIp = "Error. " & Err.Description
End Function
Public Function NetworkResults(ByVal pNumber As Integer)
Dim sResults As String
Select Case pNumber
Case 0: sResults = "Success, No Reboot Required"
Case 1: sResults = "Success, Reboot Required"
Case 64: sResults = "Method Not Supported"
Case 65: sResults = "Unknown Failure"
Case 66: sResults = "Invalid Subnet Mask"
Case 67: sResults = "Processing Error"
Case 68: sResults = "Invalid Input"
Case 69: sResults = "Gateways Limit Error"
Case 70: sResults = "Invalid IP address"
Case 71: sResults = "Invalid Gateway Address"
Case 72: sResults = "Registry Access Error"
Case 73: sResults = "Invalid Domain Name"
Case 74: sResults = "Invalid Host Name"
Case 75: sResults = "No WINS Server Defined"
Case 76: sResults = "Invalid File"
Case 77: sResults = "Invalid System Path"
Case 78: sResults = "File Copy Failed"
Case 79: sResults = "Invalid Security Parameter"
Case 80: sResults = "Unable to Configure TCP/IP"
Case 81: sResults = "Unable to Configure DHCP"
Case 82: sResults = "Unable to Renew DHCP"
Case 83: sResults = "Unable to Release DHCP"
Case 84: sResults = "IP Not Enabled On Adapter"
Case 85: sResults = "IPX Not Enabled On Adapter"
Case 86: sResults = "Frame/Network Error"
Case 87: sResults = "Invalid Frame Type"
Case 88: sResults = "Invalid Network Number"
Case 89: sResults = "Duplicate Network Number"
Case 90: sResults = "Parameter Out Of Bounds"
Case 91: sResults = "Access Denied"
Case 92: sResults = "Out Of Memory"
Case 93: sResults = "Already Exists"
Case 94: sResults = "Path, File, or Object Not Found"
Case 95: sResults = "Unable To Notify Service"
Case 96: sResults = "Unable To Notify DNS Service"
Case 97: sResults = "Interface Not Configurable"
Case 98: sResults = "Not All DHCP Released/Renewed"
Case 100: sResults = "DHCP Not Enabled On Adapter"
End Select
NetworkResults = sResults
End Function
Last edited by rory; Feb 12th, 2007 at 06:44 AM.
-
Feb 12th, 2007, 04:29 AM
#4
PowerPoster
Re: Changing IP address with VB
and .. using the example zynder posted ..
i created a modified version of that ...
returns True or False. You could modify that, its just an example.
You have to manually enter the adaptor name though.
I didnt yet find code to enumerate through them, for XP that is.
FORM:
if entering DNS, enter multiple DNS servers with a comma (no spaces) between them
VB Code:
Private Sub Command1_Click()
Dim ret As String
ret = SetIPAddress(False, "192.168.0.2", "255.255.255.0", "192.168.0.1")
Debug.Print ret
End Sub
MODULE:
VB Code:
Option Explicit
Enum regTypes
ValNull = 0
ValString = 1
ValXString = 2
ValBinary = 3
ValDWord = 4
ValDWordLE = 4
ValDWordBE = 5
ValLink = 6
ValMultiString = 7
ValResList = 8
End Enum
Private Type myBytes
B1 As Byte
B2 As Byte
B3 As Byte
B4 As Byte
End Type
Private Type myLong
Val As Long
End Type
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, _
ByVal lpValue As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, _
lpValue As Long, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExByte Lib "advapi32" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, _
szData As Byte, ByVal cbData As Long) As Long
Private Declare Function SetAdapterIPAddress Lib "Iphlpapi" Alias "#135" _
(ByVal szAdapterGUID As String, ByVal dwDHCP As Long, ByVal dwIP As Long, ByVal dwMask As Long, _
ByVal dwGateway As Long) As Long
Public Function SetIPAddress(ByVal pDHCP As Boolean, _
Optional ByVal pIPAddress As String, _
Optional ByVal pSubnet As String, _
Optional ByVal pGateway As String, _
Optional ByVal pDNS As String) _
As Boolean
Dim strInterface As String
Dim strIPAddress As String
Dim strSubnet As String
Dim strGateway As String
Dim strDns As String
Const sVal As String = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
On Error Resume Next
strInterface = "{CE675518-5238-4C6D-8AD2-98B866B71CDF}"
If Len(strInterface) = 0 Then Exit Function
strIPAddress = Trim$(pIPAddress)
strSubnet = Trim$(pSubnet)
strGateway = Trim$(pGateway)
If Not IsMissing(pDNS) Then
strDns = Trim$(pDNS)
End If
If pDHCP = True Then
strIPAddress = vbNull
strSubnet = vbNull
strGateway = vbNull
SaveValue &H80000002, sVal & strInterface, "NameServer", "", ValString
Else
strIPAddress = IP2Long(strIPAddress)
strSubnet = IP2Long(strSubnet)
strGateway = IP2Long(strGateway)
SaveValue &H80000002, sVal & strInterface, "NameServer", strDns, ValString
End If
SetIPAddress = SetAdapterIPAddress(strInterface, pDHCP, strIPAddress, strSubnet, strGateway) = 0
End Function
' Convert a dotted quad IP string to a Long.
Private Function IP2Long(IP As String) As Long
' Perhaps the bytes are not in the politically correct order,
' but as long as you undo it with Long2IP you'll be fine.
ReDim parse(0 To 3) As String
Dim B As myBytes
Dim L As myLong
parse = Split(IP, ".")
B.B1 = Val(parse(0))
B.B2 = Val(parse(1))
B.B3 = Val(parse(2))
B.B4 = Val(parse(3))
LSet L = B
IP2Long = L.Val
End Function
Private Sub SaveValue(ByVal hKey As Long, ByVal strPath As String, _
ByVal strValue As String, ByVal varData As Variant, ByVal ValType As regTypes)
Dim keyhand As Long
Dim lResult As Long
Dim ordType As Long
Dim c As Long
lResult = RegCreateKey(hKey, strPath, keyhand)
If lResult Then
'error
Else
Select Case ValType
Case ValBinary
If (VarType(varData) = vbArray + vbByte) Then
Dim ab() As Byte
ab = varData
c = UBound(ab) - LBound(ab) + 1
lResult = RegSetValueExByte(keyhand, strValue, 0&, ValType, ab(0), c)
Else
'error
End If
Case ValDWord, ValDWordBE, ValDWordLE
If (VarType(varData) = vbInteger) Or (VarType(varData) = vbLong) Then
Dim i As Long
i = varData
ordType = ValDWord
lResult = RegSetValueExLong(keyhand, strValue, 0&, ordType, i, 4)
Else
'error
End If
Case ValString, ValXString
Dim s As String, iPos As Long
s = varData
ordType = ValString
iPos = InStr(s, "%")
If iPos Then
If InStr(iPos + 2, s, "%") Then ordType = ValXString
End If
c = Len(s) + 1
s = s & vbNullChar
lResult = RegSetValueExString(keyhand, strValue, 0&, ordType, s, c)
Case Else
'error
End Select
If lResult Then
'error
End If
RegCloseKey keyhand
End If
End Sub
Last edited by rory; Feb 12th, 2007 at 04:36 AM.
-
Feb 12th, 2007, 06:10 AM
#5
Thread Starter
Addicted Member
Re: Changing IP address with VB
Thank you for all your replies!
I've been looking through the code, but I'm a bit of a VB noob I'm afraid and all the references to Reg keys and whatnot are losing me a bit.
I don't need to edit the DNS settings, nor do I need to be able to change between static and dynamic IP.
If it's not too much trouble, would someone be able to take one of these examples and strip out all the stuff about changing DNS settings and all the other bits? I just need something that calls the code, to go into my Command1, such as the example above, and a chunk of code that just changes the IP address, subnet mask, and default gateway.
Thank you!
-
Feb 12th, 2007, 06:20 AM
#6
PowerPoster
Re: Changing IP address with VB
Hi, both examples i posted change only the IP, Subnet, and Gateway.
DNS is just an optional item in both of them.
If you arent going to know the Adapter name, which I doubt you will if you are giving this to other people, then use the WMI one (1st one), for now at least. All you need is the one line inside the Command1_Click sub.
SetStaticIp "192.168.0.2", "255.255.255.0", "192.168.0.1"
The other 2 subs can go either in the same form or in a seperate module, doesnt matter. You can get the return string from the function by assigning it to a string variable, such as in the example. Or just set it like above.
If you look at the attachment in the WMI post, you will also see there is more code for getting the IP information etc.
However, typically you must also set the DNS info when using a Static IP or you wont get online.
Last edited by rory; Feb 12th, 2007 at 06:26 AM.
-
Feb 12th, 2007, 06:24 AM
#7
Thread Starter
Addicted Member
Re: Changing IP address with VB
I don't mind setting the adaptor myself, the program is only for me at the moment anyway. What buttons/textboxes/etc are required for your second one though? Like I said, I'm new at this and the code all just turns into a blur to me when I read it, I only know very simple commands . I'm just trying to figure out which bits I can strip out relating to DNS settings and whatnot, but it's quite confusing for me.
-
Feb 12th, 2007, 06:32 AM
#8
PowerPoster
Re: Changing IP address with VB
if you dont care about getting a return string from it ...
and without the DNS info also ..
VB Code:
Private Sub Command1_Click()
SetIP "192.168.0.2", "255.255.255.0", "192.168.0.1"
End Sub
Public Sub SetIP(ByVal pIPAddress As String, ByVal pSubnet As String, _
ByVal pGateway As String)
Dim objWMIService As Object
Dim colNetAdapters As Object
Dim objNetAdapter As Object
Dim strSelect As String
Dim strIPAddress As String
Dim strSubnet As String
Dim strGateway As String
Dim varIPAddress As Variant
Dim varSubnet As Variant
Dim varGateway As Variant
Dim varGatewaymetric As Variant
On Error GoTo Error_Handler
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
strSelect = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
Set colNetAdapters = objWMIService.ExecQuery(strSelect)
strIPAddress = Trim$(pIPAddress)
strSubnet = Trim$(pSubnet)
strGateway = Trim$(pGateway)
varIPAddress = Array("" & strIPAddress & "")
varSubnet = Array("" & strSubnet & "")
varGateway = Array("" & strGateway & "")
varGatewaymetric = Array(1)
For Each objNetAdapter In colNetAdapters
objNetAdapter.EnableStatic varIPAddress, varSubnet
objNetAdapter.SetGateways varGateway, varGatewaymetric
Next
Error_Handler:
Set colNetAdapters = Nothing
Set objWMIService = Nothing
Set objNetAdapter = Nothing
End Sub
-
Feb 12th, 2007, 06:34 AM
#9
Thread Starter
Addicted Member
Re: Changing IP address with VB
Oh thank you! That looks nice and simple .
What is a return string? Does it need to return anything? It's setting data, not getting data. Like I said I'm only a noob at this. I don't know, do I want a return string? All I know is that I don't need to set DNS server settings, or change between static and dynamic IP .
Thanks again ^^.
-
Feb 12th, 2007, 06:35 AM
#10
PowerPoster
Re: Changing IP address with VB
 Originally Posted by Arby
I don't mind setting the adaptor myself, the program is only for me at the moment anyway. What buttons/textboxes/etc are required for your second one though? Like I said, I'm new at this and the code all just turns into a blur to me when I read it, I only know very simple commands  . I'm just trying to figure out which bits I can strip out relating to DNS settings and whatnot, but it's quite confusing for me.
or just use the app i attached and add / delete / modify the buttons ..
all they do is call the procedures in the code .. you can alter them or create additional procedures with the preset info.
-
Feb 12th, 2007, 06:37 AM
#11
PowerPoster
Re: Changing IP address with VB
 Originally Posted by Arby
Oh thank you! That looks nice and simple  .
What is a return string? Does it need to return anything? It's setting data, not getting data. Like I said I'm only a noob at this. I don't know, do I want a return string? All I know is that I don't need to set DNS server settings, or change between static and dynamic IP  .
Thanks again ^^.
incase it doesnt change the info or there is an error while doing so, like if the user enters an incorrect IP, Subnet, or Gateway .. but you need to know what the error returned means hence that extra "NetworkResults" function in the first one. So basically incase you want the user to be alerted that there was an error and what that error is, or if it completed successfully.
-
Feb 12th, 2007, 06:42 AM
#12
Thread Starter
Addicted Member
Re: Changing IP address with VB
How does it return it, as a msgbox?
And if it would be possible, may I have the code with with the return code but without the DNS stuff? I believe it gets rid of a lot of the reg stuff then. I don't want to just put all the code in and delete buttons because
A) It creates unnessecery code-bloat
B) I wouldn't know which buttons should be "Set" of "Un-set" by default, and if, say, radio boxes go, and the program has to perform an IF check to see which box is ticked, won't it crash?
C) It makes it hard for me to try and edit the code afterwards because I don't know what's going on, and
D) I don't know what buttons/radio boxes / textboxes and etc to add to the form or what to call them!
Thanks!
-
Feb 12th, 2007, 06:50 AM
#13
PowerPoster
Re: Changing IP address with VB
it returns it as a string in the above example, then you can do what you like with the string. The actual return is a number, that function just converts it to english.
If you only want to know True or False then I can post that code, without the DNS.
-
Feb 12th, 2007, 06:54 AM
#14
PowerPoster
Re: Changing IP address with VB
without DNS, returns a string.
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim ret As String
ret = SetIp("192.168.0.2", "255.255.255.0", "192.168.0.1")
Debug.Print ret
End Sub
'// wmi set static ip address
Public Function SetIp(ByVal pIPAddress As String, ByVal pSubnet As String, _
ByVal pGateway As String) As String
Dim objWMIService As Object
Dim colNetAdapters As Object
Dim objNetAdapter As Object
Dim strSelect As String
Dim strIPAddress As String
Dim strSubnet As String
Dim strGateway As String
Dim varIPAddress As Variant
Dim varSubnet As Variant
Dim varGateway As Variant
Dim varGatewaymetric As Variant
Dim lngEnable As Long
Dim lngGateways As Long
On Error GoTo Error_Handler
'// connect and query
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
strSelect = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
Set colNetAdapters = objWMIService.ExecQuery(strSelect)
'// clean strings
strIPAddress = Trim$(pIPAddress)
strSubnet = Trim$(pSubnet)
strGateway = Trim$(pGateway)
'// create arrays
varIPAddress = Array("" & strIPAddress & "")
varSubnet = Array("" & strSubnet & "")
varGateway = Array("" & strGateway & "")
varGatewaymetric = Array(1)
'// loop through adaptor info
For Each objNetAdapter In colNetAdapters
lngEnable = objNetAdapter.EnableStatic(varIPAddress, varSubnet)
lngGateways = objNetAdapter.SetGateways(varGateway, varGatewaymetric)
Next
'// return codes
If lngEnable > 1 Then
SetIp = NetworkResults(lngEnable)
ElseIf lngGateways > 1 Then
SetIp = NetworkResults(lngGateways)
Else
SetIp = NetworkResults(lngEnable)
End If
Error_Handler: '// cleanup
Set colNetAdapters = Nothing
Set objWMIService = Nothing
Set objNetAdapter = Nothing
If Err <> 0 Then _
SetIp = "Error. " & Err.Description
End Function
'// convert return codes to strings
Public Function NetworkResults(ByVal pNumber As Integer)
Dim sResults As String
Select Case pNumber
Case 0: sResults = "Success, No Reboot Required"
Case 1: sResults = "Success, Reboot Required"
Case 64: sResults = "Method Not Supported"
Case 65: sResults = "Unknown Failure"
Case 66: sResults = "Invalid Subnet Mask"
Case 67: sResults = "Processing Error"
Case 68: sResults = "Invalid Input"
Case 69: sResults = "Gateways Limit Error"
Case 70: sResults = "Invalid IP address"
Case 71: sResults = "Invalid Gateway Address"
Case 72: sResults = "Registry Access Error"
Case 73: sResults = "Invalid Domain Name"
Case 74: sResults = "Invalid Host Name"
Case 75: sResults = "No WINS Server Defined"
Case 76: sResults = "Invalid File"
Case 77: sResults = "Invalid System Path"
Case 78: sResults = "File Copy Failed"
Case 79: sResults = "Invalid Security Parameter"
Case 80: sResults = "Unable to Configure TCP/IP"
Case 81: sResults = "Unable to Configure DHCP"
Case 82: sResults = "Unable to Renew DHCP"
Case 83: sResults = "Unable to Release DHCP"
Case 84: sResults = "IP Not Enabled On Adapter"
Case 85: sResults = "IPX Not Enabled On Adapter"
Case 86: sResults = "Frame/Network Error"
Case 87: sResults = "Invalid Frame Type"
Case 88: sResults = "Invalid Network Number"
Case 89: sResults = "Duplicate Network Number"
Case 90: sResults = "Parameter Out Of Bounds"
Case 91: sResults = "Access Denied"
Case 92: sResults = "Out Of Memory"
Case 93: sResults = "Already Exists"
Case 94: sResults = "Path, File, or Object Not Found"
Case 95: sResults = "Unable To Notify Service"
Case 96: sResults = "Unable To Notify DNS Service"
Case 97: sResults = "Interface Not Configurable"
Case 98: sResults = "Not All DHCP Released/Renewed"
Case 100: sResults = "DHCP Not Enabled On Adapter"
End Select
NetworkResults = sResults
End Function
-
Feb 12th, 2007, 06:58 AM
#15
PowerPoster
Re: Changing IP address with VB
without DNS, just returns True or False (True if successful without error)
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim ret As Boolean
ret = SetIp("192.168.0.2", "255.255.255.0", "192.168.0.1")
Debug.Print ret
End Sub
'// wmi set static ip address
Public Function SetIp(ByVal pIPAddress As String, ByVal pSubnet As String, _
ByVal pGateway As String) As Boolean
Dim objWMIService As Object
Dim colNetAdapters As Object
Dim objNetAdapter As Object
Dim strSelect As String
Dim strIPAddress As String
Dim strSubnet As String
Dim strGateway As String
Dim varIPAddress As Variant
Dim varSubnet As Variant
Dim varGateway As Variant
Dim varGatewaymetric As Variant
Dim lngEnable As Long
Dim lngGateways As Long
On Error GoTo Error_Handler
'// connect and query
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
strSelect = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
Set colNetAdapters = objWMIService.ExecQuery(strSelect)
'// clean strings
strIPAddress = Trim$(pIPAddress)
strSubnet = Trim$(pSubnet)
strGateway = Trim$(pGateway)
'// create arrays
varIPAddress = Array("" & strIPAddress & "")
varSubnet = Array("" & strSubnet & "")
varGateway = Array("" & strGateway & "")
varGatewaymetric = Array(1)
'// loop through adaptor info
For Each objNetAdapter In colNetAdapters
lngEnable = objNetAdapter.EnableStatic(varIPAddress, varSubnet)
lngGateways = objNetAdapter.SetGateways(varGateway, varGatewaymetric)
Next
If lngEnable = 0 And lngGateways = 0 Then SetIp = True
Error_Handler: '// cleanup
Set colNetAdapters = Nothing
Set objWMIService = Nothing
Set objNetAdapter = Nothing
End Function
-
Feb 12th, 2007, 07:11 AM
#16
Thread Starter
Addicted Member
Re: Changing IP address with VB
Wow that's great, thanks a lot! I'll have a look through it all and see what I can do.
Thanks again!
-
Feb 12th, 2007, 07:20 AM
#17
Thread Starter
Addicted Member
Re: [RESOLVED] Changing IP address with VB
Oh, one last quick question - is there a bit of code for reading the IP - i.e, so I can have a button to "load current settings", or a text label to show the current settings? Thanks!
-
Feb 12th, 2007, 07:30 AM
#18
PowerPoster
Re: [RESOLVED] Changing IP address with VB
no prob .. also you can reference WMI in the project using Microsoft WMI scripting Library, or just set them as object as in the above examples ..
here is how to get the info, in this example i put the return values in the function as byref but you could just assign them to your text boxes direct.
It returns false if there is an error, true if no error.
I put textbox and WMI reference options in comments
VB Code:
Private Sub Command1_Click()
Dim sIP As String, sMask As String, sGate As String
If GetIPInfo(sIP, sMask, sGate) = True Then
Debug.Print sIP '// txtIP.Text = sIP
Debug.Print sMask '// txtSubnet.Text = sMask
Debug.Print sGate '// txtGateway.Text = sGate
Else
Debug.Print "Error!" '// MsgBox "Error!"
End If
End Sub
'// wmi get ip info
Public Function GetIPInfo(ByRef strIPAddress As String, _
ByRef strSubnet As String, ByRef strGateway As String) As Boolean
Dim WMIService As Object '// As SWbemServices
Dim IPConfigSet As Object '// As SWbemObjectSet
Dim IPConfig As Object '// As SWbemObject
Dim SQL As String, i As Long
On Error GoTo Error_Handler
'// connect and query
Set WMIService = GetObject("winmgmts:\\.\root\cimv2")
SQL = "Select IPAddress, IPSubnet, DNSServerSearchOrder, DefaultIPGateway, " & _
"DHCPEnabled from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
Set IPConfigSet = WMIService.ExecQuery(SQL)
'// loop through info
For Each IPConfig In IPConfigSet
'// get ip address
If Not IsNull(IPConfig.IpAddress) Then
For i = LBound(IPConfig.IpAddress) To UBound(IPConfig.IpAddress)
strIPAddress = IPConfig.IpAddress(i)
'// txtIP.Text = sIP
Next i
End If
'// get subnet mask
If Not IsNull(IPConfig.IPSubnet) Then
For i = LBound(IPConfig.IPSubnet) To UBound(IPConfig.IPSubnet)
strSubnet = IPConfig.IPSubnet(i)
'// txtSubnet.Text = sMask
Next i
End If
'// get gateway
If Not IsNull(IPConfig.DefaultIPGateway) Then
For i = LBound(IPConfig.DefaultIPGateway) To UBound(IPConfig.DefaultIPGateway)
strGateway = IPConfig.DefaultIPGateway(i)
'// txtGateway.Text = sGate
Next i
End If
Next IPConfig
Error_Handler: '// cleanup
Set IPConfigSet = Nothing
Set WMIService = Nothing
GetIPInfo = Err.Number = 0
End Function
Last edited by rory; Feb 12th, 2007 at 07:54 AM.
-
Feb 12th, 2007, 08:15 AM
#19
Thread Starter
Addicted Member
Re: Changing IP address with VB
Ooh thank you!
Thanks for all the help!
Last edited by BubbleLife; Feb 12th, 2007 at 08:19 AM.
-
Feb 12th, 2007, 08:19 AM
#20
PowerPoster
Re: Changing IP address with VB
no, only in the post where it uses the registry, the second post i made.
all this other code is WMI stuff and doesnt use the registry.
The PC needs to have WMI installed, with XP should be no issues.
-
Feb 12th, 2007, 10:17 AM
#21
Thread Starter
Addicted Member
-
Feb 12th, 2007, 01:05 PM
#22
Re: Changing IP address with VB
 Originally Posted by rory
You have to manually enter the adaptor name though.
I didnt yet find code to enumerate through them, for XP that is.
VB Code:
Dim sAdapter As String
Dim objWMIService As Object
Dim colNetAdapters As Object
Dim objNetAdapter As Object
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
sAdapter = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
Set colNetAdapters = objWMIService.ExecQuery(sAdapter)
' loop through adaptor info
For Each objNetAdapter In colNetAdapters
cmbAdapter.AddItem objNetAdapter.Caption
Next objNetAdapter
If cmbAdapter.ListCount > 0 Then
cmbAdapter.ListIndex = 0
Else
MsgBox "There doesn't appear to be any IP-enabled Network Adapter in this computer.", vbCritical, App.EXEName
End If
'When selecting the adapter to set the IP on, just cycle through colNetAdapters, looking for the objNetAdapter.Caption that matches cmbAdapter.Text
How about a snippet to set the adapter to dynamic? I can't seem to find that info.
Last edited by Al42; Feb 12th, 2007 at 01:08 PM.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Feb 12th, 2007, 03:27 PM
#23
PowerPoster
Re: Changing IP address with VB
 Originally Posted by Al42
How about a snippet to set the adapter to dynamic? I can't seem to find that info.
I was meaning using the registry or API :-)
What do you mean by Dynamic in this case?
If DHCP then here it is ..
VB Code:
Private Sub Command1_Click()
If SetDHCP = True Then
Debug.Print "DHCP Set Successfully"
End If
End Sub
'// wmi set dhcp
Public Function SetDHCP() As Boolean
Dim objWMIService As Object
Dim colNetAdapters As Object
Dim objNetAdapter As Object
Dim strSelect As String
Dim lngEnable As Long
Dim lngDns As Long
On Error GoTo Error_Handler
'// connect and query
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
strSelect = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
Set colNetAdapters = objWMIService.ExecQuery(strSelect)
'// loop through adaptor info
For Each objNetAdapter In colNetAdapters
lngEnable = objNetAdapter.EnableDHCP()
lngDns = objNetAdapter.SetDNSServerSearchOrder()
Next
If lngEnable = 0 And lngDns = 0 Then SetDHCP = True
Error_Handler: '// cleanup
Set colNetAdapters = Nothing
Set objWMIService = Nothing
Set objNetAdapter = Nothing
End Function
Last edited by rory; Feb 12th, 2007 at 03:51 PM.
-
Feb 12th, 2007, 05:47 PM
#24
Re: [RESOLVED] Changing IP address with VB
That's what I was looking for. Thanks. I haven't been able to find any extensive documentation on WMI.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Feb 12th, 2007, 08:05 PM
#25
PowerPoster
Re: [RESOLVED] Changing IP address with VB
 Originally Posted by Al42
That's what I was looking for. Thanks. I haven't been able to find any extensive documentation on WMI.
You can download and install this from Microsoft, though it requires IE.
http://www.microsoft.com/downloads/d...displaylang=en
Lately ive just been using MSDN as it has most of that info there and its easier to copy past the properties and methods.
http://msdn2.microsoft.com/en-us/library/aa389273.aspx
If i cant find something there then i go back to the WMI tools for further info.
Also, if there is anything in particular you cant find you can always search for "VBScript WMI" in google .. or browse the "Scripting Guys" section here:
http://www.microsoft.com/technet/scr...a/hsgarch.mspx
Last edited by rory; Feb 12th, 2007 at 08:10 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|