Click here to Skip to main content
14,984,270 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I am having some problems with this line of code

VB
If RegQueryValueEx(hKey, Value, 0, pdwType, Nothing, pvSize) = ERROR_NONE Then


to anyone with knowledge about the registry I would like to know how to correct this line so that it works with my vb.net application.


the variable not listed in the function is

VB
Private Const ERROR_NONE As Integer = &H0



And here it is once more as a full function

VB
Public Function ValueExists(ByVal RootKey As ROOT_KEY, ByVal SubKey As String, ByVal Value As String) As Boolean
           Dim hKey As UIntPtr = UIntPtr.Zero
           Dim pvSize As UInteger = 0
           Dim pdwType As UInteger = CUInt(VALUE_TYPE.REG_NONE)

           Try
               If RegOpenKeyEx(RootKey, SubKey, 0, KEY_QUERY_VALUE, hKey) <> ERROR_NONE Then
                   Return False
               End If
               Return (RegQueryValueEx(hKey, Value, 0, pdwType, Nothing, pvSize) = ERROR_NONE)
           Finally
               If hKey <> UIntPtr.Zero Then
                   RegCloseKey(hKey)
               End If
           End Try
       End Function




I have translated this from C# to vb.net but get the error in full

Error 3 Overload resolution failed because no accessible 'RegQueryValueEx' is most specific for these arguments:
'Public Shared Function RegQueryValueEx(hKey As System.UIntPtr, lpValueName As String, lpReserved As Integer, ByRef lpType As UInteger, ByRef lpData As Byte, ByRef lpcbData As UInteger) As Integer': Not most specific.
'Public Shared Function RegQueryValueEx(hKey As System.UIntPtr, lpValueName As String, lpReserved As Integer, ByRef lpType As UInteger, lpData As System.Text.StringBuilder, ByRef lpcbData As UInteger) As Integer': Not most specific. C:\Users\Admin\Desktop\cLightning.vb 1095 24
Posted
Updated 5-Jul-12 18:08pm
v2
Comments
   
Please add to your questions: all declarations of all methods ReqQueryValueEx.
The problem is extremely simple.
--SA

1 solution

Most likely, the problem is in the parameter Nothing. This is a null pointer, and it's not possible to figure out what type do you imply, because there is more than one method named RegQueryValueEx, and this Nothing can be interpreted as one or another type, because your Nothing might mean any reference.

What to do? I don't know your declarations (please see my comment to the question; is it P/Invoked Windows API? Why?), but you need to use either array of bytes of the instance of StringBuilder. Declare a variable of one and another type, initialize it with Nothing and pass this explicitly defined variable to the method. This is a usual way of resolving such issues.

There are other problems in this code. You return the return result of this method, which is integer, but ValueExists expects Boolean.

However, you don't need that stuff at all. Use the .NET class Microsoft.Win32.Registry and related classes, stop torturing yourself:
http://msdn.microsoft.com/en-US/library/microsoft.win32.registry.aspx[^].

[EDIT #1]

Answering a follow-up question:

Cleaning the registry is not easy and not safe. I would start from finding a good registry repair program. And such program should be the one which can backup all the registry and restore it. As you can screw up the registry to the point where you cannot boot or normally operate your OS, this repair program should boot its own OS from a flash drive or something.

I don't know what exactly to use but 100% sure this is possible. I would start looking from here:
http://en.wikipedia.org/wiki/List_of_data_recovery_software[^],
http://en.wikipedia.org/wiki/List_of_tools_to_create_Live_USB_systems[^],
http://en.wikipedia.org/wiki/System_Restore[^].

It's hard to say where to start with the registry clean up. I have tried only a limited subset of cleaning. For example, you can locate all references to file system objects and detect those pointing to non-existing files/directories. These entries should be eventually removed, but first, you need to walk the registry to remove the entries where the entries you would remove are used and do it all recursively. Again, this is not easy and not safe. Thoroughly plan each step and theoretically calculate possible adverse effects, back up…

[EDIT #2]

Sorry, probably I did not get it: if you not going to modify the registry, you are safe. Anyway, I hope the notes of [EDIT #1] could be useful.

—SA
   
v3
Comments
Dale 2012 6-Jul-12 1:49am
   
to be perfectly honest I have found a real nice example here on code project with the name ScanX which is a registry cleaner. How and where might I start when it comes to simply enumerating the registry root keys and sub keys so that I can start to code the parts that will check the registry for invalid objects?. At this point I would like to pass each key being enumerated to a label so that it will be visible. I will not ask questions like how do I make a registry cleaner of course but it would be of great help if you or someone could supply a small example of just that using the microsoft.win32.registry class that you talk of.

thank you very much your response is just what i needed to hear so that I dont pull my hair out with old code!!
   
You are welcome. Please see my updated answer, after [EDIT].
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900