-
Notifications
You must be signed in to change notification settings - Fork 0
/
modLink.bas
executable file
·50 lines (39 loc) · 1.89 KB
/
modLink.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Attribute VB_Name = "modLink"
Option Explicit
Public Function LinkConfig(sIP As String, sSubnetMask As String, sGateway As String, sMainDNS As String, Optional sSecondDNS As String) As Long
On Error GoTo LCErr:
Dim strComputer As String, objWMIService As Object, colNetAdapters As Object
Dim strIPAddress As Variant, strSubnetMask As Variant, strGateway As Variant, strGatewaymetric As Variant, strDNS As Variant
Dim objNetAdapter As Object, errEnable As Long, errGateways As Long, errDNS As Long
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array(sIP) 'IPµØÖ·
strSubnetMask = Array(sSubnetMask) '×ÓÍø
strGateway = Array(sGateway) 'Gateways
strDNS = Array(sMainDNS, sSecondDNS) 'MAIN DNS AND SECOND DNS
strGatewaymetric = Array(1)
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNS)
LinkConfig = Not (errEnable = 0 And errGateways = 0 And errDNS = 0)
Next
Exit Function
LCErr:
LinkConfig = 1
End Function
Public Function PingIP(sIP As String) As Boolean
On Error GoTo PIErr
Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address='" & sIP & "'")
For Each objItem In colItems
PingIP = (objItem.StatusCode = 0)
Next
Exit Function
PIErr:
PingIP = False
End Function