RegQueryValueEx reading fails

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="margin:0in 0in 0pt <span style="font-size:10pt <span style="font-family:Calibri RegQueryValueEx function fails in this program
<p style="margin:0in 0in 0pt <span style="font-size:10pt <span style="font-family:Calibri Pls some1 help.
<p style="margin:0in 0in 0pt <span style="font-size:10pt <span style="font-family:Calibri
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; color:blue; font-size:9.5pt Module<span style="font-family:Consolas; font-size:9.5pt
<span style="color:#2b91af Module1
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Public <span style="color:blue Const HKEY_LOCAL_MACHINE = &H80000002
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Public <span style="color:blue Const KEY_ALL_ACCESS = &HF003F
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Public <span style="color:blue Const REG_OPTION_NON_VOLATILE = 0<span style="
<span style="color:green Key is preserved when system is rebooted
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim hregkey <span style="color:blue As
<span style="color:blue Integer <span style="color:green receives handle to the newly created or opened registry key
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim subkey <span style="color:blue As
<span style="color:blue String <span style="color:green name of the subkey to create
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim neworused <span style="color:blue As
<span style="color:blue Integer <span style="color:green receives 1 if new key was created or 2 if an existing key was opened
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim stringbuffer <span style="color:blue
As <span style="color:blue String <span style="color:green the string to put into the registry
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim retval <span style="color:blue As
<span style="color:blue Integer <span style="color:green return value
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim cRegistrySubKey <span style="color:blue
As <span style="color:blue String
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Private <span style="color:blue Declare
<span style="color:blue Function RegCloseKey <span style="color:blue Lib
<span style="color:#a31515 "advapi32.dll" (<span style="color:blue ByVal hKey
<span style="color:blue As <span style="color:blue Integer) <span style="color:blue
As <span style="color:blue Integer
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Declare <span style="color:blue Function RegOpenKeyEx
<span style="color:blue Lib <span style="color:#a31515 "advapi32.dll"
<span style="color:blue Alias _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:#a31515 "RegOpenKeyExA" (<span style="color:blue ByVal hKey
<span style="color:blue As <span style="color:blue Integer, _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue ByVal lpSubKey <span style="color:blue As
<span style="color:blue String, _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue ByVal ulOptions <span style="color:blue As
<span style="color:blue Integer, _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue ByVal samDesired <span style="color:blue
As <span style="color:blue Integer, _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue ByRef phkResult <span style="color:blue As
<span style="color:blue Integer) <span style="color:blue As <span style="color:blue
Integer
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Declare <span style="color:blue Function RegQueryValueEx
<span style="color:blue Lib <span style="color:#a31515 "advapi32.dll"
<span style="color:blue Alias _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:#a31515 "RegQueryValueExA" (<span style="color:blue ByVal hKey
<span style="color:blue As <span style="color:blue Integer, _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue ByVal lpValueName <span style="color:blue
As <span style="color:blue String, _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue ByVal lpReserved <span style="color:blue
As <span style="color:blue Integer, _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue ByRef lpType <span style="color:blue As
<span style="color:blue Integer, _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue ByRef lpData <span style="color:blue As
<span style="color:blue String, _
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue ByRef lpcbData <span style="color:blue As
<span style="color:blue Integer) <span style="color:blue As <span style="color:blue
Integer
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Sub Main()
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim lcReturn <span style="color:blue As
<span style="color:blue String
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim lcOptName <span style="color:blue As
<span style="color:blue String <span style="color:green Name of the option
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim lcOptValue <span style="color:blue As
<span style="color:blue String <span style="color:green Value of the option
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
subkey = <span style="color:#a31515 "SoftwareTestSubkey"
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
lcOptName = <span style="color:#a31515 "OptName"
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
lcOptValue = <span style="color:#a31515 "OptValue"
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:green Open the sub key
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
retval = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_ALL_ACCESS, hregkey)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue If retval = ERROR_SUCCESS <span style="color:blue
Then
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
lcReturn = <span style="color:#a31515 "Success"
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Else
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
lcReturn = <span style="color:#a31515 "Failed " & ErrProc(retval)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue End <span style="color:blue If
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:#2b91af Console.WriteLine(<span style="color:#a31515 "Return of RegOpenKeyEx " & lcReturn)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:green Read the value from the option
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim lnOptType <span style="color:blue As
<span style="color:blue Integer
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style=" <span style="color:blue Dim lcRetOptValue
<span style="color:blue As <span style="color:blue String
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim lnRetOptValue <span style="color:blue
As <span style="color:blue Integer
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
lcRetOptValue = Space(256)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
lnRetOptValue = Len(lcRetOptValue)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
retval = RegQueryValueEx(hregkey, lcOptName, 0, lnOptType, lcRetOptValue, lnRetOptValue)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue If retval = ERROR_SUCCESS <span style="color:blue
Then
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
lcReturn = <span style="color:#a31515 "Success"
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Else
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
lcReturn = <span style="color:#a31515 "Failed " & ErrProc(retval)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue End <span style="color:blue If
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:#2b91af Console.WriteLine(<span style="color:#a31515 "Return of RegQueryValueEx " & lcReturn)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:#2b91af Console.ReadLine()
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue End <span style="color:blue Sub
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; color:blue; font-size:9.5pt End<span style="font-family:Consolas; font-size:9.5pt
<span style="color:blue Module

View the full article
 
Back
Top