Click here to Skip to main content
15,892,537 members
Articles / Programming Languages / C++

Registry Redirector in x64 / IA64

Rate me:
Please Sign up or sign in to vote.
4.83/5 (21 votes)
11 Jul 200711 min read 141.8K   770   53  
This article gives you a deeper view into the registry redirector on x64 / IA64 systems
// ReflectionTest.cpp : Defines the entry point for the console application.
//

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <string>

#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif

typedef LONG (WINAPI *PRegQueryReflectionKey) (
  IN HKEY hKey,
  OUT LPBOOL pbIsReflectionDisabled
);
PRegQueryReflectionKey pRegQueryReflectionKey = NULL;

typedef LONG (WINAPI* PRegDisableReflectionKey) (
  IN HKEY hBase
);
PRegDisableReflectionKey pRegDisableReflectionKey = NULL;

typedef LONG (WINAPI* PRegEnableReflectionKey) (
  IN HKEY hBase
);
PRegEnableReflectionKey pRegEnableReflectionKey = NULL;

typedef LONG (WINAPI* PRegQueryKeyFlags) (
  IN HKEY hKey,
  IN DWORD dwAttribMask,
  OUT PDWORD pdwAttribute
);
PRegQueryKeyFlags pRegQueryKeyFlags = NULL;

#define MY_KEY_FLAG_DISABLE_REDIRECTION 0x00000010L
#define MY_KEY_FLAG_EXEMPT_REFLECTION 0x00000004L
#define MY_KEY_FLAG_OWNERSHIP_REFLECTION 0x00000008L


void DisplayError(LONG lErr)
{
  TCHAR szBuf[1024];
  DWORD dw = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, lErr, 0, szBuf, 1024, NULL);
  _tprintf(szBuf);
}


bool IsReflected(HKEY hKey, LPCTSTR szNodeName)
{
  if (pRegQueryReflectionKey != NULL)
  {
    BOOL bReflectedDisabled = TRUE;
    LONG lResult = pRegQueryReflectionKey(hKey, &bReflectedDisabled);
    if (lResult == ERROR_SUCCESS)
    {
      if (bReflectedDisabled != FALSE)
      {
        _tprintf(_T("DISABLED: %s\n"), szNodeName);
        return true;
      }
    }
    else
    {
      // An error occured:
      _tprintf(_T("Error (RegQueryReflectionKey): 0x%8.8x (%s): "), lResult, szNodeName);
      DisplayError(lResult);
      _tprintf(_T("\n"));
    }
  }
  return false;
}

bool ShowKeyFlags(HKEY hKey, LPCTSTR szNodeName)
{
  if (pRegQueryKeyFlags != NULL)
  {
    DWORD dwFlags;
    LONG lResult = pRegQueryKeyFlags(hKey,
      MY_KEY_FLAG_DISABLE_REDIRECTION |
      MY_KEY_FLAG_EXEMPT_REFLECTION |
      MY_KEY_FLAG_OWNERSHIP_REFLECTION,
      &dwFlags);
    if (lResult == ERROR_SUCCESS)
    {
      if (dwFlags != 0)
      {
        if (dwFlags & MY_KEY_FLAG_DISABLE_REDIRECTION)
          _tprintf(_T("FLAG_DISABLE_REDIRECTION: %s\n"), szNodeName);
        if (dwFlags & MY_KEY_FLAG_EXEMPT_REFLECTION)
          _tprintf(_T("FLAG_EXEMPT_REFLECTION: %s\n"), szNodeName);
        if (dwFlags & MY_KEY_FLAG_OWNERSHIP_REFLECTION)
          _tprintf(_T("FLAG_OWNERSHIP_REFLECTION: %s\n"), szNodeName);
        return true;
      }
    }
    else
    {
      // An error occured:
      _tprintf(_T("Error (RegQueryKeyFlags): 0x%8.8x (%s): "), lResult, szNodeName);
      DisplayError(lResult);
      _tprintf(_T("\n"));
    }
  }
  return false;
}

void RecurseKeys(HKEY hKey, DWORD level, std::tstring &node)
{
  DWORD dwIdx = 0;
  TCHAR szKeyName[1024];
  DWORD dwSize = 1024;
  FILETIME fTime;

  IsReflected(hKey, node.c_str());
  ShowKeyFlags(hKey, node.c_str());

  while(RegEnumKeyEx(hKey, dwIdx, szKeyName, &dwSize, NULL, NULL, NULL, &fTime) == ERROR_SUCCESS)
  {
    std::tstring node2(node);
    if (node2.size() > 0)
      node2 += _T("\\");
    node2 += szKeyName;

    dwSize = 1024;
    dwIdx++;

    HKEY hSubKey;
    if (RegOpenKeyEx(hKey, szKeyName, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
    {
      RecurseKeys(hSubKey, level + 1, node2);
      RegCloseKey(hSubKey);
    }
  }
}

int _tmain(int argc, _TCHAR* argv[])
{
  HMODULE hMod = LoadLibrary(_T("Advapi32.dll"));
  pRegQueryReflectionKey = (PRegQueryReflectionKey) GetProcAddress(hMod, "RegQueryReflectionKey");
  pRegDisableReflectionKey = (PRegDisableReflectionKey) GetProcAddress(hMod, "RegDisableReflectionKey");
  pRegEnableReflectionKey = (PRegEnableReflectionKey) GetProcAddress(hMod, "RegEnableReflectionKey");
  pRegQueryKeyFlags = (PRegQueryKeyFlags) GetProcAddress(hMod, "RegQueryKeyFlags");

  // display all keys
  std::tstring nodeName;
  _tprintf(_T("** HKEY_LOCAL_MACHINE **\n"));
  RecurseKeys(HKEY_LOCAL_MACHINE, 0, nodeName);

  _tprintf(_T("** HKEY_CURRENT_USER **\n"));
  nodeName.empty();
  RecurseKeys(HKEY_CURRENT_USER, 0, nodeName);

  return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Germany Germany
1982: My first computer (VC20)
1984: Finished to build my first own computer (Z80)
1993: Mission-Volunteer in Papua New Guinea
1998: Dipl. Inform. (FH)
... working, working, working....

Comments and Discussions