Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6862163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:37:40+00:00 2026-05-27T02:37:40+00:00

I am using the following code This error occurs : Attempted to read or

  • 0

I am using the following code

This error occurs :

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

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

Please show me the right way

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:37:40+00:00Added an answer on May 27, 2026 at 2:37 am

    This is usually caused by an incorrect Private Declare Function statement. The types listed in the Windows API are different to those used in VB or C# code. This is a great list of data type conversions between the Windows API and .Net: Win32 API C++ to .NET

    The PInvoke site often has the correct VB code listed.

    For RegEnumValue, fix the data types, and lpcValueName is a ByRef, not a 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
    

    For RegCloseKey, just fix the data types:

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

    For RegOpenKey, fix the data types and change phkResult to a ByRef:

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

    So your function should look more like this. Unfortunately, I’m not sure what to write for strData or RetvalData. I added in a Try/Finally block which will make sure RegCloseKey is called even if an error occurs. You want to make sure you always close things, especially if something goes wrong.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using following code but cannot return data from MySQL. This is the output:
I'm using the following code to update a DataGridView's cell value. This is called
I'm using the following code.. $(document).ready(function(){ $(#test a).click(function(){ var labelTo = $(this).text(); window.location =
I have the following code snippet: using (SPSite site = new SPSite(this.ListAddress)) { using
I am using 'git' to checkout chromium code by following this document: http://code.google.com/p/chromium/wiki/UsingGit And
This is using the web app framework, not Django. The following template code is
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,
I am using following code to check whether a check box on my website
I am using following code to design my home page. The output (as shown
I open a pop-up window using following code in main.html function openwindow(url) { window.open(url,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.