WindowsRuntime.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include "il2cpp-vm-support.h"
  3. #include "os/WindowsRuntime.h"
  4. #include "vm/Exception.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. IL2CPP_VM_RAISE_IF_FAILED(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. IL2CPP_VM_RAISE_IF_FAILED(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. IL2CPP_VM_RAISE_IF_FAILED(hr, false);
  32. return result;
  33. }
  34. static inline void DeleteHString(Il2CppHString hstring)
  35. {
  36. il2cpp_hresult_t hr = os::WindowsRuntime::DeleteHString(hstring);
  37. IL2CPP_VM_RAISE_IF_FAILED(hr, false);
  38. }
  39. static inline Il2CppString* HStringToManagedString(Il2CppHString hstring)
  40. {
  41. return os::WindowsRuntime::HStringToManagedString(hstring);
  42. }
  43. static inline void* PreallocateHStringBuffer(uint32_t length, Il2CppNativeChar** buffer)
  44. {
  45. void* bufferHandle;
  46. il2cpp_hresult_t hr = os::WindowsRuntime::PreallocateHStringBuffer(length, buffer, &bufferHandle);
  47. IL2CPP_VM_RAISE_IF_FAILED(hr, false);
  48. return bufferHandle;
  49. }
  50. static inline Il2CppHString PromoteHStringBuffer(void* bufferHandle)
  51. {
  52. Il2CppHString hstring;
  53. il2cpp_hresult_t hr = os::WindowsRuntime::PromoteHStringBuffer(bufferHandle, &hstring);
  54. if (IL2CPP_HR_FAILED(hr))
  55. {
  56. // Prevent memory leaks by deleting the hstring buffer that was supposed to be promoted before raising an exception
  57. os::WindowsRuntime::DeleteHStringBuffer(bufferHandle);
  58. IL2CPP_VM_RAISE_COM_EXCEPTION(hr, false);
  59. }
  60. return hstring;
  61. }
  62. static void MarshalTypeToNative(const Il2CppType* type, Il2CppWindowsRuntimeTypeName& nativeType);
  63. static const Il2CppType* MarshalTypeFromNative(Il2CppWindowsRuntimeTypeName& nativeType);
  64. static inline void DeleteNativeType(Il2CppWindowsRuntimeTypeName& nativeType)
  65. {
  66. DeleteHString(nativeType.typeName);
  67. }
  68. };
  69. }
  70. }