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.

110 lines
3.2 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // GlobalFuncs.cpp
  7. //
  8. // Description:
  9. // Contains the definitions of a few unrelated global functions
  10. //
  11. // Maintained By:
  12. // David Potter (DavidP) 25-MAR-2002
  13. // Vij Vasu (Vvasu) 06-SEP-2000
  14. //
  15. //////////////////////////////////////////////////////////////////////////////
  16. //////////////////////////////////////////////////////////////////////////////
  17. // Include Files
  18. //////////////////////////////////////////////////////////////////////////////
  19. // The precompiled header.
  20. #include "Pch.h"
  21. //////////////////////////////////////////////////////////////////////////////
  22. //++
  23. //
  24. // DwLoadString()
  25. //
  26. // Description:
  27. // Allocate memory for and load a string from the string table.
  28. //
  29. // Arguments:
  30. // uiStringIdIn
  31. // Id of the string to look up
  32. //
  33. // rsszDestOut
  34. // Reference to the smart pointer to the loaded string.
  35. //
  36. // Return Value:
  37. // S_OK
  38. // If the call succeeded
  39. //
  40. // Other Win32 error codes
  41. // If the call failed.
  42. //
  43. // Remarks:
  44. // This function cannot load a zero length string.
  45. //--
  46. //////////////////////////////////////////////////////////////////////////////
  47. DWORD
  48. DwLoadString(
  49. UINT nStringIdIn
  50. , SmartSz & rsszDestOut
  51. )
  52. {
  53. TraceFunc( "" );
  54. DWORD dwError = ERROR_SUCCESS;
  55. UINT uiCurrentSize = 0;
  56. SmartSz sszCurrentString;
  57. UINT uiReturnedStringLen = 0;
  58. do
  59. {
  60. // Grow the current string by an arbitrary amount.
  61. uiCurrentSize += 256;
  62. sszCurrentString.Assign( new WCHAR[ uiCurrentSize ] );
  63. if ( sszCurrentString.FIsEmpty() )
  64. {
  65. dwError = TW32( ERROR_NOT_ENOUGH_MEMORY );
  66. TraceFlow2( "Error %#x occurred trying allocate memory for string (string id is %d).", dwError, nStringIdIn );
  67. LogMsg( "Error %#x occurred trying allocate memory for string (string id is %d).", dwError, nStringIdIn );
  68. break;
  69. } // if: the memory allocation has failed
  70. uiReturnedStringLen = ::LoadStringW(
  71. g_hInstance
  72. , nStringIdIn
  73. , sszCurrentString.PMem()
  74. , uiCurrentSize
  75. );
  76. if ( uiReturnedStringLen == 0 )
  77. {
  78. dwError = TW32( GetLastError() );
  79. TraceFlow2( "Error %#x occurred trying load string (string id is %d).", dwError, nStringIdIn );
  80. LogMsg( "Error %#x occurred trying load string (string id is %d).", dwError, nStringIdIn );
  81. break;
  82. } // if: LoadString() had an error
  83. ++uiReturnedStringLen;
  84. }
  85. while( uiCurrentSize <= uiReturnedStringLen );
  86. if ( dwError == ERROR_SUCCESS )
  87. {
  88. rsszDestOut = sszCurrentString;
  89. } // if: there were no errors in this function
  90. else
  91. {
  92. rsszDestOut.PRelease();
  93. } // else: something went wrong
  94. RETURN( dwError );
  95. } //*** DwLoadString()