注册 登录
编程论坛 VB6论坛

vb 怎么实现改计算机用户名呢。。做机房管理系统其它都实现了,就差这一个了。

sandra1991 发布于 2013-03-24 18:44, 684 次点击
以下是实现的代码部分








 Private Sub Command2_Click()
Dim Num
Num = Text1.Text
Dim Name
If Num < 10 Then
       Name = "KS"
Else
       If Num < 100 Then
             Name = "KS"
       Else
              Name = "KS"
        End If
End If
Set sh = CreateObject("WScript.Shell")
sh.RegWrite "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName", Name + Num, "REG_SZ"
sh.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname", Name + Num, "REG_SZ"
sh.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname", Name + Num, "REG_SZ"
Set sh = Nothing
Set oShell = Nothing

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array("192.168.0." & Num)
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.0.1")
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway)
Dim dns
dns = Array("218.85.152.99", "218.85.157.99")
Err = objNetAdapter.SetDNSServerSearchOrder(dns)
Next

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer In colComputers
'ObjComputer.Rename("game" &Num)
Next
Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")
objNetworkSettings.SetIPXVirtualNetworkNumber (Num)
End Sub
3 回复
#2
yuccn2013-03-24 20:45
不懂,偶然来vb版转转,看到又分,流口水了~
#3
lowxiong2013-03-25 08:47
应该是修改系统用户名吧,没事修改计算机名干嘛。
如果是修改计算机名的话,使用SetComputerName 函数,声明是
Declare Function SetComputerName Lib "kernel32" Alias "SetComputerNameA" (ByVal lpComputerName As String) As Long
修改了计算机名好像要重启才有效吧。
如果是修改系统用户名的话则一般用net user命令完成的,如vb中运行下列代码可添加一个超级用户的lowxiong账户
Shell "net user lowxiong  /add"
Shell "net localgroup administrators lowxiong /add"
下面命令删除刚刚添加的用户
Shell "net user lowxiong  /del"
当前用户必须有足够权限才能使net user命令正常运行,具体方法你可以百度net user
你引用了wmi,wmi应该可以完成这些的。

[ 本帖最后由 lowxiong 于 2013-3-25 08:49 编辑 ]
#4
VBatman2013-03-26 18:00
我看你的代码是想修改计算机名称的吧。刚好昨晚写出来了一个。。可能会帮到你。。迟点给你发代码
1