如何修改REG_DWORD的注册表键值。。。我的方法老是不对

cow_boy 2001-12-25 12:44:26
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, _
ByVal lpData As Any, _
ByVal cbData As Long _
) As Long '创建或改变一个键值,lpData应由缺省的ByRef型改为ByVal型
Const REG_DWORD = 4& 'DWORD 值

Call RegOpenKey(HKEY_CURRENT_USER, "Software\Policies\Microsoft\Internet Explorer\Control Panel", nKeyHandle)
Call RegSetValueEx(nKeyHandle, "HomePage", 0, REG_DWORD, "Ox00000001(1)", 255) '1禁用,0启用
...全文
468 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cow_boy 2001-12-26
  • 打赏
  • 举报
回复
今天结账
uguess 2001-12-25
  • 打赏
  • 举报
回复
Call RegSetValueEx(nKeyHandle, "HomePage", 0, REG_DWORD, 1, 255) '1禁用,0启用
cow_boy 2001-12-25
  • 打赏
  • 举报
回复
Call RegOpenKey(HKEY_CURRENT_USER, "Software\Policies\Microsoft\Internet Explorer\Control Panel", nKeyHandle)
Call RegSetValueEx(nKeyHandle, "HomePage", 0, REG_DWORD, "1", 4) '1禁用,0启用
我这样改后,HomePage的值为31,我如何才能改为1.
renren6250 2001-12-25
  • 打赏
  • 举报
回复
gzgz
长弓落日 2001-12-25
  • 打赏
  • 举报
回复
改用DELPHI吧!
否则,请勿捣乱!
http://www.csdn.net/expert/Topic/441/441125.shtm
老北漂 2001-12-25
  • 打赏
  • 举报
回复
操作注册表全函数


Option Explicit
'String
Global Const REG_SZ As Long = 1
'Dword
Global Const REG_DWORD As Long = 4
'Binary
Global Const REG_BINARY As Long = 3

Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_USERS = &H80000003

Global Const ERROR_NONE = 0
Global Const ERROR_SUCCESS = 0&
Global Const ERROR_BADDB = 1
Global Const ERROR_BADKEY = 2
Global Const ERROR_CANTOPEN = 3
Global Const ERROR_CANTREAD = 4
Global Const ERROR_CANTWRITE = 5
Global Const ERROR_OUTOFMEMORY = 6
Global Const ERROR_INVALID_PARAMETER = 7
Global Const ERROR_ACCESS_DENIED = 8
Global Const ERROR_INVALID_PARAMETERS = 87
Global Const ERROR_NO_MORE_ITEMS = 259

Global Const KEY_ALL_ACCESS = &H3F

Global Const REG_OPTION_NON_VOLATILE = 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 RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition 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 RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, 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
Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hkey As Long, ByVal lpValueName As String) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hkey As Long, ByVal lpSubKey As String) As Long
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

Global Const MyAppKey = "SoftWare\ScriptServer\" '本软件所在注册表的主键
Global Const AppKey = "SoftWare\ScriptServer\1.0\" '应用程序所在注册表的主键
Global Const UserKey = "SoftWare\ScriptServer\Users\" '用户信息所在注册表的主键


Public Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
'Usage: CreateNewKey "KeyName\SubKey\SubSubKey", HKEY_CURRENT_USER
Dim hNewKey As Long 'handle to the new key
Dim lRetVal As Long 'result of the RegCreateKeyEx function
lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal)
RegCloseKey (hNewKey)
If lRetVal <> 0 Then Exit Sub
End Sub

Public Sub DeleteValue(sKeyName As String, sValueName As String, lPredefinedKey As Long)
'Usage: DeleteKey "Test1\Test2\Test3","ValueName",HKEY_CURRENT_USER
Dim lRetVal As Long 'result of the SetValueEx function
Dim hkey As Long 'handle of open key
'open the specified key
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hkey)
lRetVal = RegDeleteValue(hkey, sValueName)
RegCloseKey (hkey)
If lRetVal <> 0 Then Exit Sub
End Sub

Public Sub DeleteKey(sKeyName As String, sDelKeyName As String, lPredefinedKey As Long)
'Usage: DeleteKey "Test1\Test2","Test3",HKEY_CURRENT_USER
Dim lRetVal As Long 'result of the SetValueEx function
Dim hkey As Long 'handle of open key
'open the specified key
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hkey)
lRetVal = RegDeleteKey(hkey, sDelKeyName)
RegCloseKey (hkey)
If lRetVal <> 0 Then Exit Sub
End Sub

Public Sub SetKeyValue(sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long, lPredefinedKey As Long)
'Usage: SetKeyValue "TestKey\SubKey1", "StringValue", "Hello", REG_SZ, HKEY_CURRENT_USER
'NOTE: Binary values are reversed in registry: 95 00 00 00 in the registry must be entered as 00 00 00 95
Dim lRetVal As Long 'result of the SetValueEx function
Dim hkey As Long 'handle of open key
'open the specified key
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hkey)
lRetVal = SetValueEx(hkey, sValueName, lValueType, vValueSetting)
RegCloseKey (hkey)
If lRetVal <> 0 Then Exit Sub
End Sub

Private Function SetValueEx(ByVal hkey As Long, sValueName As String, lType As Long, ByVal vValue As Variant) As Long
'Called by SetKeyValue
Dim lValue As Long
Dim sValue As String
Select Case lType
Case REG_SZ
sValue = vValue & Chr$(0)
SetValueEx = RegSetValueExString(hkey, sValueName, 0&, lType, sValue, Len(sValue) * 2)
Case REG_DWORD, REG_BINARY
lValue = vValue
SetValueEx = RegSetValueExLong(hkey, sValueName, 0&, lType, lValue, 4)
End Select
End Function

Private Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
'Called By Query Value
Dim cch As Long
Dim lrc As Long
Dim lType As Long
Dim lValue As Long
Dim sValue As String

On Error GoTo QueryValueExError

' Determine the size and type of data to be read
lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
If lrc <> ERROR_NONE Then Error 5

Select Case lType
' For strings
Case REG_SZ:
sValue = String(cch, 0)
lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
If lrc = ERROR_NONE Then
vValue = Left$(sValue, cch)
Else
vValue = Empty
End If
' For DWORDS
Case REG_DWORD, REG_BINARY:
lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
If lrc = ERROR_NONE Then vValue = lValue
Case Else
'all other data types not supported
lrc = -1
End Select

QueryValueExExit:
QueryValueEx = lrc
Exit Function
QueryValueExError:
Resume QueryValueExExit
End Function

Public Function QueryValue(sKeyName As String, sValueName As String, lPredefinedKey As Long) As Variant
'Usage: QueryValue "TestKey\SubKey1", "StringValue",HKEY_CURRENT_USER
Dim lRetVal As Long 'result of the API functions
Dim hkey As Long 'handle of opened key
Dim vValue As Variant 'setting of queried value
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hkey)
lRetVal = QueryValueEx(hkey, sValueName, vValue)
QueryValue = vValue
RegCloseKey (hkey)
If lRetVal <> 0 Then Exit Function
End Function

'写注册表
Public Function WriteReg(ActionType As String, KeyName As String, ByVal KeyValue As String) As Long
On Error GoTo Err
Dim hkey As Long
RegOpenKeyEx HKEY_CURRENT_USER, ActionType, 0, KEY_ALL_ACCESS, hkey
'RegCreateKey HKEY_CURRENT_USER, ActionType, hkey
RegSetValueEx hkey, KeyName, 0, REG_SZ, ByVal KeyValue, Len(KeyValue) + 1
RegCloseKey hkey
WriteReg = 1
Exit Function
Err:
WriteReg = 0
End Function

'读注册表
Public Function ReadReg(ActionType As String, KeyName As String) As String
On Error GoTo Err
Dim hkey As Long, lenData As Long, typeData As Long
Dim sS As String
RegOpenKeyEx HKEY_CURRENT_USER, ActionType, 0, KEY_ALL_ACCESS, hkey
'RegCreateKey HKEY_CURRENT_USER, ActionType, hkey
RegQueryValueEx hkey, KeyName, 0, typeData, ByVal vbNullString, lenData
sS = String(lenData, Chr(0))
RegQueryValueEx hkey, KeyName, 0, typeData, ByVal sS, lenData '注意ByVal千万别忘了
sS = Left(sS, InStr(sS, Chr(0)) - 1)
RegCloseKey hkey
ReadReg = sS
Exit Function
Err:
ReadReg = ""
End Function

fraser01 2001-12-25
  • 打赏
  • 举报
回复
' This code is licensed according to the terms and conditions listed here.

' Create a key called HKEY_CURRENT_USER\Software\MyCorp\MyProgram\Config.
' Then create a "username" value under that key and set its value to "Rimmer".
Dim hregkey As Long ' receives handle to the newly created or opened registry key
Dim secattr As SECURITY_ATTRIBUTES ' security settings of the key
Dim subkey As String ' name of the subkey to create
Dim neworused As Long ' receives 1 if new key was created or 2 if an existing key was opened
Dim stringbuffer As String ' the string to put into the registry
Dim retval As Long ' return value

' Set the name of the new key and the default security settings
subkey = "Software\MyCorp\MyProgram\Config"
secattr.nLength = Len(secattr) ' size of the structure
secattr.lpSecurityDescriptor = 0 ' default security level
secattr.bInheritHandle = True ' the default value for this setting

' Create or open the registry key
retval = RegCreateKeyEx(HKEY_CURRENT_USER, subkey, 0, "", 0, KEY_WRITE, secattr, hregkey, neworused)
If retval <> 0 Then ' error during open
Debug.Print "Error opening or creating registry key -- aborting."
End ' terminate the program
End If

' Write the string to the registry. Note that because Visual Basic is being used, the string
' passed to the function must explicitly be passed ByVal.
stringbuffer = "Rimmer" & vbNullChar ' note how a null character must be appended to the string
retval = RegSetValueEx(hregkey, "username", 0, REG_SZ, ByVal stringbuffer, Len(stringbuffer)) ' write the string

cow_boy 2001-12-25
  • 打赏
  • 举报
回复
这我已经试过了。出错
说类型不对,
错误是那个1

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧