VB 高手帮改段代码?
建立一个窗口跟一个模块就行了,并放置若干控件(5个label,5个text,1个command)
窗口代码如下:
Private Sub Command1_Click()
Call SetStringValue(HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName", Text1.Text)
Call SetStringValue(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "Hostname", Text2.Text)
Call SetStringValue2(HKEY_LOCAL_MACHINE, "SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\{C09A8CD0-2C7A-4866-AAD0-D36CBD733EFC}", "IPAddress", Text3.Text)
Call SetStringValue2(HKEY_LOCAL_MACHINE, "SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\{C09A8CD0-2C7A-4866-AAD0-D36CBD733EFC}", "DefaultGateway", Text4.Text)
Call SetStringValue(HKEY_LOCAL_MACHINE, "SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\{C09A8CD0-2C7A-4866-AAD0-D36CBD733EFC}", "NameServer", Text5.Text)
End Sub
Private Sub Form_Load()
Command1.Caption = "写入"
Label1.Caption = "ComputerName:"
Text1.Text = GetStringValue(HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName")
Label2.Caption = "HostName:"
Text2.Text = GetStringValue(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "Hostname")
Label3.Caption = "IP:"
Text3.Text = GetStringValue(HKEY_LOCAL_MACHINE, "SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\{C09A8CD0-2C7A-4866-AAD0-D36CBD733EFC}", "IPAddress")
Label4.Caption = "默认网关:"
Text4.Text = GetStringValue(HKEY_LOCAL_MACHINE, "SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\{C09A8CD0-2C7A-4866-AAD0-D36CBD733EFC}", "DefaultGateway")
Label5.Caption = "DNS服务器:"
Text5.Text = GetStringValue(HKEY_LOCAL_MACHINE, "SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\{C09A8CD0-2C7A-4866-AAD0-D36CBD733EFC}", "NameServer")
End Sub
模块代码如下:
'注册表的入口常量
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
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
Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
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
Public Const REG_SZ = 1
Public Const REG_DWORD = 4
Public Sub savekey(hKey As Long, strPath As String)
Dim keyhand&
r = RegCreateKey(hKey, strPath, keyhand&)
r = RegCloseKey(keyhand&)
End Sub
Public Function GetStringValue(hKey As Long, strPath As String, strValue As String)
Dim keyhand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
r = RegOpenKey(hKey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
GetStringValue = Left$(strBuf, intZeroPos - 1)
Else
GetStringValue = strBuf
End If
End If
End If
End Function
Public Sub SetStringValue(hKey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
r = RegCloseKey(keyhand)
End Sub
Function GetDwordValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long
r = RegOpenKey(hKey, strPath, keyhand)
lDataBufSize = 4
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
If lValueType = REG_DWORD Then
GetDwordValue = lBuf
End If
End If
r = RegCloseKey(keyhand)
End Function
Function SetDwordValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
Dim lResult As Long
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, keyhand)
lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
r = RegCloseKey(keyhand)
End Function
Public Function DeleteKey(lPredefinedKey As Long, sKeyName As String)
'删除主键
Dim lRetVal As Long
Dim hKey As Long
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
lRetVal = RegDeleteKey(lPredefinedKey, sKeyName)
RegCloseKey (hKey)
End Function
Public Function DeleteValue(lPredefinedKey As Long, sKeyName As String, sValueName As String)
'删除键值
Dim lRetVal As Long
Dim hKey As Long
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
lRetVal = RegDeleteValue(hKey, sValueName)
RegCloseKey (hKey)
End Function
Public Function QueryValue(lPredefinedKey As Long, sKeyName As String, sValueName As String)
'获得键值
Dim lRetVal As Long
Dim hKey As Long
Dim vValue As Variant
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
lRetVal = RegQueryValueEx(hKey, sValueName, vValue, 0, 0, 0)
QueryValue = vValue
RegCloseKey (hKey)
End Function