No Description
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.

WindowsRuntime.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_WINDOWS && !IL2CPP_USE_GENERIC_WINDOWSRUNTIME
  3. #include "il2cpp-class-internals.h"
  4. #include "il2cpp-string-types.h"
  5. #include "os/WindowsRuntime.h"
  6. #include "utils/Expected.h"
  7. #include "utils/Il2CppError.h"
  8. #include "utils/StringUtils.h"
  9. #include "vm/CCW.h"
  10. #include "WindowsHeaders.h"
  11. #include <roerrorapi.h>
  12. namespace il2cpp
  13. {
  14. namespace os
  15. {
  16. il2cpp_hresult_t WindowsRuntime::GetActivationFactory(Il2CppHString className, Il2CppIActivationFactory** activationFactory)
  17. {
  18. IL2CPP_ASSERT(className != NULL);
  19. IL2CPP_ASSERT(activationFactory != NULL);
  20. return RoGetActivationFactory(reinterpret_cast<HSTRING>(className), reinterpret_cast<const IID&>(Il2CppIActivationFactory::IID), reinterpret_cast<void**>(activationFactory));
  21. }
  22. il2cpp_hresult_t WindowsRuntime::CreateHStringReference(const utils::StringView<Il2CppNativeChar>& str, Il2CppHStringHeader* header, Il2CppHString* hstring)
  23. {
  24. IL2CPP_ASSERT(header != NULL);
  25. IL2CPP_ASSERT(hstring != NULL);
  26. if (str.Length() == 0)
  27. {
  28. *hstring = NULL;
  29. return S_OK;
  30. }
  31. return WindowsCreateStringReference(str.Str(), static_cast<uint32_t>(str.Length()), reinterpret_cast<HSTRING_HEADER*>(header), reinterpret_cast<HSTRING*>(hstring));
  32. }
  33. il2cpp_hresult_t WindowsRuntime::CreateHString(const utils::StringView<Il2CppChar>& str, Il2CppHString* hstring)
  34. {
  35. IL2CPP_ASSERT(str.Str() != NULL || str.Length() == 0);
  36. if (str.Length() == 0)
  37. {
  38. *hstring = NULL;
  39. return S_OK;
  40. }
  41. return WindowsCreateString(str.Str(), static_cast<uint32_t>(str.Length()), reinterpret_cast<HSTRING*>(hstring));
  42. }
  43. il2cpp_hresult_t WindowsRuntime::DuplicateHString(Il2CppHString hstring, Il2CppHString* duplicated)
  44. {
  45. return WindowsDuplicateString(reinterpret_cast<HSTRING>(hstring), reinterpret_cast<HSTRING*>(duplicated));
  46. }
  47. il2cpp_hresult_t WindowsRuntime::DeleteHString(Il2CppHString hstring)
  48. {
  49. if (hstring == NULL)
  50. return IL2CPP_S_OK;
  51. return WindowsDeleteString(reinterpret_cast<HSTRING>(hstring));
  52. }
  53. utils::Expected<const Il2CppChar*> WindowsRuntime::GetHStringBuffer(Il2CppHString hstring, uint32_t* length)
  54. {
  55. return WindowsGetStringRawBuffer(reinterpret_cast<HSTRING>(hstring), length);
  56. }
  57. utils::Expected<const Il2CppNativeChar*> WindowsRuntime::GetNativeHStringBuffer(Il2CppHString hstring, uint32_t* length)
  58. {
  59. return GetHStringBuffer(hstring, length);
  60. }
  61. utils::Expected<il2cpp_hresult_t> WindowsRuntime::PreallocateHStringBuffer(uint32_t length, Il2CppNativeChar** mutableBuffer, void** bufferHandle)
  62. {
  63. return WindowsPreallocateStringBuffer(length, mutableBuffer, reinterpret_cast<HSTRING_BUFFER*>(bufferHandle));
  64. }
  65. utils::Expected<il2cpp_hresult_t> WindowsRuntime::PromoteHStringBuffer(void* bufferHandle, Il2CppHString* hstring)
  66. {
  67. return WindowsPromoteStringBuffer(static_cast<HSTRING_BUFFER>(bufferHandle), reinterpret_cast<HSTRING*>(hstring));
  68. }
  69. utils::Expected<il2cpp_hresult_t> WindowsRuntime::DeleteHStringBuffer(void* bufferHandle)
  70. {
  71. return WindowsDeleteStringBuffer(static_cast<HSTRING_BUFFER>(bufferHandle));
  72. }
  73. Il2CppIRestrictedErrorInfo* WindowsRuntime::GetRestrictedErrorInfo()
  74. {
  75. Il2CppIRestrictedErrorInfo* errorInfo;
  76. HRESULT hr;
  77. hr = ::GetRestrictedErrorInfo(reinterpret_cast<IRestrictedErrorInfo**>(&errorInfo));
  78. if (FAILED(hr))
  79. return NULL;
  80. return errorInfo;
  81. }
  82. void WindowsRuntime::OriginateLanguageException(il2cpp_hresult_t hresult, Il2CppException* ex, Il2CppString* exceptionString, GetOrCreateFunc createCCWCallback)
  83. {
  84. utils::StringView<Il2CppNativeChar> message(utils::StringUtils::GetChars(exceptionString), utils::StringUtils::GetLength(exceptionString));
  85. Il2CppHString messageHString;
  86. Il2CppHStringHeader unused;
  87. CreateHStringReference(message, &unused, &messageHString);
  88. Il2CppIUnknown* exceptionCCW = createCCWCallback(reinterpret_cast<Il2CppObject*>(ex), Il2CppIUnknown::IID);
  89. RoOriginateLanguageException(hresult, reinterpret_cast<HSTRING>(messageHString), reinterpret_cast<IUnknown*>(exceptionCCW));
  90. exceptionCCW->Release();
  91. }
  92. void WindowsRuntime::EnableErrorReporting()
  93. {
  94. il2cpp_hresult_t hr = RoSetErrorReportingFlags(RO_ERROR_REPORTING_USESETERRORINFO);
  95. IL2CPP_ASSERT(IL2CPP_HR_SUCCEEDED(hr) && "RoSetErrorReportingFlags failed");
  96. }
  97. }
  98. }
  99. #endif