如何修改REG_DWORD的注册表键值。。。我的方法老是不对
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启用
问题点数:100、回复次数:8Top
1 楼uguess(天地间,有我在行走!)回复于 2001-12-25 08:37:50 得分 0
Call RegSetValueEx(nKeyHandle, "HomePage", 0, REG_DWORD, 1, 255) '1禁用,0启用
Top
2 楼cow_boy()回复于 2001-12-25 14:42:25 得分 0
这我已经试过了。出错
说类型不对,
错误是那个1
Top
3 楼fraser01(王晓栋)回复于 2001-12-25 14:51:43 得分 40
' 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
Top
4 楼zhuho()回复于 2001-12-25 14:53:21 得分 60
操作注册表全函数
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
Top
5 楼z_x_b(长弓落日)回复于 2001-12-25 14:55:06 得分 0
改用DELPHI吧!
否则,请勿捣乱!
http://www.csdn.net/expert/Topic/441/441125.shtmTop
6 楼renren6250(刀客)回复于 2001-12-25 15:26:30 得分 0
gzgzTop
7 楼cow_boy()回复于 2001-12-25 15:41:43 得分 0
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.
Top
8 楼cow_boy()回复于 2001-12-26 17:41:07 得分 0
今天结账Top




