title: Reading and Writing registry keys. C++ MFC MBCS.
body: <p>I would like to detect, and if possible read to a CString, a registry key starting with "HKEY_LOCAL_MACHINE\SOFTWARE\blah\SetupPath".</p>
  
  <p>I see the MSDN on RegOpenKeyEx function</p>
  
  <pre><code>LONG WINAPI RegOpenKeyEx(
    _In_        HKEY hKey,
    _In_opt_    LPCTSTR lpSubKey,
    _Reserved_  DWORD ulOptions,
    _In_        REGSAM samDesired,
    _Out_       PHKEY phkResult
  );
  </code></pre>
  
  <p>So for this it looks like I need to setup a few things.</p>
  
  <pre><code>HKEY hKey = HKEY_LOCAL_MACHINE;
  LPCTSTR lpSubKey = "SOFTWARE\blah\SetupPath";
  </code></pre>
  
  <p>And to see if the key exists just do</p>
  
  <pre><code>LONG res = RegOpenKeyEx(hKey, lpSubKey, 0, 0, 0);
  if(res == ERROR_SUCCESS)
      // The key exists
  </code></pre>
  
  <p>Now if the key exists I want to read what is there, into a CString. I also see the RegQueryValueEx</p>
  
  <pre><code>LONG WINAPI RegQueryValueEx(
    _In_         HKEY hKey,
    _In_opt_     LPCTSTR lpValueName,
    _Reserved_   LPDWORD lpReserved,
    _Out_opt_    LPDWORD lpType,
    _Out_opt_    LPBYTE lpData,
    _Inout_opt_  LPDWORD lpcbData
  );
  </code></pre>
  
  <p>It also looks like I need some setup before I call this function too.</p>
  
  <pre><code>HKEY hKey = HKEY_LOCAL_MACHINE;
  lpSubKey = "SOFTWARE\blah\SetupPath";
  LPDWORD type = null;
  LPDWORD data = null;
  </code></pre>
  
  <p>Now I can call it</p>
  
  <pre><code>LONG res2 = RegValueQueryEX(hKey, lpSubKey, 0, type, data,0);
  </code></pre>
  
  <p>Then I think I can check to see the type and then cast to a string?</p>
  
  <pre><code>CString regVal;
  if(res2 == ERROR_SUCCESS)
     if(type == REG_SZ)
        if(data != null)
            regVal = new CString((LPSTR)data);
  </code></pre>
  
  <p>Is this all correct? What might I be missing or need to do?</p>