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.

248 lines
5.9 KiB

  1. /*++=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. registry.cpp
  5. Abstract:
  6. Registry functions.
  7. Author:
  8. Paul M Midgen (pmidge) 23-May-2000
  9. Revision History:
  10. 23-May-2000 pmidge
  11. Created
  12. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--*/
  13. #include "common.h"
  14. //#include "registry.h"
  15. LPCWSTR g_wszAppRootKeyName = L"Software\\W3Spoof";
  16. /*++=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  17. GetRegValue()
  18. WHAT : Reads the value of a REG_DWORD or REG_SZ registry value. The
  19. caller must free the value returned through ppvData.
  20. ARGS : szValueName - the value to look up
  21. dwType - can be REG_SZ or REG_DWORD
  22. ppvData - address of a pointer to initialize to the data
  23. read from the registry
  24. RETURNS : True if the lookup succeeded, false if there was an error. The
  25. caller can call GetLastError() to determine the type of error.
  26. Possible values returned by GetLastError() are:
  27. ERROR_NOT_ENOUGH_MEMORY - failed to allocate storage for the
  28. requested key
  29. ERROR_INVALID_PARAMETER - unsupported type in dwType
  30. If registry lookups fail we set last error to the retcode
  31. from the registry api we called.
  32. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--*/
  33. BOOL
  34. GetRegValue(LPCWSTR wszValueName, DWORD dwType, LPVOID* ppvData)
  35. {
  36. BOOL bStatus = FALSE;
  37. DWORD dwRet = 0L;
  38. LPBYTE lpData = NULL;
  39. DWORD cbData = 0L;
  40. HKEY hkAppRoot = _GetRootKey(TRUE);
  41. switch( dwType )
  42. {
  43. case REG_DWORD :
  44. {
  45. cbData = sizeof(DWORD);
  46. lpData = new BYTE[cbData];
  47. if( !lpData )
  48. {
  49. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  50. goto quit;
  51. }
  52. }
  53. break;
  54. case REG_SZ :
  55. {
  56. // get required buffer size
  57. dwRet = RegQueryValueEx(
  58. hkAppRoot, wszValueName, 0L,
  59. &dwType, lpData, &cbData
  60. );
  61. if( dwRet )
  62. {
  63. //DEBUG_TRACE(REGISTRY, ("requested key (%s) doesn't exist", szValueName));
  64. SetLastError(dwRet);
  65. goto quit;
  66. }
  67. lpData = new BYTE[cbData];
  68. if( !lpData )
  69. {
  70. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  71. goto quit;
  72. }
  73. }
  74. break;
  75. default :
  76. {
  77. DEBUG_TRACE(REGISTRY, ("requested type not supported"));
  78. SetLastError(ERROR_INVALID_PARAMETER);
  79. goto quit;
  80. }
  81. break;
  82. }
  83. //
  84. // do the lookup
  85. //
  86. dwRet = RegQueryValueEx(
  87. hkAppRoot, wszValueName, 0L,
  88. &dwType, lpData, &cbData
  89. );
  90. if( dwRet )
  91. {
  92. delete [] lpData;
  93. SetLastError(dwRet);
  94. goto quit;
  95. }
  96. bStatus = TRUE;
  97. *ppvData = (LPVOID) lpData;
  98. #ifdef _DEBUG
  99. if( bStatus )
  100. {
  101. switch( dwType )
  102. {
  103. case REG_DWORD :
  104. DEBUG_TRACE(REGISTRY, ("lpData: %d; cbData: %d", (DWORD)*lpData, cbData));
  105. break;
  106. case REG_SZ :
  107. DEBUG_TRACE(REGISTRY, ("lpdata: %s; cbData: %d", (LPSTR)lpData, cbData));
  108. break;
  109. }
  110. }
  111. #endif
  112. quit:
  113. return bStatus;
  114. }
  115. /*++=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  116. SetRegValue()
  117. WHAT : Writes a value under the application registry key.
  118. ARGS : szValueName - the name of the regkey to write to
  119. dwType - type of the regkey to write
  120. pvData - regkey data
  121. dwSize - bytecount of data to write
  122. RETURNS : True if the write succeeded, false if otherwise.
  123. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--*/
  124. BOOL
  125. SetRegValue(LPCWSTR wszValueName, DWORD dwType, LPVOID pvData, DWORD dwSize)
  126. {
  127. BOOL bStatus = FALSE;
  128. DWORD dwRet = 0L;
  129. HKEY hkAppRoot = _GetRootKey(TRUE);
  130. #ifdef _DEBUG
  131. switch( dwType )
  132. {
  133. case REG_DWORD :
  134. DEBUG_TRACE(REGISTRY, ("pvData: %d; dwSize: %d", (DWORD)*((LPDWORD)pvData), dwSize));
  135. break;
  136. case REG_SZ :
  137. DEBUG_TRACE(REGISTRY, ("pvData: %s; dwSize: %d", (LPSTR)pvData, dwSize));
  138. break;
  139. }
  140. #endif
  141. if( !dwSize && pvData )
  142. dwSize = strlen((LPSTR)pvData);
  143. dwRet = RegSetValueEx(
  144. hkAppRoot, wszValueName, 0L,
  145. dwType, (LPBYTE)pvData, dwSize
  146. );
  147. if( dwRet != ERROR_SUCCESS )
  148. SetLastError(dwRet);
  149. else
  150. bStatus = TRUE;
  151. return bStatus;
  152. }
  153. /*++=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  154. _GetRootKey()
  155. WHAT : Creates/opens the root key used by the app. Remembers the key
  156. handle across calls, and can be called to release the key handle.
  157. ARGS : fOpen - if true, open the regkey, if false, close it.
  158. RETURNS : regkey handle.
  159. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--*/
  160. HKEY _GetRootKey(BOOL fOpen)
  161. {
  162. DWORD disp = 0;
  163. DWORD ret = 0;
  164. static HKEY root = NULL;
  165. if( fOpen )
  166. {
  167. if( !root )
  168. {
  169. ret = RegCreateKeyEx(
  170. HKEY_CURRENT_USER,
  171. g_wszAppRootKeyName,
  172. 0,
  173. NULL,
  174. REG_OPTION_NON_VOLATILE,
  175. KEY_ALL_ACCESS,
  176. NULL,
  177. &root,
  178. &disp
  179. );
  180. }
  181. }
  182. else
  183. {
  184. if( root )
  185. {
  186. RegCloseKey(root);
  187. root = NULL;
  188. }
  189. }
  190. return root;
  191. }