win2000下如何操作注册表呢?!

friendwei 2003-06-13 11:36:09
以前可以在win98下很方便地对注册表地进行读写,但现在win2000里就不好使了呀!!!谁有这方面的例子呀?!
...全文
15 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
weill 2003-06-13
  • 打赏
  • 举报
回复
HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", hKey
RegSetValueEx hKey, "MYEXE", 0, REG_SZ, ByVal "D:\MYEXE\MYEXE.EXE", Len("D:\MYEXE\MYEXE.EXE")

改变上面这一串,就可以改变你写到注册表中的位置了。怎么改,你应该能看懂,如果不能,呵,你就没资格去谈注册表读写了。
weill 2003-06-13
  • 打赏
  • 举报
回复
你可以把我的代码放入一个模块中去,你需要改进的只是那两个公共函数。应该很容易改造的,对吧,呵。不过我建议你还是要写什么改什么,因为那东东的字串太长了。
weill 2003-06-13
  • 打赏
  • 举报
回复
还是看我的吧:
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_SZ = 1


Public Function REGWRIT_ME()
'本函数将"D:\MYEXE\MYEXE.EXE"加入到注册表中,让它在电脑起动时自动运行。
Dim hKey As Long
RegCreateKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", hKey
RegSetValueEx hKey, "MYEXE", 0, REG_SZ, ByVal "D:\MYEXE\MYEXE.EXE", Len("D:\MYEXE\MYEXE.EXE")
RegCloseKey hKey
End Function

Public Function REGDEL_ME()
'本函数删了"D:\MYEXE\MYEXE.EXE",让它不能开机自动运行。
RegCreateKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", hKey
RegDeleteValue hKey, "MYEXE"
RegCloseKey hKey
End Function
friendwei 2003-06-13
  • 打赏
  • 举报
回复
IWshRuntimeLibrary.IWshShell_Class 要引用是哪个类呀?
sxs69 2003-06-13
  • 打赏
  • 举报
回复
1、RegOpenKey:打开SubKey
RegCreateKey:建立SubKey
RegCloseKey:关闭打开SubKey

2、存取Value:RegQueryValue:读取某Key的默认值
RegQueryValueEx:读取某Key的特定名称值
RegEnumValue:列举某Key所有名称值
RegDeleteValue、:删除某Key的某一名称
RegsetValue:设置某Key的默认值
RegsetValueEx:设置某Key的特定名称值

3、存取Key: RegEnumKey:逐一列举SubKey
RegQueryinfoKey:查询Key的消息
RegDeleteKey:删除Key或SubKey
ydzqw 2003-06-13
  • 打赏
  • 举报
回复
RegQueryValue
regsetvalue
真方天画戟 2003-06-13
  • 打赏
  • 举报
回复
注册表操作


  使用Windows Script Host对象还可以进行注册表操作,例如建立、修改、读取、删除主键或者键值等。而且只需要三个函数,比使用Windows API来说方便的多。下面的语句就是一个建立和删除注册表键值的范例:


  Dim WSHShell As New IWshRuntimeLibrary.IWshShell_Class


  ’在HKEY_CURRENT_USER下建立一个项,将默认值设置为Top level key


  WSHShell.RegWrite ″HKCU\MyRegKey\″,″Top level key″


  ’在MyRegKey下建立一个子项,将默认值设置为Second level key


  WSHShell.RegWrite ″HKCU\MyRegKey\Entry\″,″Second level key″


  ’在MyRegKey下建立一个值,名称为Value


  WSHShell.RegWrite ″HKCU\MyRegKey\Value″, 1


  ’在MyRegKey下建立一个值,名称为Entry,类型设置为REG_DWORD


  WSHShell.RegWrite ″HKCU\MyRegKey\Entry″,2,″REG_DWORD″


  WSHShell.RegWrite ″HKCU\MyRegKey\Entry\Value1″,3,″REG_BINARY″


  ’删除相应的键


  WSHShell.RegDelete ″HKCU\MyRegKey\Entry\Value1″


  WSHShell.RegDelete ″HKCU\MyRegKey\Entry\″


  WSHShell.RegDelete ″HKCU\MyRegKey\″


  上面的程序建立并删除建立的项,要看到程序运行效果,可以将上面的最后三句RegDelete语句删除,然后打开注册表编辑器察看。从上面的语句可以看到,使用RegWrite语句可以建立和修改一个项或者值,如果第一个参数字符串最后有一个“\”符号的话就是操作一个项,否则就是操作一个值。
friendwei 2003-06-13
  • 打赏
  • 举报
回复
呵呵。来给分啦!这分你们真容易得呀。
Alicky 2003-06-13
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio\VB98\Template\Code
这个目录下有“标准答案”
friendwei 2003-06-13
  • 打赏
  • 举报
回复
"标准答案"也不好使。我现在想知道如读。
lihonggen0 2003-06-13
  • 打赏
  • 举报
回复
'This program needs 3 buttons
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_BINARY = 3 ' Free form binary
Const HKEY_CURRENT_USER = &H80000001
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private 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
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, lpData As Any, ByVal cbData As Long) As Long
Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
'Create a buffer
strBuf = String(lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 Then
'Remove the unnecessary chr$(0)'s
RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End If
ElseIf lValueType = REG_BINARY Then
Dim strData As Integer
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 Then
RegQueryStringValue = strData
End If
End If
End If
End Function
Function GetString(hKey As Long, strPath As String, strValue As String)
Dim Ret
'Open the key
RegOpenKey hKey, strPath, Ret
'Get the key's content
GetString = RegQueryStringValue(Ret, strValue)
'Close the key
RegCloseKey Ret
End Function
Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Save a string to the key
RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
'close the key
RegCloseKey Ret
End Sub
Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Set the key's value
RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
'close the key
RegCloseKey Ret
End Sub
Sub DelSetting(hKey As Long, strPath As String, strValue As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Delete the key's value
RegDeleteValue Ret, strValue
'close the key
RegCloseKey Ret
End Sub
Private Sub Command1_Click()
Dim strString As String
'Ask for a value
strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
If strString = "" Or Val(strString) > 255 Or Val(strString) < 0 Then
MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
Exit Sub
End If
'Save the value to the registry
SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
End Sub
Private Sub Command2_Click()
'Get a string from the registry
Ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
If Ret = "" Then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
End Sub
Private Sub Command3_Click()
'Delete the setting from the registry
DelSetting HKEY_CURRENT_USER, "KPD-Team", "BinaryValue"
MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
End Sub
Private Sub Form_Load()

Command1.Caption = "Set Value"
Command2.Caption = "Get Value"
Command3.Caption = "Delete Value"
End Sub
friendwei 2003-06-13
  • 打赏
  • 举报
回复
to weill(哎呀……) : 如何读呢?
topikachu 2003-06-13
  • 打赏
  • 举报
回复
to weill(哎呀……)
不要那么狂!
连RegQueryValueEx都没有,你有什么资格在这诈唬的!!

win98和nt(包括2000)的内码不一样,98是ansi一个字节,nt是unicode两个字节,所以直接的注册表读写会有差异
解决办法就是最好能一个字节一个字节的读

现有的组件如alexfu(云) 所说的是已经封装好的,在所有平台上都可以运行,不过前提是要安装ie4(废话,现在还有不装ie的么)

如果你遇到的机器土的掉渣,是nt4没装sp3或者win95,那就要用api函数了
不过,有现成的
在你vb目录下面找到\Template\Code文件夹,里面有一个模块是注册表访问的,我装得是中文版vb就叫"注册表访问.bas".英文版的vb有对应文件名!

7,762

社区成员

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

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