gpt4 book ai didi

vb.net - 尝试读取或写入 protected 内存。这通常表明其他内存已损坏

转载 作者:行者123 更新时间:2023-12-02 03:29:06 25 4
gpt4 key购买 nike

我正在使用以下代码

出现此错误:

尝试读取或写入 protected 内存。这通常表明其他内存已损坏。尝试读取或写入 protected 内存。这通常表明其他内存已损坏。

Public Class FormRegEnumValue

Private Const ERROR_SUCCESS = 0&
Private Const ERROR_NO_MORE_ITEMS = 259&
Private Const HKEY_CURRENT_USER = &H80000001

Private Const REG_BINARY = 3
Private Const REG_DWORD = 4
Private Const REG_EXPAND_SZ = 2
Private Const REG_SZ = 1

Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, ByVal lpcbValueName As Long, ByVal lpReserved As Long, ByVal lpType As Long, ByVal lpData As Object, ByVal lpcbData As Long) As Long
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, ByVal phkResult As Long) As Long


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hKey As Long, num As Long, strName As String
Dim strData As String, Retval As Long, RetvalData As Long

Const Buffer As Long = 255
num = 0
strName = Space(Buffer)
strData = Space(Buffer)
Retval = Buffer
RetvalData = Buffer
If RegOpenKey(HKEY_CURRENT_USER, "Control Panel\Desktop", hKey) = 0 Then 'error
While RegEnumValue(hKey, num, strName, Retval, 0, 0&, strData, RetvalData) <> ERROR_NO_MORE_ITEMS
If RetvalData > 0 Then
ListBox1.Items.Add(strName + Retval + " = " + strData + RetvalData - 1)
End If
num = num + 1
strName = Space(Buffer)
strData = Space(Buffer)
Retval = Buffer
RetvalData = Buffer
End While
RegCloseKey(hKey)
Else
ListBox1.Items.Add("Error")
End If
End Sub
End Class

请告诉我正确的方法

最佳答案

这通常是由不正确的私有(private)声明函数语句引起的。 Windows API 中列出的类型与 VB 或 C# 代码中使用的类型不同。这是 Windows API 和 .Net 之间的数据类型转换的重要列表:Win32 API C++ to .NET

PInvoke网站通常会列出正确的 VB 代码。

对于RegEnumValue ,修复数据类型,lpcValueName 是 ByRef,而不是 ByVal:

Declare Auto Function RegEnumValue Lib "Advapi32" ( _
ByVal hKey As IntPtr, _
ByVal dwIndex As Integer, _
ByVal lpValueName As StringBuilder, _
ByRef lpcValueName As Integer, _
ByVal lpReserved As IntPtr, _
ByVal lpType As IntPtr, _
ByVal lpData As IntPtr, _
ByVal lpcbData As IntPtr _
) As Integer

对于RegCloseKey ,只需修复数据类型:

Declare Function RegCloseKey Lib "advapi32.dll" ( _
ByVal hKey As UIntPtr _
) As Integer

对于RegOpenKey ,修复数据类型并将 phkResult 更改为 ByRef:

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" ( _
ByVal hKey As Integer, _
ByVal lpSubKey As String, _
ByRef phkResult As IntPtr _
) As Integer

所以你的函数应该看起来更像这样。不幸的是,我不确定要为 strDataRetvalData 编写什么。我添加了一个 Try/Finally block ,即使发生错误,它也会确保调用 RegCloseKey 。您需要确保始终关闭某些内容,尤其是在出现问题时。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const Buffer As Long = 255
Dim hKey As IntPtr = IntPtr.Zero
Dim num As Integer = 0
Dim strName As New StringBuilder
Dim strData As IntPtr = ' I'm not surte what goes here.
Dim Retval As Integer = Buffer
Dim RetvalData As IntPtr = ' I'm not surte what goes here.
If RegOpenKey(HKEY_CURRENT_USER, "Control Panel\Desktop", hKey) = 0 Then 'error
Try
While RegEnumValue(hKey, num, strName, Retval, IntPtr.Zero, IntPtr.Zero, strData, RetvalData) <> ERROR_NO_MORE_ITEMS
If RetvalData > 0 Then
ListBox1.Items.Add(strName.ToString + Retval + " = " + strData + RetvalData - 1)
End If
num = num + 1
strName = New StringBuilder(Buffer)
strData = ' I'm not sure what goes here.
Retval = Buffer
RetvalData = ' I'm not surte what goes here.
End While
Finally
RegCloseKey(hKey)
End Try
Else
ListBox1.Items.Add("Error")
End If
End Sub

关于vb.net - 尝试读取或写入 protected 内存。这通常表明其他内存已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8262833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com