Click to See Complete Forum and Search --> : Retrieving a registry string


MikeSaunders
May 10th, 2001, 04:50 AM
In order to retrieve a Visual Basic string registry value, you have to first make ample space in a string with a function such as Space, String or something similar.
What I want to know is if there’s any way I can find out how much space I need to set without having to query the registry value first to determine the string size, setting the space and then requerying in order to retrieve the data.
As far as I can tell you can store up to 30,000 bytes in a registry string, class REG_SZ. But I most definitely don’t want to allocate nearly 30 KB of memory space for each registry string request.
Any advice?

coolbiz
May 10th, 2001, 08:30 AM
I assume that you're importing RegQueryValue() API to get the string value. Anyway, instead of re-allocating 30K of space everytime you need to get the data, just allocate 1 var as a global variable and re-use it whenever you need to call up the API function.

In a module or form

option Explicit

dim szRegValueBuffer as string * 30000

Function GetRegValue(szSubKey as string) as string
' assuming that all declaration has been done before
dim nBufSize as long
nBufSize = 30000

if (RegQueryValue(hKey, szSubKey, szRegValueBuffer, nBufSize) = 0) then
' got it
GetRegValue = Left$(szRegValueBuffer, nBufSize)
else
' error
GetRegValue = ""
end if
End Function




-Cool Bizs