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.

124 lines
2.9 KiB

  1. /*
  2. * BSTRING.H
  3. *
  4. * Defines a BSTRING C++ class that allows us to wrap the OLE BSTR (Basic
  5. * String) type, primarily to simplify the creation and freeing of these
  6. * objects. This is intended to be a lightweight wrapper with minimal
  7. * overhead.
  8. *
  9. * If no input string is specified to the constructor or if the allocation
  10. * of the BSTR fails, then the <m_bstr> member is set to NULL.
  11. *
  12. * Note: The BSTRING class is not intended to allow managing multiple BSTR
  13. * strings with a single object.
  14. *
  15. * Usage scenarios:
  16. *
  17. * 1) Create a BSTR from an existing string, have it automatically freed
  18. * when done.
  19. *
  20. * // Allocate BSTR using SysAllocString()
  21. * BSTRING bstrComputerName(lpstrComputerName);
  22. *
  23. * ...
  24. *
  25. * // Automatic, lightweight cast to BSTR
  26. * SomeFunctionThatTakesABSTR(bstrComputerName);
  27. *
  28. * ...
  29. *
  30. * // SysFreeString() gets called automatically when the scope of
  31. * // bstrComputerName ends.
  32. *
  33. * 2) Create a null BSTRING object, pass it to a function that returns an
  34. * allocated BSTR, then have it automatically freed when done.
  35. *
  36. * // Create null BSTRING
  37. * BSTRING bstrReturnedComputerName;
  38. *
  39. * ...
  40. *
  41. * // Call a function that returns an allocated BSTR.
  42. * SomeFunctionThatReturnsABSTR(bstrReturnedComputerName.GetLPBSTR());
  43. *
  44. * ...
  45. *
  46. * // SysFreeString() gets called automatically when the scope of
  47. * // bstrReturnedComputerName ends.
  48. *
  49. * Author:
  50. * dannygl, 29 Oct 96
  51. */
  52. #if !defined(_BSTRING_H_)
  53. #define _BSTRING_H_
  54. #include <oleauto.h>
  55. #include <confdbg.h>
  56. class BSTRING
  57. {
  58. private:
  59. BSTR m_bstr;
  60. public:
  61. // Constructors
  62. BSTRING() {m_bstr = NULL;}
  63. inline BSTRING(LPCWSTR lpcwString);
  64. #if !defined(UNICODE)
  65. // We don't support construction from an ANSI string in the Unicode build.
  66. BSTRING(LPCSTR lpcString);
  67. #endif // !defined(UNICODE)
  68. // Destructor
  69. inline ~BSTRING();
  70. // Cast to BSTR
  71. operator BSTR() {return m_bstr;}
  72. // Get a BSTR pointer.
  73. //
  74. // This member function is intended for passing this object to
  75. // functions that allocate a BSTR, return a pointer to this BSTR,
  76. // and expect the caller to free the BSTR when done. The BSTR is
  77. // freed when the BSTRING destructor gets called.
  78. inline LPBSTR GetLPBSTR(void);
  79. };
  80. BSTRING::BSTRING(LPCWSTR lpcwString)
  81. {
  82. if (NULL != lpcwString)
  83. {
  84. // SysAllocString returns NULL on failure
  85. m_bstr = SysAllocString(lpcwString);
  86. ASSERT(NULL != m_bstr);
  87. }
  88. else
  89. {
  90. m_bstr = NULL;
  91. }
  92. }
  93. BSTRING::~BSTRING()
  94. {
  95. if (NULL != m_bstr)
  96. {
  97. SysFreeString(m_bstr);
  98. }
  99. }
  100. inline LPBSTR BSTRING::GetLPBSTR(void)
  101. {
  102. // This function is intended to be used to set the BSTR value for
  103. // objects that are initialized to NULL. It should not be called
  104. // on objects which already have a non-NULL BSTR.
  105. ASSERT(NULL == m_bstr);
  106. return &m_bstr;
  107. }
  108. #endif // !defined(_BSTRING_H_)