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.

68 lines
1.4 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>(::malloc(size * sizeof(wchar_t)));
  21. if (!pDest) {
  22. _com_issue_error(E_OUTOFMEMORY);
  23. }
  24. pDest[0] = '\0';
  25. if (::MultiByteToWideChar(CP_ACP, 0, pSrc, -1, pDest, size) == 0) {
  26. free(pDest);
  27. _com_issue_error(HRESULT_FROM_WIN32(GetLastError()));
  28. }
  29. BSTR pResult = ::SysAllocString(pDest);
  30. free(pDest);
  31. return pResult;
  32. }
  33. }
  34. // Convert BSTR to char *
  35. //
  36. char* __stdcall ConvertBSTRToString(BSTR pSrc) throw(_com_error)
  37. {
  38. if (pSrc == NULL) {
  39. return NULL;
  40. }
  41. else {
  42. int size = (wcslen(pSrc) + 1) * sizeof(wchar_t);
  43. char* pDest = ::new char[size];
  44. if (pDest == NULL) {
  45. _com_issue_error(E_OUTOFMEMORY);
  46. }
  47. pDest[0] = '\0';
  48. if (::WideCharToMultiByte(CP_ACP, 0, pSrc, -1, pDest, size, NULL, NULL) == 0) {
  49. _com_issue_error(HRESULT_FROM_WIN32(GetLastError()));
  50. }
  51. return pDest;
  52. }
  53. }
  54. }