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.

275 lines
8.8 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. ** 08-04-99 a-skuzin 'GetRegMultiString' **
  11. ** function added **
  12. ** 08-11-99 a-skuzin 'SetRegKey', **
  13. ** 'SetRegKeyString', **
  14. ** 'DeleteRegKey'now return **
  15. ** an error value if any, **
  16. ** and ERROR_SUCCESS if success.**
  17. **-----------------------------------------------*/
  18. /*
  19. LONG SetRegKey (HKEY root, TCHAR *szKeyPath,
  20. TCHAR *szKeyName, BYTE nKeyValue);
  21. LONG DeleteRegKey (HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName);
  22. BOOL CheckForRegKey (HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName);
  23. int GetRegKeyValue (HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName);
  24. TCHAR * GetRegString (HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName);
  25. LONG SetRegKeyString (HKEY root, TCHAR *szRegString,
  26. TCHAR *szKeyPath, TCHAR *szKeyName);
  27. registry keys used:
  28. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\
  29. Notify\tsver\Asynchronous REG_DWORD = 0
  30. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\
  31. Notify\tsver\DllName REG_SZ = tsver.dll
  32. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\
  33. Notify\tsver\Impersonate REG_DWORD = 0
  34. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\
  35. Notify\tsver\Startup REG_SZ = TsVerEventStartup
  36. HKEY_USERS\.DEFAULT\Software\Microsoft\Windows NT\CurrentVersion\TsVer\
  37. Constraints
  38. */
  39. #include "tsverui.h"
  40. TCHAR *KeyName[] = {TEXT("Asynchronous"), TEXT("DllName"), TEXT("Impersonate"), TEXT("Startup"),
  41. TEXT("Constraints"), TEXT("MsgEnabled"), TEXT("Title"), TEXT("Message"),
  42. TEXT("ClientVersions"), TEXT("DoNotShowWelcome")};
  43. ///////////////////////////////////////////////////////////////////////////////
  44. // Saves a numeric value in the registry
  45. //
  46. // SetRegKey(i, KeyValue);
  47. // i is the index of the KeyName variable
  48. // nKeyValue is the value we want to store.
  49. //
  50. // TCHAR *KeyName[] = {"Constraints"};
  51. // const TCHAR szWinStaKey[] =
  52. // {"Software\\Microsoft\\Windows NT\\CurrentVersion\\TsVer"};
  53. ///////////////////////////////////////////////////////////////////////////////
  54. LONG SetRegKey(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName, BYTE nKeyValue)
  55. {
  56. HKEY hKey;
  57. DWORD dwDisposition;
  58. LONG lResult;
  59. if ((lResult=RegCreateKeyEx(root, szKeyPath,
  60. 0, TEXT("REG_DWORD"), REG_OPTION_NON_VOLATILE,
  61. KEY_ALL_ACCESS, 0, &hKey, &dwDisposition))
  62. == ERROR_SUCCESS) {
  63. //
  64. // write the key value to the registry
  65. //
  66. lResult=RegSetValueEx(hKey, szKeyName, 0, REG_DWORD,
  67. &nKeyValue,
  68. sizeof(DWORD));
  69. RegCloseKey(hKey);
  70. }
  71. return lResult;
  72. }
  73. //////////////////////////////////////////////////////////////////////////////
  74. // Deletes the specified registry key.
  75. //
  76. //////////////////////////////////////////////////////////////////////////////
  77. LONG DeleteRegKey(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName)
  78. {
  79. HKEY hKey;
  80. LONG lResult;
  81. if ((lResult=RegOpenKeyEx(root, szKeyPath, 0,
  82. KEY_ALL_ACCESS, &hKey)) == ERROR_SUCCESS)
  83. {
  84. lResult=RegDeleteValue(hKey, szKeyName);
  85. RegCloseKey(hKey);
  86. }
  87. //if value not found it does not exist already.
  88. if(lResult==ERROR_FILE_NOT_FOUND)
  89. lResult=ERROR_SUCCESS;
  90. return lResult;
  91. }
  92. //////////////////////////////////////////////////////////////////////////////
  93. // returns TRUE if the registry key is there and FALSE if it isn't
  94. //
  95. // TCHAR *KeyName[] = {"Constraints"};
  96. // TCHAR szWinStaKey[] =
  97. // {"Software\\Microsoft\\Windows NT\\CurrentVersion\\TsVer"};
  98. //////////////////////////////////////////////////////////////////////////////
  99. BOOL CheckForRegKey(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName)
  100. {
  101. HKEY hKey;
  102. DWORD dwType;
  103. DWORD dwSize;
  104. dwType = REG_SZ;
  105. dwSize = sizeof(DWORD);
  106. if (RegOpenKeyEx(root, szKeyPath, 0,
  107. KEY_READ, &hKey) == ERROR_SUCCESS) {
  108. if (RegQueryValueEx(hKey, szKeyName, 0,
  109. &dwType, NULL,
  110. &dwSize) == ERROR_SUCCESS) {
  111. RegCloseKey(hKey);
  112. return TRUE;
  113. }
  114. RegCloseKey(hKey);
  115. }
  116. return FALSE;
  117. }
  118. //////////////////////////////////////////////////////////////////////////////
  119. // pass the index of the KeyName variable and the function
  120. // returns the value stored in the registry
  121. // TCHAR *KeyName[] = {"Constraints"};
  122. //////////////////////////////////////////////////////////////////////////////
  123. int GetRegKeyValue(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName)
  124. {
  125. int nKeyValue;
  126. HKEY hKey;
  127. DWORD dwType;
  128. DWORD dwSize;
  129. dwType = REG_SZ;
  130. dwSize = sizeof(DWORD);
  131. if (RegOpenKeyEx(root, szKeyPath, 0,
  132. KEY_READ, &hKey) == ERROR_SUCCESS) {
  133. if (RegQueryValueEx(hKey, szKeyName, 0,
  134. &dwType, (LPBYTE) &nKeyValue,
  135. &dwSize) == ERROR_SUCCESS) {
  136. RegCloseKey(hKey);
  137. return nKeyValue;
  138. }
  139. RegCloseKey(hKey);
  140. }
  141. return 0;
  142. }
  143. //////////////////////////////////////////////////////////////////////////////
  144. // Allocates and returns a string if it succeeds or NULL if it fails
  145. //
  146. // TCHAR *KeyName[] = {"Constraints"};
  147. // #define CONSTRAINTS 0
  148. // GetRegString(CONSTRAINTS);
  149. // const TCHAR szWinStaKey[] =
  150. // {"Software\\Microsoft\\Windows NT\\CurrentVersion\\TsVer"};
  151. //
  152. //////////////////////////////////////////////////////////////////////////////
  153. TCHAR * GetRegString(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName)
  154. {
  155. HKEY hKey = 0;
  156. DWORD dwType = REG_SZ;
  157. DWORD dwSize = 0;
  158. TCHAR *szValue=NULL;
  159. if (RegOpenKeyEx(root, szKeyPath, 0,
  160. KEY_READ, &hKey) == ERROR_SUCCESS)
  161. {
  162. if(RegQueryValueEx(hKey, szKeyName, 0, &dwType,
  163. NULL, &dwSize) == ERROR_SUCCESS){
  164. szValue=new TCHAR[dwSize/sizeof(TCHAR)];
  165. if (RegQueryValueEx(hKey, szKeyName, 0, &dwType,
  166. (LPBYTE)szValue, &dwSize) == ERROR_SUCCESS){
  167. RegCloseKey(hKey);
  168. return szValue;
  169. } else {
  170. delete szValue;
  171. szValue=NULL;
  172. }
  173. }
  174. }
  175. return NULL;
  176. }
  177. //////////////////////////////////////////////////////////////////////////////
  178. // returns TRUE if success, returns FAIL otherwise
  179. //
  180. // TCHAR szNewRegistryString[] = "the rain in spain falls, mainly";
  181. // TCHAR *KeyName[] = {"Constraints", "StartTsVer"};
  182. // #define CONSTRAINTS 0
  183. // #define MAX_LEN 1024
  184. // SetRegKeyString(szNewRegistryString,
  185. // szWinStaKey, KeyName[CONSTRAINTS]);
  186. //////////////////////////////////////////////////////////////////////////////
  187. LONG SetRegKeyString(HKEY root, TCHAR *szRegString,
  188. TCHAR *szKeyPath, TCHAR *szKeyName)
  189. {
  190. HKEY hKey;
  191. DWORD dwDisposition;
  192. TCHAR lpszKeyType[] = TEXT("REG_SZ");
  193. LONG lResult;
  194. if ((lResult=RegCreateKeyEx(root, szKeyPath,
  195. 0, lpszKeyType, REG_OPTION_NON_VOLATILE,
  196. KEY_ALL_ACCESS, 0, &hKey, &dwDisposition))
  197. == ERROR_SUCCESS) {
  198. //
  199. // write the key value to the registry
  200. //
  201. lResult=RegSetValueEx(hKey, szKeyName, 0, REG_SZ,
  202. (BYTE*)szRegString,
  203. (_tcslen(szRegString)+1)*sizeof(TCHAR));
  204. RegCloseKey(hKey);
  205. }
  206. return lResult;
  207. }
  208. //////////////////////////////////////////////////////////////////////////////
  209. /*++
  210. Routine Description:
  211. Allocates buffer and fills it with data from registry.
  212. Arguments:
  213. Return Value:
  214. none
  215. --*/
  216. void GetRegMultiString(HKEY root, TCHAR *szKeyPath, TCHAR *szKeyName, TCHAR **ppBuffer)
  217. {
  218. *ppBuffer=NULL;//if failed (i.e. value does not exist)
  219. HKEY hKey = 0;
  220. DWORD dwType = REG_MULTI_SZ;
  221. DWORD dwSize = 0;
  222. if (RegOpenKeyEx(root, szKeyPath, 0, KEY_READ, &hKey) == ERROR_SUCCESS){
  223. if (RegQueryValueEx(hKey, szKeyName, 0, &dwType, NULL, &dwSize) == ERROR_SUCCESS){
  224. *ppBuffer=new TCHAR[dwSize/sizeof(TCHAR)];
  225. if(*ppBuffer != NULL)
  226. {
  227. ZeroMemory(*ppBuffer,dwSize);
  228. RegQueryValueEx(hKey, szKeyName, 0, &dwType, (LPBYTE)*ppBuffer, &dwSize);
  229. }
  230. }
  231. RegCloseKey(hKey);
  232. }
  233. }