RegQueryValueEx

This is a discussion on RegQueryValueEx within the Windows Programming forums, part of the Platform Specific Boards category; Hi all! I'm looking for the easiest way to read some registry key but i am really newbie. i search ...

  1. #1
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52

    RegQueryValueEx

    Hi all!
    I'm looking for the easiest way to read some registry key but i am really newbie.
    i search in msdn and inside this forum but everything i found is too complex and i couldn't understand. Someone could help me to call this windows api plz!? I just want to store some registry key value into a variable.

    thanks for any help

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    A couple of helper functions that I use:
    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;
    }
    Usage:
    Code:
    	HKEY hKey         = NULL;
    	LONG lResult;
    	LPTSTR szVal;
    	DWORD dwVal;
    
    	// Open the registry key...
    	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Company\\Program", 0, KEY_QUERY_VALUE, &hKey);
    
    	if (lResult != ERROR_SUCCESS) return;
    
    	// Get the registry values...
    	hr = RegGetString(hKey, TEXT("szVal"), &szVal);
    	if (FAILED(hr)) return;
    
    	hr = RegGetDWord(hKey,  TEXT("dwVal"), &dwVal);
    	if (FAILED(hr)) return;
    
    	// Print the results...
    	printf("The values: %s, %d", szVal, dwVal);

  3. #3
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    thank you! it helped a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RegQueryValueEx ERROR
    By koyboy in forum Windows Programming
    Replies: 11
    Last Post: 05-12-2008, 01:14 PM
  2. Help with RegQueryValueEx
    By centimel in forum C Programming
    Replies: 6
    Last Post: 01-08-2008, 06:38 PM
  3. RegQueryValueEx Doesn't return data
    By willc0de4food in forum Windows Programming
    Replies: 8
    Last Post: 10-22-2007, 09:41 AM
  4. RegQueryValueEx problem
    By BobS0327 in forum Windows Programming
    Replies: 4
    Last Post: 10-08-2006, 08:11 PM
  5. RegQueryValueEx Giving error 234
    By Narcose in forum C Programming
    Replies: 4
    Last Post: 07-14-2006, 10:16 AM

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