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.

62 lines
1.2 KiB

  1. //
  2. // The definitions of functions and data declared in comutil.h
  3. //
  4. #include <comdef.h>
  5. #pragma hdrstop
  6. #include <malloc.h>
  7. #pragma warning(disable:4290)
  8. _variant_t vtMissing(DISP_E_PARAMNOTFOUND, VT_ERROR);
  9. namespace _com_util {
  10. //
  11. // Convert char * to BSTR
  12. //
  13. BSTR __stdcall ConvertStringToBSTR(const char* pSrc) throw(_com_error)
  14. {
  15. if (pSrc == NULL) {
  16. return NULL;
  17. }
  18. else {
  19. int size = lstrlenA(pSrc) + 1;
  20. BSTR pDest = static_cast<BSTR>(::_alloca(size * sizeof(wchar_t)));
  21. pDest[0] = '\0';
  22. if (::MultiByteToWideChar(CP_ACP, 0, pSrc, -1, pDest, size) == 0) {
  23. _com_issue_error(HRESULT_FROM_WIN32(GetLastError()));
  24. }
  25. return ::SysAllocString(pDest);
  26. }
  27. }
  28. // Convert BSTR to char *
  29. //
  30. char* __stdcall ConvertBSTRToString(BSTR pSrc) throw(_com_error)
  31. {
  32. if (pSrc == NULL) {
  33. return NULL;
  34. }
  35. else {
  36. int size = (wcslen(pSrc) + 1) * sizeof(wchar_t);
  37. char* pDest = ::new char[size];
  38. if (pDest == NULL) {
  39. _com_issue_error(E_OUTOFMEMORY);
  40. }
  41. pDest[0] = '\0';
  42. if (::WideCharToMultiByte(CP_ACP, 0, pSrc, -1, pDest, size, NULL, NULL) == 0) {
  43. _com_issue_error(HRESULT_FROM_WIN32(GetLastError()));
  44. }
  45. return pDest;
  46. }
  47. }
  48. }