修改2000系统以及XP系统的本机IP地址,应该使用哪个API函数?
我现在使用修改注册表进行修改,需要重启电脑。而且也不知道这样设置会不会在换机使用的时候引起错误。请各位大侠多多赐教!
小女子在此谢过~~~

解决方案 »

  1.   

    用WMI。
    不要试图直接改注册表,虽然可能效率比较高,但是兼容性会有问题,而且不安全。
    下面是一段代码,是某个工程中的一个部分,仔细看看然后查查MCDN就明白了
    Public Function ApplyChange(bChange As Boolean) As Long
        On Error GoTo errHandler
        Err.Clear
        Dim objNIC As Object
        Dim objNICs As Object
        
        'Test Preconditions
        Debug.Assert (mvarIsConnected = True)
        
        If bChange Then
            
            'Get NIC Collection object from WMI var MACAddr
            Set objNICs = GetObject("winmgmts:").ExecQuery( _
                "Select * FROM Win32_NetworkAdapterConfiguration where MACAddress='" _
                & mvarMACAddr & "'")
                 
                       
            'the count must be 1
            If objNICs.Count <> 1 Then
                ApplyChange = E_ErrorUnKnown
                Set objNICs = Nothing
                Exit Function
            End If
           
           'Enable DHCP
        If mvarDHCPEnabled Then
                'Enable DHCP
                For Each objNIC In objNICs
                    ApplyChange = objNIC.EnableDHCP
                Next
                
                'Destroy the object
                Set objNIC = Nothing
                Set objNICs = Nothing
                
           'Static IP
        Else
                For Each objNIC In objNICs
                    Dim arryIP
                    Dim arryMask
                    Dim lRet As Long
                    arryIP = Array(mvarIPAddr)
                    arryMask = Array(mvarSubNetMask)
                    
                    'Try to set IP and subnetmask var WIN32_NetworkAdapterConfigration.EnableSatic
                    'Note: the parameters must be array! Wrong prototype are given in  MSDN 
                    lRet = objNIC.EnableStatic(arryIP, arryMask)
                    
                    'lRet=0 for no reboot required; 1 for reboot required. Otherwise, error code
                    If lRet > 1 Then
                        ApplyChange = lRet
                        Set objNIC = Nothing
                        Set objNICs = Nothing
                        Exit Function
                    End If
                    
                    
                    If mvarDefautlGateWay <> "" Then
                        Dim arryGW
                        arryGW = Array(mvarDefautlGateWay)
                        ApplyChange = objNIC.SetGateways(arryGW)
                        If ApplyChange > 1 Then
                            Set objNIC = Nothing
                            Set objNICs = Nothing
                            Exit Function
                        End If
                    End If
                    
                    'if anyone of ApplyChange or lRet is 1 then the system should reboot
                    'so,the return value should be 0 or 1
                    ApplyChange = ApplyChange + lRet
                    If ApplyChange <> 0 Then ApplyChange = 1
                    
                Next
                'Destroy the object
                Set objNIC = Nothing
                Set objNICs = Nothing
           
           End If 'End mvarDHCPEnabled
        
        End If 'End bChange
        Exit Function
        
    errHandler:
        ApplyChange = Err.Number
        Set objNIC = Nothing
        Set objNICs = NothingEnd Function