1

Firstly, this is a legacy project, very old, I have the short straw of bringing it up to date.

I noticed that one of the errors is that the application relies on registry structure already existing. I want to test the structure is present and if not create any missing keys.

There is a existing class called CRegKey, the Open method:

    inline LONG CRegKey::Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired) throw() {
        ATLASSUME(hKeyParent != NULL);
        HKEY hKey = NULL;
        LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyName, 0, samDesired, &hKey);
        if (lRes == ERROR_SUCCESS) {
            lRes = Close();
            ATLASSERT(lRes == ERROR_SUCCESS);
            m_hKey = hKey;
    #if WINVER >= 0x0501
            m_samWOW64 = samDesired & (KEY_WOW64_32KEY | KEY_WOW64_64KEY);
    #endif
        }
        return lRes;
    }

The problem is that I've found then when calling this routine, if the key doesn't exist it returns ERROR_SUCCESS, a typical example:

    DWORD dwRes = rKey.Open(HKEY_LOCAL_MACHINE, szPath, KEY_READ);

In the above example szPath has a value of:

    SOFTWARE\Name\Scada\LonAgent

I've exported the original registry and for test purposes have deleted the 'LonAgent' key.

The problem is that although this key doesn't exist, dwRes is still 0 which is the same as ERROR_SUCCESS....why?

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • Try this https://stackoverflow.com/questions/1343577/checking-if-a-registry-key-exists – Ckiller Aug 06 '18 at 17:30
  • 1
    Could the WOW64 redirector be affecting this? `HKEY_LOCAL_MACHINE\Software` is redirected, see: https://learn.microsoft.com/en-us/windows/desktop/winprog64/registry-redirector So 64-bit regedit and a 32-bit application will see different views of the registry. – Richard Critten Aug 06 '18 at 17:42
  • @Ckiller, I saw that before I asked the question, problem is if the key exists or not the return is still 0 (ERROR_SUCCESS) which I did state in the question. – SPlatten Aug 06 '18 at 20:45
  • @RichardCritten, I don't think so. – SPlatten Aug 06 '18 at 20:46
  • 1
    @SPlatten easy to check: is everything 64-bit or everything 32-bit - tools, app, tests. If not 64-bit programs see one view of the registry and 32-bit programs see another. Remember that on 64-bit Windows 32-bit programs effectively run in an emulated environment. – Richard Critten Aug 06 '18 at 20:51
  • @RichardCritten, I'll investigate tomorrow, I'm don't have access to the system tonight. – SPlatten Aug 06 '18 at 20:53
  • @RichardCritten, you were right! – SPlatten Aug 07 '18 at 05:27

1 Answers1

1

The result clearly indicates that the registry key exists. For 32-bit programs, registry keys are redirected as follows:

HKEY_LOCAL_MACHINE\SOFTWARE\Name\Scada\LonAgent is redicrect to:
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Name\Scada\LonAgent

Use KEY_WOW64_64KEY to stop redirection

DWORD dwRes = rKey.Open(HKEY_LOCAL_MACHINE, szPath, KEY_WOW64_64KEY | KEY_READ);

But that's usually not for 32-bit programs. Instead use KEY_WOW64_32KEY to force redirection, or just use KEY_READ

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • Thank you, why does this even exist? What's the point of another registry area? – SPlatten Aug 07 '18 at 05:27
  • 32-bit and 64-bit programs are not compatible, Windows keeps them separate. For example a 64-bit program checks the registry for a dll, Windows will direct it to a 64-bit dll. A 32-bit program checks the same registry and gets a 32-bit dll. So the 32-bit program can run as if it's on a 32-bit system. There is probably more it though. More info: [Running 32-bit Applications](https://learn.microsoft.com/en-us/windows/desktop/winprog64/running-32-bit-applications) – Barmak Shemirani Aug 07 '18 at 06:51