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.

186 lines
4.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996.
  5. //
  6. // File: registry.cxx
  7. //
  8. // Contents: Registry utilities
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 23-May-1996 RamV (Ram Viswanathan) Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "procs.hxx"
  18. #pragma hdrstop
  19. //+---------------------------------------------------------------------------
  20. //
  21. // Function: SetKeyAndValue
  22. //
  23. // Synopsis: Used for setting a key value
  24. //
  25. //
  26. // Arguments: pszRegLocation: where to look for/create key
  27. // pszKey : name of the Key
  28. // pszSubKey: name of the subkey
  29. // pszValue : Value to set
  30. //
  31. // Returns: HRESULT
  32. //
  33. // Modifies:
  34. //
  35. // History: 5/21/1996 RamV Created.
  36. //
  37. //----------------------------------------------------------------------------
  38. HRESULT
  39. SetKeyAndValue(
  40. LPTSTR pszRegLocation,
  41. LPTSTR pszKey,
  42. LPTSTR pszSubKey,
  43. LPTSTR pszValue
  44. )
  45. {
  46. HKEY hKey;
  47. TCHAR szKey[MAX_PATH];
  48. LONG lErrorCode;
  49. HRESULT hr = S_OK;
  50. _tcscpy(szKey, pszRegLocation);
  51. if (pszSubKey != NULL){
  52. _tcscpy(szKey, TEXT("\\"));
  53. _tcscat(szKey, pszSubKey);
  54. }
  55. lErrorCode = RegCreateKeyEx(HKEY_CURRENT_USER,
  56. szKey,
  57. 0,
  58. NULL,
  59. REG_OPTION_NON_VOLATILE,
  60. KEY_ALL_ACCESS,
  61. NULL,
  62. &hKey,
  63. NULL);
  64. if (lErrorCode != ERROR_SUCCESS){
  65. return HRESULT_FROM_WIN32(lErrorCode);
  66. }
  67. if (pszValue != NULL){
  68. lErrorCode = RegSetValueEx(hKey,
  69. pszKey,
  70. 0,
  71. REG_SZ,
  72. (BYTE *)pszValue,
  73. (_tcslen(pszValue)+1)*sizeof(TCHAR));
  74. if(lErrorCode != ERROR_SUCCESS){
  75. hr = HRESULT_FROM_WIN32(lErrorCode);
  76. goto cleanup;
  77. }
  78. }
  79. cleanup:
  80. RegCloseKey(hKey);
  81. return S_OK;
  82. }
  83. HRESULT
  84. QueryKeyValue(
  85. LPTSTR pszRegLocation,
  86. LPTSTR pszKey,
  87. LPTSTR * ppszValue
  88. )
  89. {
  90. LONG lErrorCode;
  91. TCHAR szKey[MAX_PATH];
  92. DWORD dwDataLen;
  93. DWORD dwType;
  94. HKEY hKey = NULL;
  95. HRESULT hr = S_OK;
  96. dwDataLen = sizeof(TCHAR)* MAX_PATH;
  97. *ppszValue = (LPTSTR)AllocADsMem( dwDataLen );
  98. if(!*ppszValue){
  99. hr = E_OUTOFMEMORY;
  100. goto cleanup;
  101. }
  102. _tcscpy(szKey, pszRegLocation);
  103. lErrorCode = RegOpenKeyEx(HKEY_CURRENT_USER,
  104. szKey,
  105. NULL,
  106. KEY_ALL_ACCESS,
  107. &hKey);
  108. if( lErrorCode != ERROR_SUCCESS){
  109. hr = HRESULT_FROM_WIN32(lErrorCode);
  110. goto cleanup;
  111. }
  112. lErrorCode = RegQueryValueEx(hKey,
  113. pszKey,
  114. NULL,
  115. &dwType,
  116. (LPBYTE)*ppszValue,
  117. &dwDataLen);
  118. if (lErrorCode == ERROR_MORE_DATA){
  119. FreeADsMem(*ppszValue);
  120. *ppszValue = NULL;
  121. *ppszValue = (LPTSTR)AllocADsMem (dwDataLen);
  122. if(!*ppszValue){
  123. hr = E_OUTOFMEMORY;
  124. goto cleanup;
  125. }
  126. lErrorCode = RegQueryValueEx(hKey,
  127. pszKey,
  128. NULL,
  129. &dwType,
  130. (LPBYTE)*ppszValue,
  131. &dwDataLen);
  132. hr = HRESULT_FROM_WIN32(lErrorCode);
  133. BAIL_IF_ERROR(hr);
  134. } else if (lErrorCode != ERROR_SUCCESS) {
  135. hr = HRESULT_FROM_WIN32(lErrorCode);
  136. FreeADsMem(*ppszValue);
  137. *ppszValue = NULL;
  138. }
  139. cleanup:
  140. if(hKey){
  141. RegCloseKey(hKey);
  142. }
  143. RRETURN(hr);
  144. }