Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB

  1. // key.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 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. #include "stdafx.h"
  13. #include "key.h"
  14. #include <winreg.h>
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char BASED_CODE THIS_FILE[] = __FILE__;
  18. #endif
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CKey
  21. void CKey::Close()
  22. {
  23. if (m_hKey != NULL)
  24. {
  25. #if defined(_DEBUG)
  26. LONG lRes = RegCloseKey(m_hKey);
  27. ASSERT(lRes == ERROR_SUCCESS);
  28. #else
  29. RegCloseKey(m_hKey);
  30. #endif
  31. m_hKey = NULL;
  32. }
  33. }
  34. BOOL CKey::Create(HKEY hKey, LPCTSTR lpszKeyName, REGSAM samDesired)
  35. {
  36. ASSERT(hKey != NULL);
  37. return (RegCreateKeyEx(hKey, lpszKeyName, 0, NULL, 0, samDesired, NULL, &m_hKey, NULL) == ERROR_SUCCESS);
  38. }
  39. BOOL CKey::Open(HKEY hKey, LPCTSTR lpszKeyName, REGSAM samDesired)
  40. {
  41. ASSERT(hKey != NULL);
  42. return (RegOpenKeyEx(hKey, lpszKeyName, 0, samDesired, &m_hKey) == ERROR_SUCCESS);
  43. }
  44. BOOL CKey::SetStringValue(LPCTSTR lpszValue, LPCTSTR lpszValueName)
  45. {
  46. ASSERT(m_hKey != NULL);
  47. return (RegSetValueEx(m_hKey, lpszValueName, NULL, REG_SZ,
  48. (BYTE * const)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR)) == ERROR_SUCCESS);
  49. }
  50. BOOL CKey::GetStringValue(CString& str, LPCTSTR lpszValueName)
  51. {
  52. ASSERT(m_hKey != NULL);
  53. str.Empty();
  54. DWORD dw = 0;
  55. DWORD dwType = 0;
  56. LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
  57. NULL, &dw);
  58. if (lRes == ERROR_SUCCESS)
  59. {
  60. ASSERT(dwType == REG_SZ);
  61. LPTSTR lpsz = str.GetBufferSetLength(dw);
  62. lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType, (BYTE*)lpsz, &dw);
  63. ASSERT(lRes == ERROR_SUCCESS);
  64. str.ReleaseBuffer();
  65. return TRUE;
  66. }
  67. return FALSE;
  68. }