是对 代理服务器 键值的修改 "Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections" 下的 DefaultConnectionSettings,修改可以改IE代理,但就是读取显示"尝试读取或写入受保护的内存。这通常指示其他内存已损坏",求大大们帮帮
本人菜鸟一个,没删注释! Text1.Text的内容为 DefaultConnectionSettings
模块:
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Integer, ByVal lpSubKey As String, ByVal ulOptions As Integer, ByVal samDesired As Integer, ByRef phkResult As Integer) As Integer
'读取注册表的函数
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" ( _
ByVal hKey As Integer, _
ByVal lpValueName As String, _
ByVal lpReserved As Integer, _
ByVal lpType As Integer, _
ByRef lpData As String, _
ByRef ByreflpcbData As Integer _
) As Integer
' Note that if you declare the lpData parameter as String, you must pass it By Value.
'设置注册表的函数
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Integer, ByVal lpValueName As String, ByVal Reserved As Integer, ByVal dwType As Integer, ByVal lpData As String, ByVal cbData As Integer) As Integer ' Note that if you declare the lpData parameter as String, you must pass it By Value.
'关闭注册表的函数
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Integer) As Integer
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Integer, ByVal lpSubKey As String) As Integer
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Integer, ByVal lpValueName As String) As Integer
Public Const HKEY_CURRENT_USER = &H80000001
Public Const sKey = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"
'这句声明表示是以二进制方式打开注册表
Public Const REG_BINARY = 3
Public Const REG_dword = 4&
Public Const internet_option_proxy = 38
Public Const INTERNET_OPTION_SETTINGS_CHANGED = 39
'下面的这些声明是操作注册表的权限,最后一个声明 KEY_ALL_ACCESS 就是把所有的权限都打开了
Public Const STANDARD_RIGHTS_ALL = &H1F0000
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 SYNCHRONIZE = &H100000
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or _
KEY_CREATE_LINK) And (Not SYNCHRONIZE))
下面是具体写读注册表:问题在1111111111 标记处
Public Sub setproxy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setproxy.Click
'hKey接受打开注册表项的句柄,lenData接受要获取的数据的长度
Dim hKey As Integer, lenData As Integer
'以所有可操作的权限打开注册表,并把打开的注册表的句柄保存到 hKey 变量中
RegOpenKeyEx(HKEY_CURRENT_USER, sKey, 0, KEY_ALL_ACCESS, hKey)
'获取指定连接的数据长度放到 lenData 中
RegQueryValueEx(hKey, Trim(Text1.Text), 0, 0, vbNullString, lenData)
Debug.Write(lenData)
'分配一个长度是 lenData 的字节型变量数组存放数据。存取注册表中二进制的数据必须用字节型变量,也就是 Byte 类型的变量
Dim bArr(lenData - 1) As Byte
'以二进制的方式获取 lenData 长度的数据并保存到 bArr 数组中
11111111111:
RegQueryValueEx(hKey, Trim(Text1.Text), 0, REG_BINARY, bArr(0), lenData)
'如果使用代理服务器
bArr(8) = 3
Dim i As Long, j As Long, s As String
s = Trim(proxyT1.Text) '变量等于去掉填写代理服务器地址和端口号的文本框中内容的空格的内容
i = Len(s) '获取去掉空格后的代理服务器地址和端口号的长度,以供后面写入注册表中使用
bArr(12) = i '第十三个数组存放代理服务器地址和端口号的长度
For j = 1 To i '循环范围是从1到代理服务器地址和端口号的长度
bArr(j + 15) = Asc(Mid(s, j, 1)) '依次取出代理服务器地址和端口号的单个字符并且把字符转换成字符的ASCII代码,并且依次放到从第17个数组开始的数组
Next j
'把设置后的数组,也就是要设置的信息放到注册表中
RegSetValueEx(hKey, Trim(Text1.Text), 0, REG_BINARY, bArr(0), UBound(bArr) + 1)
'关闭注册表
RegCloseKey(hKey)
End Sub
急!急!注册表二进制数据 读取不了问题!