PlatformInvoke.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #include "il2cpp-blob.h"
  4. #include "il2cpp-runtime-metadata.h"
  5. #include "il2cpp-object-internals.h"
  6. #include "vm/Array.h"
  7. #include "vm/Class.h"
  8. #include "vm/MarshalAlloc.h"
  9. #include "vm/Object.h"
  10. #include "vm/String.h"
  11. #include "utils/StringView.h"
  12. #include <vector>
  13. #include <string>
  14. struct Il2CppString;
  15. struct Il2CppStringBuilder;
  16. struct PInvokeArguments;
  17. namespace il2cpp
  18. {
  19. namespace vm
  20. {
  21. class LIBIL2CPP_CODEGEN_API PlatformInvoke
  22. {
  23. public:
  24. static void SetFindPluginCallback(Il2CppSetFindPlugInCallback method);
  25. static Il2CppMethodPointer Resolve(const PInvokeArguments& pinvokeArgs);
  26. static void MarshalFree(void* ptr);
  27. static char* MarshalCSharpStringToCppString(Il2CppString* managedString);
  28. static void MarshalCSharpStringToCppStringFixed(Il2CppString* managedString, char* buffer, int numberOfCharacters);
  29. static Il2CppChar* MarshalCSharpStringToCppWString(Il2CppString* managedString);
  30. static void MarshalCSharpStringToCppWStringFixed(Il2CppString* managedString, Il2CppChar* buffer, int numberOfCharacters);
  31. static il2cpp_hresult_t MarshalCSharpStringToCppBStringNoThrow(Il2CppString* managedString, Il2CppChar** bstr);
  32. static Il2CppChar* MarshalCSharpStringToCppBString(Il2CppString* managedString);
  33. static Il2CppString* MarshalCppStringToCSharpStringResult(const char* value);
  34. static Il2CppString* MarshalCppWStringToCSharpStringResult(const Il2CppChar* value);
  35. static Il2CppString* MarshalCppBStringToCSharpStringResult(const Il2CppChar* value);
  36. static void MarshalFreeBString(Il2CppChar* value);
  37. static char* MarshalEmptyStringBuilder(Il2CppStringBuilder* stringBuilder);
  38. static Il2CppChar* MarshalEmptyWStringBuilder(Il2CppStringBuilder* stringBuilder);
  39. static char* MarshalStringBuilder(Il2CppStringBuilder* stringBuilder);
  40. static Il2CppChar* MarshalWStringBuilder(Il2CppStringBuilder* stringBuilder);
  41. static void MarshalStringBuilderResult(Il2CppStringBuilder* stringBuilder, char* buffer);
  42. static void MarshalWStringBuilderResult(Il2CppStringBuilder* stringBuilder, Il2CppChar* buffer);
  43. #if !IL2CPP_TINY
  44. static intptr_t MarshalDelegate(Il2CppDelegate* d);
  45. static Il2CppDelegate* MarshalFunctionPointerToDelegate(void* functionPtr, Il2CppClass* delegateType);
  46. static bool IsFakeDelegateMethodMarshaledFromNativeCode(const Il2CppDelegate* d);
  47. #else
  48. static intptr_t MarshalDelegate(Il2CppDelegate* d) { IL2CPP_ASSERT(false && "This should not be called on tiny"); return 0; }
  49. static Il2CppDelegate* MarshalFunctionPointerToDelegate(void* functionPtr, Il2CppClass* delegateType) { IL2CPP_ASSERT(false && "This should not be called on tiny"); return NULL; }
  50. static bool IsFakeDelegateMethodMarshaledFromNativeCode(const Il2CppDelegate* d) { IL2CPP_ASSERT(false && "This should not be called on tiny"); return NULL; }
  51. #endif
  52. template<typename T>
  53. static T* MarshalAllocateStringBuffer(size_t numberOfCharacters)
  54. {
  55. return (T*)MarshalAlloc::Allocate(numberOfCharacters * sizeof(T));
  56. }
  57. private:
  58. static char* MarshalEmptyStringBuilder(Il2CppStringBuilder* stringBuilder, size_t& stringLength, std::vector<std::string>& utf8Chunks, std::vector<Il2CppStringBuilder*>& builders);
  59. static Il2CppChar* MarshalEmptyWStringBuilder(Il2CppStringBuilder* stringBuilder, size_t& stringLength);
  60. };
  61. } /* namespace vm */
  62. } /* namespace il2cpp */