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.

198 lines
5.6 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // Registry.cpp: Registry �N���X�̃C���v�������e�[�V����
  4. //
  5. // 1998 Jun, Hiro Yamamoto
  6. //
  7. //////////////////////////////////////////////////////////////////////
  8. #include "stdafx.h"
  9. #include "custcon.h"
  10. #include "Registry.h"
  11. #include "KeyDef.h"
  12. #ifdef _DEBUG
  13. #undef THIS_FILE
  14. static char THIS_FILE[]=__FILE__;
  15. #define new DEBUG_NEW
  16. #endif
  17. //////////////////////////////////////////////////////////////////////
  18. // �\�z/����
  19. //////////////////////////////////////////////////////////////////////
  20. CConRegistry::CConRegistry()
  21. {
  22. if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Console"), 0, KEY_ALL_ACCESS, &m_hkey) != ERROR_SUCCESS ||
  23. RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Command Processor"), 0, KEY_ALL_ACCESS, &m_cmdKey) != ERROR_SUCCESS) {
  24. AfxMessageBox(IDP_FAILED_TO_OPEN_REGISTRY);
  25. AfxAbort(); // exodus
  26. }
  27. }
  28. CConRegistry::~CConRegistry()
  29. {
  30. ASSERT(m_hkey);
  31. VERIFY( RegCloseKey(m_hkey) == ERROR_SUCCESS );
  32. VERIFY( RegCloseKey(m_cmdKey) == ERROR_SUCCESS );
  33. }
  34. const CString CConRegistry::m_err(_T("\xffff"));
  35. //////////////////////////////////////////////////////////////////////
  36. // Members
  37. //////////////////////////////////////////////////////////////////////
  38. CString CConRegistry::ReadString(LPCTSTR subkey)
  39. {
  40. ASSERT(m_hkey);
  41. if (m_hkey == NULL)
  42. return m_err;
  43. DWORD size = 0;
  44. DWORD type;
  45. if (RegQueryValueEx(m_hkey, subkey, NULL, &type, NULL, &size) != ERROR_SUCCESS) {
  46. TRACE2("Reg::ReadString() error accessing \"%s\". err=%d\n", subkey, ::GetLastError());
  47. return m_err;
  48. }
  49. if (type != REG_SZ) {
  50. TRACE1("Reg::ReadString() -- type(%d) is not REG_SZ.\n", type);
  51. return m_err;
  52. }
  53. CString tmp;
  54. LPTSTR buf = tmp.GetBuffer(size);
  55. VERIFY( RegQueryValueEx(m_hkey, subkey, NULL, &type, (LPBYTE)buf, &size) == ERROR_SUCCESS );
  56. tmp.ReleaseBuffer();
  57. return tmp;
  58. }
  59. bool CConRegistry::WriteString(LPCTSTR subkey, const CString& value)
  60. {
  61. ASSERT(m_hkey);
  62. if (m_hkey == NULL)
  63. return false;
  64. if (RegSetValueEx(m_hkey, subkey, 0, REG_SZ, (LPBYTE)(LPCTSTR)value, (value.GetLength() + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  65. WriteError(subkey);
  66. return false;
  67. }
  68. return true;
  69. }
  70. DWORD CConRegistry::ReadDWORD(LPCTSTR subkey)
  71. {
  72. DWORD value;
  73. DWORD type;
  74. DWORD size = sizeof value;
  75. if (RegQueryValueEx(m_hkey, subkey, NULL, &type, (LPBYTE)&value, &size) != ERROR_SUCCESS) {
  76. return 0;
  77. }
  78. if (type != REG_DWORD)
  79. return 0;
  80. return value;
  81. }
  82. bool CConRegistry::WriteDWORD(LPCTSTR subkey, DWORD value)
  83. {
  84. if (RegSetValueEx(m_hkey, subkey, 0, REG_DWORD, (LPBYTE)&value, sizeof value) != ERROR_SUCCESS) {
  85. WriteError(subkey);
  86. return false;
  87. }
  88. return true;
  89. }
  90. void CConRegistry::WriteError(LPCTSTR subkey)
  91. {
  92. CString buf;
  93. buf.Format(_T("Registry write operation (%s) failed.\r\nErr code=%d"), subkey, ::GetLastError());
  94. AfxMessageBox(buf);
  95. }
  96. //////////////////////////////////////////////////////////
  97. bool CConRegistry::ReadCustom(ExtKeyDefBuf* buf)
  98. {
  99. ASSERT(m_hkey);
  100. if (m_hkey == NULL)
  101. return false;
  102. DWORD size;
  103. DWORD type;
  104. if (RegQueryValueEx(m_hkey, CONSOLE_REGISTRY_EXTENDEDEDITKEY_CUSTOM, NULL, &type, NULL, &size) != ERROR_SUCCESS) {
  105. TRACE2("Reg::ReadCuston() error accessing \"%s\". err=%d\n", CONSOLE_REGISTRY_EXTENDEDEDITKEY_CUSTOM, ::GetLastError());
  106. return false;
  107. }
  108. if (type != REG_BINARY) {
  109. TRACE1("Reg::ReadString() -- type(%d) is not REG_SZ.\n", type);
  110. return false;
  111. }
  112. if (size != sizeof *buf) {
  113. TRACE1("Reg:ReadCuston() -- size(%d) is different.\n", size);
  114. return false;
  115. }
  116. VERIFY( RegQueryValueEx(m_hkey, CONSOLE_REGISTRY_EXTENDEDEDITKEY_CUSTOM, NULL, &type, (LPBYTE)buf, &size) == ERROR_SUCCESS );
  117. return true;
  118. }
  119. bool CConRegistry::WriteCustom(const ExtKeyDefBuf* buf)
  120. {
  121. ASSERT(m_hkey);
  122. if (m_hkey == NULL)
  123. return false;
  124. if (RegSetValueEx(m_hkey, CONSOLE_REGISTRY_EXTENDEDEDITKEY_CUSTOM, 0, REG_BINARY, (LPBYTE)buf, sizeof *buf) != ERROR_SUCCESS) {
  125. WriteError(CONSOLE_REGISTRY_EXTENDEDEDITKEY_CUSTOM);
  126. return false;
  127. }
  128. return true;
  129. }
  130. DWORD CConRegistry::ReadMode()
  131. {
  132. return ReadDWORD(CONSOLE_REGISTRY_EXTENDEDEDITKEY);
  133. }
  134. bool CConRegistry::WriteMode(DWORD value)
  135. {
  136. return WriteDWORD(CONSOLE_REGISTRY_EXTENDEDEDITKEY, value);
  137. }
  138. CString CConRegistry::ReadWordDelim()
  139. {
  140. return ReadString(CONSOLE_REGISTRY_WORD_DELIM);
  141. }
  142. bool CConRegistry::WriteWordDelim(const CString& value)
  143. {
  144. return WriteString(CONSOLE_REGISTRY_WORD_DELIM, value);
  145. }
  146. DWORD CConRegistry::ReadTrimLeadingZeros()
  147. {
  148. return ReadDWORD(CONSOLE_REGISTRY_TRIMZEROHEADINGS);
  149. }
  150. bool CConRegistry::WriteTrimLeadingZeros(DWORD value)
  151. {
  152. return WriteDWORD(CONSOLE_REGISTRY_TRIMZEROHEADINGS, value);
  153. }
  154. #define CMD_REGISTRY_FILENAME_COMPLETION _T("CompletionChar")
  155. bool CConRegistry::ReadCmdFunctions(CmdExeFunctions* func)
  156. {
  157. DWORD size = sizeof(func->dwFilenameCompletion);
  158. DWORD type;
  159. return RegQueryValueEx(m_cmdKey, CMD_REGISTRY_FILENAME_COMPLETION, NULL, &type, (LPBYTE)&func->dwFilenameCompletion, &size) == ERROR_SUCCESS;
  160. }
  161. bool CConRegistry::WriteCmdFunctions(const CmdExeFunctions* func)
  162. {
  163. return RegSetValueEx(m_cmdKey, CMD_REGISTRY_FILENAME_COMPLETION, 0, REG_DWORD, (LPBYTE)&func->dwFilenameCompletion, sizeof func->dwFilenameCompletion) == ERROR_SUCCESS;
  164. }