KEY.CPP
上传用户:aakk678
上传日期:2022-07-09
资源大小:406k
文件大小:2k
源码类别:

界面编程

开发平台:

Visual C++

  1. // key.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1997 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. LONG lRes = RegCloseKey(m_hKey);
  26. ASSERT(lRes == ERROR_SUCCESS);
  27. m_hKey = NULL;
  28. }
  29. }
  30. BOOL CKey::Create(HKEY hKey, LPCTSTR lpszKeyName)
  31. {
  32. ASSERT(hKey != NULL);
  33. return (RegCreateKey(hKey, lpszKeyName, &m_hKey) == ERROR_SUCCESS);
  34. }
  35. BOOL CKey::Open(HKEY hKey, LPCTSTR lpszKeyName)
  36. {
  37. ASSERT(hKey != NULL);
  38. return (RegOpenKey(hKey, lpszKeyName, &m_hKey) == ERROR_SUCCESS);
  39. }
  40. BOOL CKey::SetStringValue(LPCTSTR lpszValue, LPCTSTR lpszValueName)
  41. {
  42. ASSERT(m_hKey != NULL);
  43. return (RegSetValueEx(m_hKey, lpszValueName, NULL, REG_SZ, 
  44. (BYTE * const)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR)) == ERROR_SUCCESS);
  45. }
  46. BOOL CKey::GetStringValue(CString& str, LPCTSTR lpszValueName)
  47. {
  48. ASSERT(m_hKey != NULL);
  49. str.Empty();
  50. DWORD dw = 0;
  51. DWORD dwType = 0;
  52. LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType, 
  53. NULL, &dw);
  54. if (lRes == ERROR_SUCCESS)
  55. {
  56. ASSERT(dwType == REG_SZ);
  57. LPTSTR lpsz = str.GetBufferSetLength(dw);
  58. lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType, (BYTE*)lpsz, &dw);
  59. ASSERT(lRes == ERROR_SUCCESS);
  60. str.ReleaseBuffer();
  61. return TRUE;
  62. }
  63. return FALSE;
  64. }