WindowsRuntime.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include "os/WindowsRuntime.h"
  3. #include "vm/Exception.h"
  4. #include "vm/String.h"
  5. #include "utils/StringView.h"
  6. struct Il2CppIActivationFactory;
  7. namespace il2cpp
  8. {
  9. namespace vm
  10. {
  11. class LIBIL2CPP_CODEGEN_API WindowsRuntime
  12. {
  13. public:
  14. static Il2CppIActivationFactory* GetActivationFactory(const utils::StringView<Il2CppNativeChar>& runtimeClassName);
  15. static inline void CreateHStringReference(const utils::StringView<Il2CppNativeChar>& str, Il2CppHStringHeader* header, Il2CppHString* hstring)
  16. {
  17. il2cpp_hresult_t hr = os::WindowsRuntime::CreateHStringReference(str, header, hstring);
  18. vm::Exception::RaiseIfFailed(hr, false);
  19. }
  20. static inline Il2CppHString CreateHString(Il2CppString* str)
  21. {
  22. Il2CppHString result;
  23. il2cpp_hresult_t hr = os::WindowsRuntime::CreateHString(utils::StringView<Il2CppChar>(str->chars, str->length), &result);
  24. vm::Exception::RaiseIfFailed(hr, false);
  25. return result;
  26. }
  27. static inline Il2CppHString CreateHString(const utils::StringView<Il2CppNativeChar>& str)
  28. {
  29. Il2CppHString result;
  30. il2cpp_hresult_t hr = os::WindowsRuntime::CreateHString(str, &result);
  31. vm::Exception::RaiseIfFailed(hr, false);
  32. return result;
  33. }
  34. static inline void DeleteHString(Il2CppHString hstring)
  35. {
  36. il2cpp_hresult_t hr = os::WindowsRuntime::DeleteHString(hstring);
  37. vm::Exception::RaiseIfFailed(hr, false);
  38. }
  39. static inline Il2CppString* HStringToManagedString(Il2CppHString hstring)
  40. {
  41. if (hstring == NULL)
  42. return vm::String::Empty();
  43. uint32_t length;
  44. auto result = os::WindowsRuntime::GetHStringBuffer(hstring, &length);
  45. vm::Exception::RaiseIfError(result.GetError());
  46. return vm::String::NewUtf16(result.Get(), length);
  47. }
  48. static inline void* PreallocateHStringBuffer(uint32_t length, Il2CppNativeChar** buffer)
  49. {
  50. void* bufferHandle;
  51. auto hr = os::WindowsRuntime::PreallocateHStringBuffer(length, buffer, &bufferHandle);
  52. vm::Exception::RaiseIfError(hr.GetError());
  53. vm::Exception::RaiseIfFailed(hr.Get(), false);
  54. return bufferHandle;
  55. }
  56. static inline Il2CppHString PromoteHStringBuffer(void* bufferHandle)
  57. {
  58. Il2CppHString hstring;
  59. auto hr = os::WindowsRuntime::PromoteHStringBuffer(bufferHandle, &hstring);
  60. vm::Exception::RaiseIfError(hr.GetError());
  61. if (IL2CPP_HR_FAILED(hr.Get()))
  62. {
  63. // Prevent memory leaks by deleting the hstring buffer that was supposed to be promoted before raising an exception
  64. os::WindowsRuntime::DeleteHStringBuffer(bufferHandle);
  65. vm::Exception::Raise(hr.Get(), false);
  66. }
  67. return hstring;
  68. }
  69. static void MarshalTypeToNative(const Il2CppType* type, Il2CppWindowsRuntimeTypeName& nativeType);
  70. static const Il2CppType* MarshalTypeFromNative(Il2CppWindowsRuntimeTypeName& nativeType);
  71. static inline void DeleteNativeType(Il2CppWindowsRuntimeTypeName& nativeType)
  72. {
  73. DeleteHString(nativeType.typeName);
  74. }
  75. };
  76. }
  77. }