Monday, 26 June 2017

change ip address using vb 6

  1. Private Sub Command1_Click()
  2.     Dim ret As String
  3.     ret = SetStaticIp("192.168.0.2", "255.255.255.0", "192.168.0.1")
  4.     Debug.Print ret
  5. End Sub
  6.  
  7. Public Function SetStaticIp(ByVal pIPAddress As String, _
  8.                             ByVal pSubnet As String, _
  9.                             ByVal pGateway As String, _
  10.                             Optional ByVal pDNS1 As String, _
  11.                             Optional ByVal pDNS2 As String) _
  12.                             As String
  13.                            
  14.     Dim objWMIService       As Object
  15.     Dim colNetAdapters      As Object
  16.     Dim objNetAdapter       As Object
  17.     Dim strSelect           As String
  18.     Dim strIPAddress        As String
  19.     Dim strSubnet           As String
  20.     Dim strGateway          As String
  21.     Dim strDns1             As String
  22.     Dim strDns2             As String
  23.     Dim varIPAddress        As Variant
  24.     Dim varSubnet           As Variant
  25.     Dim varGateway          As Variant
  26.     Dim varGatewaymetric    As Variant
  27.     Dim varDns              As Variant
  28.     Dim lngEnable           As Long
  29.     Dim lngGateways         As Long
  30.     Dim lngDns              As Long
  31.     Dim blnDns              As Boolean
  32.    
  33.     On Error GoTo Error_Handler
  34.  
  35.     blnDns = True
  36.     Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  37.     strSelect = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
  38.     Set colNetAdapters = objWMIService.ExecQuery(strSelect)
  39.    
  40.     strIPAddress = Trim$(pIPAddress)
  41.     strSubnet = Trim$(pSubnet)
  42.     strGateway = Trim$(pGateway)
  43.    
  44.     If Not IsMissing(pDNS1) Then
  45.         strDns1 = Trim$(pDNS1)
  46.     End If
  47.     If Not IsMissing(pDNS2) Then
  48.         strDns2 = Trim$(pDNS2)
  49.     End If
  50.    
  51.     varIPAddress = Array("" & strIPAddress & "")
  52.     varSubnet = Array("" & strSubnet & "")
  53.     varGateway = Array("" & strGateway & "")
  54.     varGatewaymetric = Array(1)
  55.    
  56.     If Len(strDns1) = 0 Then
  57.         If Len(strDns2) = 0 Then
  58.             blnDns = False
  59.             varDns = ""
  60.         Else
  61.             varDns = Array("" & strDns2 & "")
  62.         End If
  63.     Else
  64.         If Len(strDns2) = 0 Then
  65.             varDns = Array("" & strDns1 & "")
  66.         Else
  67.             varDns = Array("" & strDns1 & "", "" & strDns2 & "")
  68.         End If
  69.     End If
  70.    
  71.     For Each objNetAdapter In colNetAdapters
  72.         lngEnable = objNetAdapter.EnableStatic(varIPAddress, varSubnet)
  73.         lngGateways = objNetAdapter.SetGateways(varGateway, varGatewaymetric)
  74.         If blnDns = True Then
  75.             lngDns = objNetAdapter.SetDNSServerSearchOrder(varDns)
  76.         Else
  77.             lngDns = objNetAdapter.SetDNSServerSearchOrder()
  78.         End If
  79.     Next
  80.    
  81.     If lngEnable > 1 Then
  82.         SetStaticIp = NetworkResults(lngEnable)
  83.     ElseIf lngGateways > 1 Then
  84.         SetStaticIp = NetworkResults(lngGateways)
  85.     ElseIf lngDns > 1 Then
  86.         If lngDns = 70 Then
  87.             SetStaticIp = "Invalid DNS Address"
  88.         Else
  89.             SetStaticIp = NetworkResults(lngDns)
  90.         End If
  91.     Else
  92.         SetStaticIp = NetworkResults(lngEnable)
  93.     End If
  94.    
  95. Error_Handler:
  96.     Set colNetAdapters = Nothing
  97.     Set objWMIService = Nothing
  98.     Set objNetAdapter = Nothing
  99.     If Err <> 0 Then _
  100.     SetStaticIp = "Error. " & Err.Description
  101. End Function
  102.  
  103. Public Function NetworkResults(ByVal pNumber As Integer)
  104.     Dim sResults As String
  105.     Select Case pNumber
  106.         Case 0: sResults = "Success, No Reboot Required"
  107.         Case 1: sResults = "Success, Reboot Required"
  108.         Case 64: sResults = "Method Not Supported"
  109.         Case 65: sResults = "Unknown Failure"
  110.         Case 66: sResults = "Invalid Subnet Mask"
  111.         Case 67: sResults = "Processing Error"
  112.         Case 68: sResults = "Invalid Input"
  113.         Case 69: sResults = "Gateways Limit Error"
  114.         Case 70: sResults = "Invalid IP address"
  115.         Case 71: sResults = "Invalid Gateway Address"
  116.         Case 72: sResults = "Registry Access Error"
  117.         Case 73: sResults = "Invalid Domain Name"
  118.         Case 74: sResults = "Invalid Host Name"
  119.         Case 75: sResults = "No WINS Server Defined"
  120.         Case 76: sResults = "Invalid File"
  121.         Case 77: sResults = "Invalid System Path"
  122.         Case 78: sResults = "File Copy Failed"
  123.         Case 79: sResults = "Invalid Security Parameter"
  124.         Case 80: sResults = "Unable to Configure TCP/IP"
  125.         Case 81: sResults = "Unable to Configure DHCP"
  126.         Case 82: sResults = "Unable to Renew DHCP"
  127.         Case 83: sResults = "Unable to Release DHCP"
  128.         Case 84: sResults = "IP Not Enabled On Adapter"
  129.         Case 85: sResults = "IPX Not Enabled On Adapter"
  130.         Case 86: sResults = "Frame/Network Error"
  131.         Case 87: sResults = "Invalid Frame Type"
  132.         Case 88: sResults = "Invalid Network Number"
  133.         Case 89: sResults = "Duplicate Network Number"
  134.         Case 90: sResults = "Parameter Out Of Bounds"
  135.         Case 91: sResults = "Access Denied"
  136.         Case 92: sResults = "Out Of Memory"
  137.         Case 93: sResults = "Already Exists"
  138.         Case 94: sResults = "Path, File, or Object Not Found"
  139.         Case 95: sResults = "Unable To Notify Service"
  140.         Case 96: sResults = "Unable To Notify DNS Service"
  141.         Case 97: sResults = "Interface Not Configurable"
  142.         Case 98: sResults = "Not All DHCP Released/Renewed"
  143.         Case 100: sResults = "DHCP Not Enabled On Adapter"
  144.     End Select
  145.     NetworkResults = sResults
  146. End Function

No comments:

Post a Comment

Thank You!!!

create and activate a Python virtual environment on macOS and Windows

create and activate a Python virtual environment on macOS and Windows: Prerequisites: Python 3: Ensure you have Python 3 installed. You ca...