Source code of Windows XP (NT5)
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.

106 lines
2.2 KiB

  1. //
  2. // MODULE: CharConv.CPP
  3. //
  4. // PURPOSE: conversion between char & TCHAR
  5. //
  6. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  7. //
  8. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  9. //
  10. // AUTHOR: Joe Mabel
  11. //
  12. // ORIGINAL DATE:
  13. //
  14. // NOTES:
  15. // 1. ConvertWCharToString pulled out of VersionInfo.
  16. //
  17. // Version Date By Comments
  18. //--------------------------------------------------------------------
  19. // V3.0 JM
  20. //
  21. #include "stdafx.h"
  22. #include "CharConv.h"
  23. // Convert Unicode ("wide character") to CString, regardless of whether this
  24. // program is built Unicode. How this program is built determines the
  25. // underlying character type of CString.
  26. // As a convenience, returns a refernce to strRetVal
  27. /*static*/ CString& CCharConversion::ConvertWCharToString(LPCWSTR wsz, CString &strRetVal)
  28. {
  29. #ifdef UNICODE
  30. strRetVal = wsz;
  31. #else
  32. TCHAR * pBuf;
  33. int bufsize = ::WideCharToMultiByte(
  34. CP_ACP,
  35. 0,
  36. wsz,
  37. -1,
  38. NULL,
  39. 0,
  40. NULL,
  41. NULL
  42. );
  43. pBuf = new TCHAR[bufsize];
  44. //[BC-03022001] - added check for NULL ptr to satisfy MS code analysis tool.
  45. if(pBuf)
  46. {
  47. ::WideCharToMultiByte(
  48. CP_ACP,
  49. 0,
  50. wsz,
  51. -1,
  52. pBuf,
  53. bufsize,
  54. NULL,
  55. NULL
  56. );
  57. strRetVal = pBuf;
  58. delete[] pBuf;
  59. }
  60. #endif
  61. return strRetVal;
  62. }
  63. // Convert char* (ASCII/ANSI, not "wide" character) to CString, regardless of whether this
  64. // program is built Unicode. How this program is built determines the
  65. // underlying character type of CString.
  66. // As a convenience, returns a refernce to strRetVal
  67. /*static*/ CString& CCharConversion::ConvertACharToString(LPCSTR sz, CString &strRetVal)
  68. {
  69. #ifdef UNICODE
  70. TCHAR * pBuf;
  71. int bufsize = ::MultiByteToWideChar(
  72. CP_ACP,
  73. 0,
  74. sz,
  75. -1,
  76. NULL,
  77. 0
  78. );
  79. pBuf = new TCHAR[bufsize];
  80. //[BC-03022001] - added check for NULL ptr to satisfy MS code analysis tool.
  81. if(pBuf)
  82. {
  83. ::MultiByteToWideChar(
  84. CP_ACP,
  85. 0,
  86. sz,
  87. -1,
  88. pBuf,
  89. bufsize
  90. );
  91. strRetVal = pBuf;
  92. delete[] pBuf;
  93. }
  94. #else
  95. strRetVal = sz;
  96. #endif
  97. return strRetVal;
  98. }