Results 1 to 4 of 4

Thread: Out of Memory

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Out of Memory

    I'm pulling registry values using AdvApi.dll, and I'm getting an error for several that are REG_DWORDS that give me the error for out of memory. Below is the section of code where it shows up.

    VB Code:
    1. ' Determine the size and type of data to be read
    2. lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
    3.        
    4. If lrc <> ERROR_NONE Then Error 5

    lrc is being returned as 6 which is the error code for ERROR_OUTOFMEMORY. Do I have a variable that is the wrong type?

    I've probably done a poor job of explaining this, let me know if I can clarify it better.

    edit:
    I should probably tell what values I'm expecting back from the registry:
    0x00000000
    0x00000022
    Last edited by bat711; Mar 17th, 2005 at 04:00 PM.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Out of Memory

    Does this eaxmple from the API guide help?

    VB Code:
    1. 'This program needs 3 buttons
    2. Const REG_SZ = 1 ' Unicode nul terminated string
    3. Const REG_BINARY = 3 ' Free form binary
    4. Const HKEY_CURRENT_USER = &H80000001
    5. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    6. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    7. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    8. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    9. Private Declare Function RegQueryValueEx Lib "advapi32.dll" 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
    10. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    11. Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
    12.     Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
    13.     'retrieve nformation about the key
    14.     lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
    15.     If lResult = 0 Then
    16.         If lValueType = REG_SZ Then
    17.             'Create a buffer
    18.             strBuf = String(lDataBufSize, Chr$(0))
    19.             'retrieve the key's content
    20.             lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
    21.             If lResult = 0 Then
    22.                 'Remove the unnecessary chr$(0)'s
    23.                 RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
    24.             End If
    25.         ElseIf lValueType = REG_BINARY Then
    26.             Dim strData As Integer
    27.             'retrieve the key's value
    28.             lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
    29.             If lResult = 0 Then
    30.                 RegQueryStringValue = strData
    31.             End If
    32.         End If
    33.     End If
    34. End Function
    35. Function GetString(hKey As Long, strPath As String, strValue As String)
    36.     Dim Ret
    37.     'Open the key
    38.     RegOpenKey hKey, strPath, Ret
    39.     'Get the key's content
    40.     GetString = RegQueryStringValue(Ret, strValue)
    41.     'Close the key
    42.     RegCloseKey Ret
    43. End Function
    44. Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
    45.     Dim Ret
    46.     'Create a new key
    47.     RegCreateKey hKey, strPath, Ret
    48.     'Save a string to the key
    49.     RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
    50.     'close the key
    51.     RegCloseKey Ret
    52. End Sub
    53. Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
    54.     Dim Ret
    55.     'Create a new key
    56.     RegCreateKey hKey, strPath, Ret
    57.     'Set the key's value
    58.     RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
    59.     'close the key
    60.     RegCloseKey Ret
    61. End Sub
    62. Sub DelSetting(hKey As Long, strPath As String, strValue As String)
    63.     Dim Ret
    64.     'Create a new key
    65.     RegCreateKey hKey, strPath, Ret
    66.     'Delete the key's value
    67.     RegDeleteValue Ret, strValue
    68.     'close the key
    69.     RegCloseKey Ret
    70. End Sub
    71. Private Sub Command1_Click()
    72.     Dim strString As String
    73.     'Ask for a value
    74.     strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
    75.     If strString = "" Or Val(strString) > 255 Or Val(strString) < 0 Then
    76.         MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
    77.         Exit Sub
    78.     End If
    79.     'Save the value to the registry
    80.     SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
    81. End Sub
    82. Private Sub Command2_Click()
    83.     'Get a string from the registry
    84.     Ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
    85.     If Ret = "" Then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
    86.     MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
    87. End Sub
    88. Private Sub Command3_Click()
    89.     'Delete the setting from the registry
    90.     DelSetting HKEY_CURRENT_USER, "KPD-Team", "BinaryValue"
    91.     MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
    92. End Sub
    93. Private Sub Form_Load()
    94.     'KPD-Team 1998
    95.     'URL: [url]http://www.allapi.net/[/url]
    96.     'E-Mail: [email][email protected][/email]
    97.     Command1.Caption = "Set Value"
    98.     Command2.Caption = "Get Value"
    99.     Command3.Caption = "Delete Value"
    100. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Re: Out of Memory

    Yeah a little, I'm working on someone else's undocumented non-functioning source code that they are no longer allowed to work on so I've been having trouble following what everything from the API does. That and the fact they crippled the source code to make it difficult to modify

    Thanks for the suggestion though.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Out of Memory

    While your API is not exactly the same, here is the parameter info for the original API.

    · hKey
    Identifies a currently open key or any of the following predefined reserved handle values:
    HKEY_CLASSES_ROOT
    HKEY_CURRENT_USER
    HKEY_LOCAL_MACHINE
    HKEY_USERS

    · lpValueName
    Points to a null-terminated string containing the name of the value to be queried.

    · lpReserved
    Reserved; must be NULL.

    · lpType
    Points to a variable that receives the key’s value type. The value returned through this parameter will be one of the following:
    REG_BINARY
    Binary data in any form.
    REG_DWORD
    A 32-bit number.
    REG_DWORD_LITTLE_ENDIAN
    A 32-bit number in little-endian format (same as REG_DWORD). In little-endian format, the most significant byte of a word is the high-order byte. This is the most common format for computers running Windows NT and Windows 95.
    REG_DWORD_BIG_ENDIAN
    A 32-bit number in big-endian format. In big-endian format, the most significant byte of a word is the low-order byte.
    REG_EXPAND_SZ
    A null-terminated string that contains unexpanded references to environment variables (for example, “%PATH%”). It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions.
    REG_LINK
    A Unicode symbolic link.
    REG_MULTI_SZ
    An array of null-terminated strings, terminated by two null characters.
    REG_NONE
    No defined value type.
    REG_RESOURCE_LIST
    A device-driver resource list.
    REG_SZ
    A null-terminated string. It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions.

    The lpType parameter can be NULL if the type is not required.

    · lpData
    Points to a buffer that receives the value’s data. This parameter can be NULL if the data is not required.

    · lpcbData
    Points to a variable that specifies the size, in bytes, of the buffer pointed to by the lpData parameter. When the function returns, this variable contains the size of the data copied to lpData.
    If the buffer specified by lpData parameter is not large enough to hold the data, the function returns the value ERROR_MORE_DATA, and stores the required buffer size, in bytes, into the variable pointed to by lpcbData.
    If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS, and stores the size of the data, in bytes, in the variable pointed to by lpcbData. This lets an application determine the best way to allocate a buffer for the value key’s data.
    If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, then lpData will also include the size of the terminating null character.
    The lpcbData parameter can be NULL only if lpData is NULL.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width