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.

144 lines
2.8 KiB

  1. /*++
  2. Copyright (C) 1996-1999 Microsoft Corporation
  3. Module Name:
  4. bstrutil.cpp
  5. Abstract:
  6. B string utility functions.
  7. --*/
  8. #include "ole2.h"
  9. #include "bstrutil.h"
  10. INT SysStringAnsiLen (
  11. IN BSTR bstr
  12. )
  13. {
  14. if (bstr == NULL)
  15. return 0;
  16. #ifndef OLE2ANSI
  17. return WideCharToMultiByte(CP_ACP, 0, bstr, SysStringLen(bstr),
  18. NULL, 0, NULL, NULL);
  19. #else
  20. return SysStringLen(bstr);
  21. #endif
  22. }
  23. HRESULT BStrToStream (
  24. IN LPSTREAM pIStream,
  25. IN INT nMbChar,
  26. IN BSTR bstr
  27. )
  28. {
  29. LPSTR pchBuf;
  30. HRESULT hr;
  31. // If empty string just return
  32. if (SysStringLen(bstr) == 0)
  33. return NO_ERROR;
  34. #ifndef OLE2ANSI
  35. // Convert to multibyte string
  36. pchBuf = new char[nMbChar + 1];
  37. if (pchBuf == NULL)
  38. return E_OUTOFMEMORY;
  39. WideCharToMultiByte(CP_ACP, 0, bstr, SysStringLen(bstr),
  40. pchBuf, nMbChar+1, NULL, NULL);
  41. // Write string to stream
  42. hr = pIStream->Write(pchBuf, nMbChar, NULL);
  43. delete [] pchBuf;
  44. #else
  45. hr = pIStream->Write(bstr, nMbChar, NULL);
  46. #endif
  47. return hr;
  48. }
  49. HRESULT BStrFromStream (
  50. IN LPSTREAM pIStream,
  51. IN INT nChar,
  52. OUT BSTR *pbstrRet
  53. )
  54. {
  55. HRESULT hr;
  56. BSTR bstr;
  57. ULONG nRead;
  58. LPSTR pchBuf;
  59. INT nWChar;
  60. *pbstrRet = NULL;
  61. // if zero-length string just return
  62. if (nChar == 0)
  63. return NO_ERROR;
  64. #ifndef OLE2ANSI
  65. // Allocate char array and read in string
  66. pchBuf = new char[nChar];
  67. if (pchBuf == NULL)
  68. return E_OUTOFMEMORY;
  69. hr = pIStream->Read(pchBuf, nChar, &nRead);
  70. // Verify read count
  71. if (!FAILED(hr)) {
  72. if (nRead != (ULONG)nChar)
  73. hr = E_FAIL;
  74. }
  75. if (!FAILED(hr)) {
  76. // Allocate BString for UNICODE translation
  77. nWChar = MultiByteToWideChar(CP_ACP, 0, pchBuf, nChar, NULL, 0);
  78. bstr = SysAllocStringLen(NULL, nWChar);
  79. if (bstr != NULL) {
  80. MultiByteToWideChar(CP_ACP, 0, pchBuf, nChar, bstr, nWChar);
  81. bstr[nWChar] = 0;
  82. *pbstrRet = bstr;
  83. }
  84. else
  85. hr = E_OUTOFMEMORY;
  86. }
  87. delete [] pchBuf;
  88. #else
  89. // Allocate BString
  90. bstr = SysAllocStringLen(NULL, nChar);
  91. if (bstr == NULL)
  92. return E_OUTOFMEMORY;
  93. // Read in string
  94. hr = pIStream->Read(bstr, nChar, &nRead);
  95. // Verify read count
  96. if (!FAILED(hr)) {
  97. if (nRead != (ULONG)nChar)
  98. hr = E_FAIL;
  99. }
  100. // Return or free string
  101. if (!FAILED(hr))
  102. *pbstrRet = bstr;
  103. else
  104. SysFreeString(bstr);
  105. #endif
  106. return hr;
  107. }