Need RegQuery help

This is a discussion on Need RegQuery help within the Windows Programming forums, part of the Platform Specific Boards category; hey, i'm trying to check if a registry key exists and i open the key with this code: Code: /* ...

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    Need RegQuery help

    hey, i'm trying to check if a registry key exists and i open the key with this code:
    Code:
    /* variables */
    TCHAR szBuf[20] = "Software\\ShibbyInc", value[9] = "Password";
    LPDWORD type;
    LONG regresult;
    HKEY hkey;
    
    if (RegOpenKeyEx(HKEY_CURRENT_USER, szBuf, 0, KEY_READ, &hkey)) 
    {   return FALSE;   }
    and then i need to query it (i believe) because i need to check if a key exists which the program creates depending on the user, and then if the key is there i need the value stored. so i've tried:
    Code:
    regresult = RegQueryValueEx(hkey, value, NULL, type, NULL, NULL);
    if (regresult == ERROR_SUCCESS) MessageBox(hwnd, "it exists.", "ok", MB_OK);
    
    and
    
    if (RegQueryValueEx(hkey, value, NULL, type, NULL, NULL))
    MessageBox(hwnd, "it exists.", "ok", MB_OK);
    but with either query, i get the Windows message saying i violated memory what am i doing wrong?
    thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    When a function takes a pointer, it expects a value that is either NULL (when allowed by the documentation) or a value that points somewhere valid. It never expects a random value.
    Code:
    // type is not initialized and therefore has a "random" value.
    LPDWORD type; 
    
    // Whoops, crash when function tries to write to random memory address
    regresult = RegQueryValueEx(hkey, value, NULL, type, NULL, NULL);
    Either of these are valid:
    Code:
    regresult = RegQueryValueEx(hkey, value, NULL, NULL, NULL, NULL);
    or:
    Code:
    DWORD type;
    // Pass a valid pointer to a DWORD variable...
    regresult = RegQueryValueEx(hkey, value, NULL, &type, NULL, NULL);

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    ahh, things are much more clear now thank you.
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Code:
    LONG RegQueryValueEx(
      HKEY hKey,
      LPCTSTR lpValueName,
      LPDWORD lpReserved,
      LPDWORD lpType,
      LPBYTE lpData,
      LPDWORD lpcbData
    );
    what datatypes are useable / required for the second to last variable (LPBYTE lpData) ? should i use a typecase operator with like, a TCHAR or something?
    thanks
    Last edited by willc0de4food; 07-08-2005 at 12:02 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    It depends on the type of the registry value that you are requesting. If it's a DWORD value, lpData should be a pointer to a DWORD. If it's a string, lpData should specify a TCHAR array. If it's binary, lpData should specify a BYTE array. You will need to use a cast for this argument. Here are a couple of functions that demonstrate.
    Code:
    // =====================================================================================
    HRESULT RegGetString(HKEY hKey, LPCTSTR szValueName, LPTSTR * lpszResult) {
    
    	// Given a HKEY and value name returns a string from the registry.
    	// Upon successful return the string should be freed using free()
    	// eg. RegGetString(hKey, TEXT("my value"), &szString);
    
    	DWORD dwType=0, dwDataSize=0, dwBufSize=0;
    	LONG lResult;
    
    	// Incase we fail set the return string to null...
    	if (lpszResult != NULL) *lpszResult = NULL;
    
    	// Check input parameters...
    	if (hKey == NULL || lpszResult == NULL) return E_INVALIDARG;
    
    	// Get the length of the string in bytes (placed in dwDataSize)...
    	lResult = RegQueryValueEx(hKey, szValueName, 0, &dwType, NULL, &dwDataSize );
    
    	// Check result and make sure the registry value is a string(REG_SZ)...
    	if (lResult != ERROR_SUCCESS) return HRESULT_FROM_WIN32(lResult);
    	else if (dwType != REG_SZ)    return DISP_E_TYPEMISMATCH;
    
    	// Allocate memory for string - We add space for a null terminating character...
    	dwBufSize = dwDataSize + (1 * sizeof(TCHAR));
    	*lpszResult = malloc(dwBufSize);
    
    	if (*lpszResult == NULL) return E_OUTOFMEMORY;
    
    	// Now get the actual string from the registry...
    	lResult = RegQueryValueEx(hKey, szValueName, 0, &dwType, (LPBYTE) *lpszResult, &dwDataSize );
    
    	// Check result and type again.
    	// If we fail here we must free the memory we allocated...
    	if (lResult != ERROR_SUCCESS) { free(*lpszResult); return HRESULT_FROM_WIN32(lResult); }
    	else if (dwType != REG_SZ)    { free(*lpszResult); return DISP_E_TYPEMISMATCH; }
    
    	// We are not guaranteed a null terminated string from RegQueryValueEx.
    	// Explicitly null terminate the returned string...
    	(*lpszResult)[(dwBufSize / sizeof(TCHAR)) - 1] = TEXT('\0');
    
    	return NOERROR;
    }
    
    
    // =====================================================================================
    HRESULT RegGetDWord(HKEY hKey, LPCTSTR szValueName, DWORD * lpdwResult) {
    
    	// Given a value name and an hKey returns a DWORD from the registry.
    	// eg. RegGetDWord(hKey, TEXT("my dword"), &dwMyValue);
    
    	LONG lResult;
    	DWORD dwDataSize = sizeof(DWORD);
    	DWORD dwType = 0;
    
    	// Check input parameters...
    	if (hKey == NULL || lpdwResult == NULL) return E_INVALIDARG;
    
    	// Get dword value from the registry...
    	lResult = RegQueryValueEx(hKey, szValueName, 0, &dwType, (LPBYTE) lpdwResult, &dwDataSize );
    
    	// Check result and make sure the registry value is a DWORD(REG_DWORD)...
    	if (lResult != ERROR_SUCCESS) return HRESULT_FROM_WIN32(lResult);
    	else if (dwType != REG_DWORD) return DISP_E_TYPEMISMATCH;
    
    	return NOERROR;
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21