Partager via


Checking the Registry for Policies and Preferences

The following code example shows how your application can check the registry for policy and preference information.

#define PREFERENCE_KEY TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer")
#define POLICY_KEY TEXT("Software\\Policies\\Microsoft\\Windows\\Explorer")

DWORD ReadValue (LPTSTR lpValueName, DWORD dwDefault)
{
     HKEY hKey;
     LONG lResult;
     DWORD dwValue, dwSize = sizeof(dwValue);

    // First, check for a policy.
    lResult = RegOpenKeyEx (HKEY_CURRENT_USER, POLICY_KEY, 0,
                            KEY_READ, &hKey);
    if (lResult == ERROR_SUCCESS)
    {
        lResult = RegQueryValueEx (hKey, lpValueName, 0, &dwType, 
                                   (LPBYTE) &dwValue, &dwSize);
        RegCloseKey (hKey);
    }

    // Exit if a policy value was found.
    if (lResult == ERROR_SUCCESS)
    {
        return dwValue;
    }              

    // Second, check for a preference.
    lResult = RegOpenKeyEx (HKEY_CURRENT_USER, PREFERENCE_KEY, 0,
                            KEY_READ, &hKey);
    if (lResult == ERROR_SUCCESS)
    {
        lResult = RegQueryValueEx (hKey, lpValueName, 0,
                                   &dwType, (LPBYTE) &dwValue, &dwSize);
                                 
        RegCloseKey (hKey);
    }
    
    // Exit if a preference was found.
    if (lResult == ERROR_SUCCESS)
    {
        return dwValue;
    }                                                     

    // Neither a policy nor a preference was found; return the default value.
    return dwDefault;
}