Source code of Windows XP (NT5)
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.

241 lines
7.6 KiB

  1. /*-----------------------------------------------**
  2. ** Copyright (c) 1999 Microsoft Corporation **
  3. ** All Rights reserved **
  4. ** **
  5. ** reg.cpp **
  6. ** **
  7. ** Functions for reading, writing, and deleting **
  8. ** registry keys **
  9. ** 07-01-99 a-clindh Created **
  10. **-----------------------------------------------*/
  11. /*
  12. void SetRegKey (HKEY root, TCHAR *szKeyPath,
  13. TCHAR *szKeyName, BYTE nKeyValue);
  14. void DeleteRegKey (HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName);
  15. BOOL CheckForRegKey (HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName);
  16. int GetRegKeyValue (HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName);
  17. TCHAR * GetRegString (HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName);
  18. BOOL SetRegKeyString (HKEY root, TCHAR *szRegString,
  19. TCHAR *szKeyPath, TCHAR *szKeyName);
  20. registry keys used:
  21. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\
  22. Notify\tsver\Asynchronous REG_DWORD = 0
  23. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\
  24. Notify\tsver\DllName REG_SZ = tsver.dll
  25. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\
  26. Notify\tsver\Impersonate REG_DWORD = 0
  27. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\
  28. Notify\tsver\Startup REG_SZ = TsVerEventStartup
  29. HKEY_USERS\.DEFAULT\Software\Microsoft\Windows NT\CurrentVersion\TsVer\
  30. Constraints
  31. */
  32. #include "TsVsm.h"
  33. TCHAR *KeyName[] = {"Constraints", "StartTsVer", "MsgEnabled", "Title", "Message"};
  34. TCHAR sRegistryKeyReturnValue[MAX_LEN];
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // Saves a numeric value in the registry
  37. //
  38. // SetRegKey(i, KeyValue);
  39. // i is the index of the KeyName variable
  40. // nKeyValue is the value we want to store.
  41. //
  42. // TCHAR *KeyName[] = {"Constraints"};
  43. // const TCHAR szWinStaKey[] =
  44. // {"Software\\Microsoft\\Windows NT\\CurrentVersion\\TsVer"};
  45. ///////////////////////////////////////////////////////////////////////////////
  46. void SetRegKey(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName, BYTE nKeyValue)
  47. {
  48. HKEY hKey;
  49. DWORD dwDisposition;
  50. if (RegCreateKeyEx(root, szKeyPath,
  51. 0, "REG_DWORD", REG_OPTION_NON_VOLATILE,
  52. KEY_ALL_ACCESS, 0, &hKey, &dwDisposition)
  53. == ERROR_SUCCESS) {
  54. //
  55. // write the key value to the registry
  56. //
  57. RegSetValueEx(hKey, szKeyName, 0, REG_DWORD,
  58. &nKeyValue,
  59. sizeof(DWORD));
  60. RegCloseKey(hKey);
  61. }
  62. }
  63. //////////////////////////////////////////////////////////////////////////////
  64. // Deletes the specified registry key.
  65. //
  66. //////////////////////////////////////////////////////////////////////////////
  67. void DeleteRegKey(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName)
  68. {
  69. HKEY hKey;
  70. if (RegOpenKeyEx(root, szKeyPath, 0,
  71. KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
  72. {
  73. RegDeleteValue(hKey, szKeyName);
  74. RegCloseKey(hKey);
  75. }
  76. }
  77. //////////////////////////////////////////////////////////////////////////////
  78. // returns TRUE if the registry key is there and FALSE if it isn't
  79. //
  80. // TCHAR *KeyName[] = {"Constraints"};
  81. // TCHAR szWinStaKey[] =
  82. // {"Software\\Microsoft\\Windows NT\\CurrentVersion\\TsVer"};
  83. //////////////////////////////////////////////////////////////////////////////
  84. BOOL CheckForRegKey(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName)
  85. {
  86. HKEY hKey;
  87. DWORD dwType;
  88. DWORD dwSize;
  89. dwType = REG_SZ;
  90. dwSize = sizeof(DWORD);
  91. if (RegOpenKeyEx(root, szKeyPath, 0,
  92. KEY_READ, &hKey) == ERROR_SUCCESS) {
  93. if (RegQueryValueEx(hKey, szKeyName, 0,
  94. &dwType, NULL,
  95. &dwSize) == ERROR_SUCCESS) {
  96. RegCloseKey(hKey);
  97. return TRUE;
  98. }
  99. RegCloseKey(hKey);
  100. }
  101. return FALSE;
  102. }
  103. //////////////////////////////////////////////////////////////////////////////
  104. // pass the index of the KeyName variable and the function
  105. // returns the value stored in the registry
  106. // TCHAR *KeyName[] = {"Constraints"};
  107. //////////////////////////////////////////////////////////////////////////////
  108. int GetRegKeyValue(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName)
  109. {
  110. int nKeyValue;
  111. HKEY hKey;
  112. DWORD dwType;
  113. DWORD dwSize;
  114. dwType = REG_SZ;
  115. dwSize = sizeof(DWORD);
  116. if (RegOpenKeyEx(root, szKeyPath, 0,
  117. KEY_READ, &hKey) == ERROR_SUCCESS) {
  118. if (RegQueryValueEx(hKey, szKeyName, 0,
  119. &dwType, (LPBYTE) &nKeyValue,
  120. &dwSize) == ERROR_SUCCESS) {
  121. RegCloseKey(hKey);
  122. return nKeyValue;
  123. }
  124. RegCloseKey(hKey);
  125. }
  126. return 0;
  127. }
  128. //////////////////////////////////////////////////////////////////////////////
  129. // returns a string if it succeeds or NULL if it fails
  130. //
  131. // TCHAR *KeyName[] = {"Constraints"};
  132. // #define CONSTRAINTS 0
  133. // #define MAX_LEN 1024
  134. // TCHAR sRegistryKeyReturnValue[MAX_LEN];
  135. // GetRegString(CONSTRAINTS);
  136. // const TCHAR szWinStaKey[] =
  137. // {"Software\\Microsoft\\Windows NT\\CurrentVersion\\TsVer"};
  138. //
  139. //////////////////////////////////////////////////////////////////////////////
  140. TCHAR * GetRegString(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName)
  141. {
  142. // sRegistryKeyReturnValue needs to ba a global variable otherwise
  143. // it will go out of scope when the function returns
  144. HKEY hKey = 0;
  145. DWORD dwType = REG_SZ;
  146. DWORD dwSize = sizeof(sRegistryKeyReturnValue);
  147. if (RegOpenKeyEx(root, szKeyPath, 0,
  148. KEY_READ, &hKey) == ERROR_SUCCESS)
  149. {
  150. if (RegQueryValueEx(hKey, szKeyName, 0, &dwType,
  151. (LPBYTE) &sRegistryKeyReturnValue,
  152. &dwSize) == ERROR_SUCCESS)
  153. {
  154. RegCloseKey(hKey);
  155. return sRegistryKeyReturnValue;
  156. }
  157. }
  158. return '\0';
  159. }
  160. //////////////////////////////////////////////////////////////////////////////
  161. // returns TRUE if success, returns FAIL otherwise
  162. //
  163. // TCHAR szNewRegistryString[] = "the rain in spain falls, mainly";
  164. // TCHAR *KeyName[] = {"Constraints", "StartTsVer"};
  165. // #define CONSTRAINTS 0
  166. // #define MAX_LEN 1024
  167. // SetRegKeyString(szNewRegistryString,
  168. // szWinStaKey, KeyName[CONSTRAINTS]);
  169. //////////////////////////////////////////////////////////////////////////////
  170. BOOL SetRegKeyString(HKEY root, TCHAR *szRegString,
  171. TCHAR *szKeyPath, TCHAR *szKeyName)
  172. {
  173. HKEY hKey;
  174. DWORD dwDisposition;
  175. TCHAR lpszKeyType[] = "REG_SZ";
  176. if (RegCreateKeyEx(root, szKeyPath,
  177. 0, lpszKeyType, REG_OPTION_NON_VOLATILE,
  178. KEY_ALL_ACCESS, 0, &hKey, &dwDisposition)
  179. == ERROR_SUCCESS) {
  180. //
  181. // write the key value to the registry
  182. //
  183. RegSetValueEx(hKey, szKeyName, 0, REG_SZ,
  184. (BYTE*)szRegString,
  185. _tcslen(szRegString));
  186. RegCloseKey(hKey);
  187. return TRUE;
  188. } else {
  189. return FALSE;
  190. }
  191. }
  192. //////////////////////////////////////////////////////////////////////////////
  193. int CDECL MB(TCHAR *szCaption, TCHAR *szFormat, ...)
  194. {
  195. TCHAR szBuffer[1024];
  196. va_list pArgList;
  197. va_start(pArgList, szFormat);
  198. _vsntprintf(szBuffer, sizeof(szBuffer) / sizeof(TCHAR),
  199. szFormat, pArgList);
  200. va_end(pArgList);
  201. return MessageBox(NULL, szBuffer, szCaption, MB_OK | MB_ICONSTOP);
  202. }
  203. //////////////////////////////////////////////////////////////////////////////