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.

101 lines
2.4 KiB

  1. // regutil.cpp : This file contains the
  2. // Created: Mar '98
  3. // History:
  4. // Copyright (C) 1998 Microsoft Corporation
  5. // All rights reserved.
  6. // Microsoft Confidential
  7. #include "cmnhdr.h"
  8. #include "debug.h"
  9. using namespace _Utils;
  10. using CDebugLevel::TRACE_DEBUGGING;
  11. using CDebugLevel::TRACE_HANDLE;
  12. using CDebugLevel::TRACE_SOCKET;
  13. bool
  14. GetRegistryDWORD
  15. (
  16. HKEY hk,
  17. LPTSTR lpszTag,
  18. LPDWORD lpdwValue,
  19. DWORD dwDefault,
  20. BOOL fOverwrite
  21. )
  22. {
  23. DWORD dwType;
  24. DWORD dwSize = sizeof( DWORD );
  25. if( !fOverwrite && RegQueryValueEx( hk, lpszTag, NULL, &dwType, ( LPBYTE )lpdwValue,
  26. &dwSize ) == ERROR_SUCCESS )
  27. {
  28. if( ( dwType != REG_DWORD ) || ( dwSize != sizeof( DWORD ) ) )
  29. return ( false );
  30. }
  31. else
  32. {
  33. *lpdwValue = dwDefault;
  34. dwSize = sizeof( DWORD );
  35. RegSetValueEx( hk, lpszTag, 0, REG_DWORD, (LPBYTE) lpdwValue, dwSize );
  36. }
  37. return ( true );
  38. }
  39. bool
  40. GetRegistrySZ
  41. (
  42. HKEY hk,
  43. LPTSTR tag,
  44. LPTSTR* lpszValue,
  45. LPTSTR def,
  46. BOOL fOverwrite
  47. )
  48. {
  49. DWORD dwSize;
  50. DWORD dwType;
  51. DWORD dwStatus;
  52. dwSize = 0;
  53. dwStatus = RegQueryValueEx(hk, tag, NULL, &dwType, (LPBYTE) NULL, &dwSize);
  54. if( !fOverwrite && (( dwStatus == ERROR_MORE_DATA ) || ( dwStatus == ERROR_SUCCESS )) )
  55. {
  56. if( ( dwType != REG_EXPAND_SZ ) && ( dwType != REG_SZ ) )
  57. return ( false );
  58. if( dwSize == 0 )
  59. dwSize++;
  60. *lpszValue = ( LPTSTR ) new TCHAR[ dwSize ];
  61. if (*lpszValue)
  62. {
  63. if( RegQueryValueEx( hk, tag, NULL, &dwType, ( LPBYTE ) *lpszValue,
  64. &dwSize ) != ERROR_SUCCESS )
  65. {
  66. return ( false );
  67. }
  68. }
  69. else
  70. {
  71. return (false);
  72. }
  73. }
  74. else
  75. {
  76. *lpszValue = ( LPTSTR ) new
  77. TCHAR[( wcslen( def )) + 1 ];
  78. if (*lpszValue)
  79. {
  80. wcscpy( *lpszValue, def ); //Attack ? size not known.
  81. dwSize = ( wcslen( *lpszValue ) + 1 ) * sizeof( TCHAR );
  82. RegSetValueEx(hk, tag, 0, REG_EXPAND_SZ, (LPBYTE) *lpszValue, dwSize);
  83. }
  84. else
  85. {
  86. return (false);
  87. }
  88. }
  89. return ( true );
  90. }