examining registry keys (RegOpenKeyEx)

Hi-

I want to use VB to examine registry keys, but I can't get to first base. The following code is basically copied from various Web sites that claim to offer code that does what I want. But it won't work for me. The call to RegOpenKeyEx returns a strange value (5613745594171479).

What am I doing wrong?

(P.S. This little test is a console application.)

[code]
Module Module1

Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const REG_SZ = 1
Public Const DELETE = &H10000
Public Const READ_CONTROL = &H20000
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const STANDARD_RIGHTS_READ = READ_CONTROL
Public Const ERROR_SUCCESS = 0&
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
ByVal phkResult As Long) As Long

Sub Main()
Dim lValue As Long ' Variable for value
Dim sKey As String ' Key to open
Dim hKey As Long ' Handle to registry key

hKey = 0&
sKey = "SOFTWAREMicrosoftWindowsCurrentVersionSharedDlls"

lValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKey, 0&, KEY_READ, hKey)
Console.WriteLine(lValue)
End Sub
End Module
[/code]

Comments

  • : Hi-
    :
    : I want to use VB to examine registry keys, but I can't get to first
    : base. The following code is basically copied from various Web sites
    : that claim to offer code that does what I want. But it won't work
    : for me. The call to RegOpenKeyEx returns a strange value
    : (5613745594171479).
    :
    : What am I doing wrong?
    :
    : (P.S. This little test is a console application.)
    :
    : [code]:
    : Module Module1
    :
    : Public Const HKEY_LOCAL_MACHINE = &H80000002
    : Public Const REG_SZ = 1
    : Public Const DELETE = &H10000
    : Public Const READ_CONTROL = &H20000
    : Public Const SYNCHRONIZE = &H100000
    : Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    : Public Const STANDARD_RIGHTS_READ = READ_CONTROL
    : Public Const ERROR_SUCCESS = 0&
    : Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    : Public Const KEY_NOTIFY = &H10
    : Public Const KEY_QUERY_VALUE = &H1
    : Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    : Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    : (ByVal hKey As Long, _
    : ByVal lpSubKey As String, _
    : ByVal ulOptions As Long, _
    : ByVal samDesired As Long, _
    : ByVal phkResult As Long) As Long
    :
    : Sub Main()
    : Dim lValue As Long ' Variable for value
    : Dim sKey As String ' Key to open
    : Dim hKey As Long ' Handle to registry key
    :
    : hKey = 0&
    : sKey = "SOFTWAREMicrosoftWindowsCurrentVersionSharedDlls"
    :
    : lValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKey, 0&, KEY_READ, hKey)
    : Console.WriteLine(lValue)
    : End Sub
    : End Module
    : [/code]:
    :
    RegOpenKeyEx() returns a handle to the registry key, which was opened. You need to use RegGetValue() to get the actual value. See the MSDN for more info on these functions
  • : : Hi-
    : :
    : : I want to use VB to examine registry keys, but I can't get to first
    : : base. The following code is basically copied from various Web sites
    : : that claim to offer code that does what I want. But it won't work
    : : for me. The call to RegOpenKeyEx returns a strange value
    : : (5613745594171479).
    : :
    : : What am I doing wrong?
    : :
    : : (P.S. This little test is a console application.)
    : :
    : : [code]: :
    : : Module Module1
    : :
    : : Public Const HKEY_LOCAL_MACHINE = &H80000002
    : : Public Const REG_SZ = 1
    : : Public Const DELETE = &H10000
    : : Public Const READ_CONTROL = &H20000
    : : Public Const SYNCHRONIZE = &H100000
    : : Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    : : Public Const STANDARD_RIGHTS_READ = READ_CONTROL
    : : Public Const ERROR_SUCCESS = 0&
    : : Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    : : Public Const KEY_NOTIFY = &H10
    : : Public Const KEY_QUERY_VALUE = &H1
    : : Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    : : Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    : : (ByVal hKey As Long, _
    : : ByVal lpSubKey As String, _
    : : ByVal ulOptions As Long, _
    : : ByVal samDesired As Long, _
    : : ByVal phkResult As Long) As Long
    : :
    : : Sub Main()
    : : Dim lValue As Long ' Variable for value
    : : Dim sKey As String ' Key to open
    : : Dim hKey As Long ' Handle to registry key
    : :
    : : hKey = 0&
    : : sKey = "SOFTWAREMicrosoftWindowsCurrentVersionSharedDlls"
    : :
    : : lValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKey, 0&, KEY_READ, hKey)
    : : Console.WriteLine(lValue)
    : : End Sub
    : : End Module
    : : [/code]: :
    : :
    : RegOpenKeyEx() returns a handle to the registry key, which was
    : opened. You need to use RegGetValue() to get the actual value. See
    : the MSDN for more info on these functions

    Thanks. According to the documentation on MSDN, the handle to the registry key should be returned in hKey. The return value of the function (lValue in my code) is supposed to be 0 if the key was opened successfully.
  • : Thanks. According to the documentation on MSDN, the handle to the
    : registry key should be returned in hKey. The return value of the
    : function (lValue in my code) is supposed to be 0 if the key was
    : opened successfully.
    :

    That is true, but it is not your problem.

    You're using VB.NET and the code is for VB6.
    The datatype sizes have changed in the transition:
    [code]
    [b]Datatype VB6 VB.NET[/b]
    Integer 2 bytes 4 bytes
    Long 4 bytes 8 bytes
    [/code]

    So, now all the parameters for the API are 8 bytes, while they should be 4 bytes, so make them Integer.
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : : Thanks. According to the documentation on MSDN, the handle to the
    : : registry key should be returned in hKey. The return value of the
    : : function (lValue in my code) is supposed to be 0 if the key was
    : : opened successfully.
    : :
    :
    : That is true, but it is not your problem.
    :
    : You're using VB.NET and the code is for VB6.
    : The datatype sizes have changed in the transition:
    : [code]:
    : [b]Datatype VB6 VB.NET[/b]
    : Integer 2 bytes 4 bytes
    : Long 4 bytes 8 bytes
    : [/code]:
    :
    : So, now all the parameters for the API are 8 bytes, while they
    : should be 4 bytes, so make them Integer.
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    If I wanted to use this in a Windows Mobile 6 application, what would I need to change from the code posted above?

  • : : : Thanks. According to the documentation on MSDN, the handle to the
    : : : registry key should be returned in hKey. The return value of the
    : : : function (lValue in my code) is supposed to be 0 if the key was
    : : : opened successfully.
    : : :
    : :
    : : That is true, but it is not your problem.
    : :
    : : You're using VB.NET and the code is for VB6.
    : : The datatype sizes have changed in the transition:
    : : [code]: :
    : : [b]Datatype VB6 VB.NET[/b]
    : : Integer 2 bytes 4 bytes
    : : Long 4 bytes 8 bytes
    : : [/code]: :
    : :
    : : So, now all the parameters for the API are 8 bytes, while they
    : : should be 4 bytes, so make them Integer.
    : : Best Regards,
    : : Richard
    : :
    : : The way I see it... Well, it's all pretty blurry
    :
    : If I wanted to use this in a Windows Mobile 6 application, what
    : would I need to change from the code posted above?
    :
    :

    Not really sure to be honest. I know some API's are not available in Windows Mobile, and I don't know how much of the .NET framework is available.
    Have you tried the code above? Did it compile/run?
    If it ran but did not work, try capture the return codes from the API calls and look up their description.
    Also, MSDN Library is a good place to start when looking for what API's are available on Mobile.


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories