7,763
社区成员
发帖
与我相关
我的任务
分享
Call CreateKey(HKEY_LOCAL_MACHINE, "Software\123", 1)
Call SetValue(HKEY_LOCAL_MACHINE, "456", "中文")
它总是直接在HKEY_LOCAL_MACHINE下创建键为"456"键值为"中文",大师能不能具体指点下如何才能正确创建?
读取我用的是:Call GetValue(HKEY_LOCAL_MACHINE, "456")能正确读到.
菜鸟没地方去问了,本来就不懂网上的一些贴出来的代码是没验证过的,缺这少那的要不运行不了要不根本就不能读改写.
求教!Private Sub Form_Load()
Dim w As Object
Set w = CreateObject("wscript.shell")
Call w.regwrite("HKEY_LOCAL_MACHINE\Software\123\456", "中文")
MsgBox 检测注册表项是否存在("HKEY_LOCAL_MACHINE\Software\123\456")
MsgBox 检测注册表项是否存在("HKEY_LOCAL_MACHINE\Software\123\4567")
End Sub
Function 检测注册表项是否存在(Key As String) As Boolean
On Error GoTo Err1
Dim w As Object
Set w = CreateObject("wscript.shell")
Dim V
V = w.REGREAD(Key)
检测注册表项是否存在 = True
Exit Function
Err1:
检测注册表项是否存在 = False
End Function
Private Sub Command1_Click()
'ie收藏夹默认路径文件夹:%USERPROFILE%\Favorites
On Error Resume Next
Dim w As Object
Set w = CreateObject("wscript.shell")
Dim 原收藏夹路径 As String, 新收藏夹路径 As String
'读取注册表项
原收藏夹路径 = w.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites")
'
'更改注册表项
新收藏夹路径 = "d:\IE收藏夹" & Format(Now, "yyyy-mm-dd hh:nn:ss")
MkDir 新收藏夹路径
Call w.RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites", 新收藏夹路径)
MsgBox "原收藏夹路径:" & 原收藏夹路径 & vbCrLf & "新收藏夹路径:" & w.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites")
'Call w.RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Favorites", "%USERPROFILE%\Favorites")
End Sub
Private Sub Command2_Click()
Dim w As Object
Set w = CreateObject("wscript.shell")
'添加一个新项目
Call w.RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\试验项", Now)
MsgBox "请打开注册表定位该处,然后按F5,检查下是否多了一个项目"
'删除一个项目
Call w.RegDelete("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\试验项")
MsgBox "请打开注册表定位该处,然后按F5,检查下是否这个项目消失了"
End Sub
Attribute VB_Name = "basReg"
Option Explicit
' Reg Data Types...
Public Const REG_NONE = 0 ' No defined value type.
Public Const REG_SZ = 1 ' A null-terminated string. It will be a Unicode or ANSI string, depending on whether you use the Unicode or ANSI functions.
Public Const REG_EXPAND_SZ = 2 ' A null-terminated string that contains unexpanded references to environment variables (for example, "%PATH%"). It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions.
Public Const REG_BINARY = 3 ' Free form binary
Public Const REG_DWORD = 4 ' 32-bit number
Public Const REG_DWORD_LITTLE_ENDIAN = 4 ' A 32-bit number in little-endian format (same as REG_DWORD). In little-endian format, the most significant byte of a word is the high-order word. This is the most common format for computers running Windows NT and Windows 95.
Public Const REG_DWORD_BIG_ENDIAN = 5 ' A 32-bit number in big-endian format. In big-endian format, the most significant byte of a word is the low-order word.
Public Const REG_LINK = 6 ' A Unicode symbolic link.
Public Const REG_MULTI_SZ = 7 ' An array of null-terminated strings, terminated by two null characters.
Public Const REG_RESOURCE_LIST = 8 ' A device-driver resource list.
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9 ' Resource list in the hardware description
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10
' Reg Create Type Values...
Public Const REG_OPTION_RESERVED = 0 ' Parameter is reserved
Public Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
Public Const REG_OPTION_VOLATILE = 1 ' Key is not preserved when system is rebooted
Public Const REG_OPTION_CREATE_LINK = 2 ' Created key is a symbolic link
Public Const REG_OPTION_BACKUP_RESTORE = 4 ' open for backup or restore
' Reg Key Security Options...
Public Const READ_CONTROL = &H20000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
Public Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
Public Const KEY_EXECUTE = KEY_READ
Public Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS _
+ KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
' Reg Key ROOT Types...
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
'Registry Types
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
Public Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKeyExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegEnumKeyExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long
Public Declare Function RegEnumValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Public Declare Function RegOpenKeyExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Public Declare Function RegQueryInfoKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
Public Declare Function RegQueryValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Public Declare Function RegSetValueExA Lib "advapi32.dll" (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
Sub Main()
End Sub
REG.CLS
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Service"
Attribute VB_Creatable = True
Attribute VB_Exposed = True
Option Explicit
Public Function CloseKey(hKey As Long) As Boolean
If RegCloseKey(hKey) Then
CloseKey = False
Else
CloseKey = True
End If
End Function
Public Function CreateKey(hKey As Long, sSubKey As String, sClass As String) As Long
Dim hKeyBuffer As Long
Dim lErr As Long
Dim lAccessDesired As Long
Dim lOptions As Long
Dim lDisposition As Long
lAccessDesired = KEY_ALL_ACCESS
lOptions = 0
lDisposition = 0
If RegCreateKeyExA(hKey, sSubKey, 0, sClass, lOptions, lAccessDesired, vbNull, hKeyBuffer, lDisposition) Then
CreateKey = False
Else
CreateKey = hKeyBuffer
End If
End Function
Public Function DeleteKey(hKey As Long, sSubKey As String) As Boolean
'Win95 will delete a Registry Key and all of it's descendants.
'WinNT will delete a Registry Key ONLY if NO descendants are present.
If RegDeleteKey(hKey, sSubKey) Then
DeleteKey = False
Else
DeleteKey = True
End If
End Function
Public Function DeleteValue(hKey As Long, sName As String) As Boolean
If RegDeleteValue(hKey, sName) Then
DeleteValue = False
Else
DeleteValue = True
End If
End Function
Public Function GetKey(hKey As Long, sSubKey As String) As Long
Dim lAccessDesired As Long
Dim hKeyBuffer As Long
lAccessDesired = KEY_ALL_ACCESS
If RegOpenKeyExA(hKey, sSubKey, 0, lAccessDesired, hKeyBuffer) Then
GetKey = False
Else
GetKey = hKeyBuffer
End If
End Function
Public Function GetValue(hKey As Long, sName As String) As String
Dim sData As String
sData = Space(254)
If RegQueryValueExA(hKey, sName, 0, REG_SZ, ByVal sData, Len(sData)) Then
GetValue = ""
Else
GetValue = Left$(sData, InStr(sData, Chr$(0)) - 1)
End If
End Function
Public Function SetValue(hKey As Long, sName As String, sData As String) As Boolean
If RegSetValueExA(hKey, sName, 0, REG_SZ, ByVal sData, Len(sData)) Then
SetValue = False
Else
SetValue = True
End If
End Function