找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 471|回复: 2

[VBA程序]:用VB语言获取、修改计算机的IP地址及子网掩码

[复制链接]
发表于 2003-10-26 19:36:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Option Explicit
Const REG_SZ As Long = 1
Const HKEY_LOCAL_MACHINE = &H80000002

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
    (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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 RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
    ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
    (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
    lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Dim aaa As String


Private Sub Command1_Click()
Dim hKey As Long, hKey1 As Long, hKey2 As Long, ret As Long, ret1 As Long, lenData As Long, typeData As Long
Dim Name As String, name1 As String, name2 As String, s As String, S1 As String
Dim idx As Integer
idx = 0
Name = String(256, Chr(0))
RegCreateKey HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Services\Class\NetTrans", hKey
Text1.Text = hKey
Do
ret = RegEnumKey(hKey, idx, Name, Len(Name))
If ret = 0 Then
aaa = Left(Name, InStr(Name, Chr(0)) - 1)
aaa = "System\CurrentControlSet\Services\Class\NetTrans\" & aaa
ret = RegOpenKey(HKEY_LOCAL_MACHINE, aaa, hKey)
'获得IP地址
If ret = 0 Then
  name1 = "IPAddress"
  name2 = "IPMask"
   ret = RegQueryValueEx(hKey, name1, 0, typeData, ByVal vbNullString, lenData)
   s = String(lenData, Chr(0))
   RegQueryValueEx hKey, name1, 0, typeData, ByVal s, lenData
  If s <> "" Then s = Left(s, InStr(s, Chr(0)) - 1)
  '获得子网掩码
   ret1 = RegQueryValueEx(hKey, name2, 0, typeData, ByVal vbNullString, lenData)
   S1 = String(lenData, Chr(0))
   RegQueryValueEx hKey, name2, 0, typeData, ByVal S1, lenData
   If S1 <> "" Then S1 = Left(S1, InStr(S1, Chr(0)) - 1)
If Val(s) > 0 Then
   Text1.Text = s
   Text2.Text = S1
   Command2.Enabled = True
   Exit Sub
End If
End If
idx = idx + 1
End If
Loop Until ret <> 0
End Sub
Private Sub Command2_Click()
  Dim hKey As Long
     If Text1.Text <> "" Then
        RegCreateKey HKEY_LOCAL_MACHINE, aaa, hKey
'       修改IP地址
        RegSetValueEx hKey, "IPAddress", 0, REG_SZ, ByVal Text1.Text, 13
'       修改子网掩码
        RegSetValueEx hKey, "IPMask", 0, REG_SZ, ByVal Text2.Text, 13
        RegCloseKey hKey
     End If
End Sub

Private Sub Command3_Click()
  End
End Sub
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2003-10-26 21:31:55 | 显示全部楼层
能具体说一下用法吗?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2003-10-26 21:48:47 | 显示全部楼层
能具体说一下用法吗?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-12-26 12:09 , Processed in 0.379075 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表