Version: 2023.1
Language : English
Author native UWP plug-ins
Debug UWP applications with IL2CPP

Use P/Invoke

P/Invoke is a technology that allows you to access structs, callbacks, and functions in native code from your managed code. The default calling convention for P/Invoke functions on x86 is __stdcall. For more information, refer to Microsoft documentation on P/Invoke.

P/invoke marshaling rules

P/Invoke marshaling rules are the same as those for .NET marshaling. However, Unity doesn’t support the following types:

  • AnsiBStr
  • Currency
  • SAFEARRAY
  • IDispatch
  • TBStr
  • VBByRefStr

P/invoke limitations

On Universal Windows PlatformAn IAP feature that supports Microsoft’s In App Purchase simulator, which allows you to test IAP purchase flows on devices before publishing your application. More info
See in Glossary
(UWP), you can’t specify the dynamic-link library (DLL) name to P/Invoke into specific system libraries. If you try to P/Invoke into any DLL that exists outside of the project, it will result in a DllNotFoundException at runtime. Therefore, it’s possible to use the __Internal keyword in place of the DLL name, which will use the C++ linker to resolve the functions when you build your project, rather than when you load them at runtime:

    [DllImport("__Internal")]
    private static extern int CountLettersInString([MarshalAs(UnmanagedType.LPWStr)]string str);

If you make an error when you declare a function in your managed code, it will produce a linker error, rather than an error at runtime. This means that no dynamic loading needs to take place at runtime and the function is called directly, which decreases the overhead of a P/Invoke call.

Additional resources

Author native UWP plug-ins
Debug UWP applications with IL2CPP
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961