home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / wordpad / key.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  2KB  |  73 lines

  1. // key.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "key.h"
  15. #include <winreg.h>
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CKey
  24.  
  25. void CKey::Close()
  26. {
  27.     if (m_hKey != NULL)
  28.     {
  29.         LONG lRes = RegCloseKey(m_hKey);
  30.         ASSERT(lRes == ERROR_SUCCESS);
  31.         m_hKey = NULL;
  32.     }
  33. }
  34.  
  35. BOOL CKey::Create(HKEY hKey, LPCTSTR lpszKeyName)
  36. {
  37.     ASSERT(hKey != NULL);
  38.     return (RegCreateKey(hKey, lpszKeyName, &m_hKey) == ERROR_SUCCESS);
  39. }
  40.  
  41. BOOL CKey::Open(HKEY hKey, LPCTSTR lpszKeyName)
  42. {
  43.     ASSERT(hKey != NULL);
  44.     return (RegOpenKey(hKey, lpszKeyName, &m_hKey) == ERROR_SUCCESS);
  45. }
  46.  
  47. BOOL CKey::SetStringValue(LPCTSTR lpszValue, LPCTSTR lpszValueName)
  48. {
  49.     ASSERT(m_hKey != NULL);
  50.     return (RegSetValueEx(m_hKey, lpszValueName, NULL, REG_SZ,
  51.         (BYTE * const)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR)) == ERROR_SUCCESS);
  52. }
  53.  
  54. BOOL CKey::GetStringValue(CString& str, LPCTSTR lpszValueName)
  55. {
  56.     ASSERT(m_hKey != NULL);
  57.     str.Empty();
  58.     DWORD dw = 0;
  59.     DWORD dwType = 0;
  60.     LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
  61.         NULL, &dw);
  62.     if (lRes == ERROR_SUCCESS)
  63.     {
  64.         ASSERT(dwType == REG_SZ);
  65.         LPTSTR lpsz = str.GetBufferSetLength(dw);
  66.         lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType, (BYTE*)lpsz, &dw);
  67.         ASSERT(lRes == ERROR_SUCCESS);
  68.         str.ReleaseBuffer();
  69.         return TRUE;
  70.     }
  71.     return FALSE;
  72. }
  73.