4 楼
caline [专家分:20] 发布于 2009-01-14 20:12:00
用注册表!下面是我编的一个小程序,适用XP系统,但我运行时总是提示0x77da7904指令引用的0x0000008内存不能被read!郁闷中谁能说下为什么?
Option Explicit
Const REG_SZ As Long = 1
Const HKEY_LOCAL_MACHINE = &H80000002
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) 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 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 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 RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Dim aaa As String
Private Sub Command1_Click()
Dim hKey As Long, hKey1 As Long, hKey2 As Long, ret As Long, ret1 As Long, lenData As Long, typeData As Long
Dim Name As String, name1 As String, name2 As String, s As String, S1 As String
Dim name3, name4, s2, s3 As String
Dim ret2, ret3 As Long
Dim idx As Integer
idx = 0
Name = String(256, Chr(0))
RegCreateKey HKEY_LOCAL_MACHINE, "SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces", hKey
Text1.Text = hKey
Do
ret = RegEnumKey(hKey, idx, Name, Len(Name))
If ret = 0 Then
aaa = Left(Name, InStr(Name, Chr(0)) - 1)
aaa = "SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\" & aaa
ret = RegOpenKey(HKEY_LOCAL_MACHINE, aaa, hKey)
'获得IP地址
If ret = 0 Then
name1 = "IPAddress"
name2 = "SubnetMask"
name3 = "DefaultGateway"
name4 = "NameServer"
ret = RegQueryValueEx(hKey, name1, 0, typeData, ByVal vbNullString, lenData)
s = String(lenData, Chr(0))
RegQueryValueEx hKey, name1, 0, typeData, ByVal s, lenData
If s <> "" Then s = Left(s, InStr(s, Chr(0)) - 1)
'获得子网掩码
ret1 = RegQueryValueEx(hKey, name2, 0, typeData, ByVal vbNullString, lenData)
S1 = String(lenData, Chr(0))
RegQueryValueEx hKey, name2, 0, typeData, ByVal S1, lenData
If S1 <> "" Then S1 = Left(S1, InStr(S1, Chr(0)) - 1)
'获取网关
ret2 = RegQueryValueEx(hKey, name3, 0, typeData, ByVal vbNullString, lenData)
s2 = String(lenData, Chr(0))
RegQueryValueEx hKey, name3, 0, typeData, ByVal s2, lenData
If s2 <> "" Then s2 = Left(s2, InStr(s2, Chr(0)) - 1)
'获取DNS服务器
ret3 = RegQueryValueEx(hKey, name4, 0, typeData, ByVal vbNullString, lenData)
s3 = String(lenData, Chr(0))
RegQueryValueEx hKey, name4, 0, typeData, ByVal s3, lenData
If s3 <> "" Then s3 = Left(s3, InStr(s3, Chr(0)) - 1)
If Val(s) > 0 Then
Text1.Text = s
Text2.Text = S1
Text3.Text = s2
Text4.Text = s3
Command2.Enabled = True
Exit Sub
End If
End If
idx = idx + 1
End If
Loop Until ret <> 0
End Sub
Private Sub Command2_Click()
Dim hKey As Long
If Text1.Text <> "" Then
RegCreateKey HKEY_LOCAL_MACHINE, aaa, hKey
' 修改IP地址
RegSetValueEx hKey, "IPAddress", 0, REG_MULTI_SZ, ByVal Text1.Text, 13
' 修改子网掩码
RegSetValueEx hKey, "SubnetMask", 0, REG_MULTI_SZ, ByVal Text2.Text, 13
'修改网关
RegSetValueEx hKey, "DefaultGateway", 0, REG_MULTI_SZ, ByVal Text3.Text, 13
'修改服务器
RegSetValueEx hKey, "NameServer", 0, REG_MULTI_SZ, ByVal Text4.Text, 13
RegCloseKey hKey
End If
End Sub
Private Sub Command3_Click()
End
End Sub