怎样让用户在VB 中的 textbox控件输入IP地址实现修改本机IP的目的?

我想环境是假如我在文本框中输入了IP网关DNS后,点击开始修改就能实现修改本机IP的功能。... 我想环境是假如我在文本框中输入了IP 网关 DNS后,点击开始修改就能实现修改本机IP的功能。 展开
 我来答
匿名用户
2013-10-21
展开全部
是可以实现的。修改注册表实现,要重起后生效. 或者禁用网卡在重启用,就可以.

程序代码:
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
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Storm代理
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I... 点击进入详情页
本回答由Storm代理提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式