Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

byref error when API expects a pointer to a Byte array

Status
Not open for further replies.

OneTJN

Programmer
Dec 25, 2007
11
US
I am using the API RegQueryValueEx to determine what diectory a user installed my program in. The Variable 'strValue' gets a byref compile error

I have defined the variable strvalue as a string and as a byte array neither worked.

Here is the code:

LocationOfProgram = GetProgramPath(HKEY_LOcal_machine, "Software\Microsoft\Windows\CurrentVersion\App Paths")

Private Function GetProgramPath(ByVal RootKey As HKEYS, ByVal sKey As String) As String

Dim hKeyHandle As Long
Dim ReturnCode As Long
Dim strValue() As Byte
Dim lngDataLen As Long


ReturnCode = RegOpenKeyEx(RootKey, sKey, 0&, lngKEY_ALL_ACCESS, hKeyHandle)

If ReturnCode = ERROR_SUCCESS Then
ReDim strValue(2000)
lngDataLen = 2000
ReturnCode = RegQueryValueEx(hKeyHandle, "ghostexp.exe", "Path", 0&, REG_SZ, strValue, lngDataLen)
End If
Call RegCloseKey(hKeyHandle)

End Function
 
There are a few mistakes in the code.

1. The name of the EXE file (ghostexp.exe) is part of the key hierarchy, so it must be passed to RegOpenKeyEx function, not the RegQueryValueEx function.

2. RegQueryValueEx function is called incorrectly. Moreover, it expects 6 arguments, while you are passing 7.

I have rewritten the code below with a few modifications.
___
[tt]
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function ExpandEnvironmentStrings Lib "kernel32" Alias "ExpandEnvironmentStringsA" (ByVal lpSrc As String, ByVal lpDst As String, ByVal nSize As Long) As Long
Const ERROR_SUCCESS = 0
Const HKEY_LOCAL_MACHINE = &H80000002
Const KEY_QUERY_VALUE = &H1
Const REG_EXPAND_SZ = 2
Private Function GetProgramPath(Program As String) As String
Dim hKey As Long, retVal As Long, strValue As String * 300, lngDataLen As Long, lpType As Long
retVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\App Paths\" & Program, 0&, KEY_QUERY_VALUE, hKey)
If retVal = ERROR_SUCCESS Then
lngDataLen = Len(strValue)
retVal = RegQueryValueEx(hKey, "Path", 0, lpType, ByVal strValue, lngDataLen)
If retVal = ERROR_SUCCESS Then
If lpType = REG_EXPAND_SZ Then lngDataLen = ExpandEnvironmentStrings(strValue, strValue, Len(strValue))
GetProgramPath = Left$(strValue, lngDataLen - 1)
End If
RegCloseKey hKey
End If
End Function
Private Sub Form_Load()
MsgBox GetProgramPath("ghostexp.exe")
Unload Me
End Sub[/tt]
___

Some functions store the path in REG_EXPAND_SZ format, which contains environment strings, like %ProgramFiles% or %SystemRoot%. These variables need to be expanded using ExpandEnvironmentStrings function. An example is wab.exe (Windows Address Book) whose path is stored using environment variables.

Note that not all programs create a "Path" value to store their path. However, almost all of them write their full path including file name to the default value of the key. I suggest that instead of looking for "Path" value, you query this default value and then obtain the folder path. To do that, you need to call RegQueryValueEx function as below.

[tt]retVal = RegQueryValueEx(hKey, vbNullString, 0, lpType, ByVal strValue, lngDataLen)[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top