首页 > 解决方案 > IL2CPP compiled UWP app using Windows System DLLs on ARM

问题描述

I am trying to use the iphlpapi.dll within a Unity3d UWP App on the Hololens 2. When I create a standalone UWP C# App and run it on the HL, it works fine and I can use the dll.

When I compile the Unity3d project, it doesn't work. The dll is loaded automatically on App start by the Hololens from

C:\Windows\SysArm\

but the DLLImport I've used successfully in the standalone UWP App doesn't work.

Apologies for the lack of code, I'm no longer in the office but the example I've used is from here:

C# - Get mac address in Universal Apps

Any pointers would be very helpful!

Many thanks, Peter

EDIT:

@Hernando

The problem occurs in runtime on the HL device once the IL2CPP has built. The version of Unity is 2018.4.12f1. The error occurs as the code attempts to use the DLLImport:

[DllImport("iphlpapi.dll", CharSet = CharSet.Ansi, ExactSpelling = true)] private static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref Int64 pBufOutLen);

The exception is DLL not found but on the standalone UWP app with identical code, it works.

标签: unity3duwpdllimportil2cpp

解决方案


On UWP, you are not allowed to dynamically load system libraries. LoadLibraryExW is only available for desktop applications. It might seem that some DllImports into native system libraries work, but it's merely trickery by il2cpp hard coding several popular native APIs in "UNITY_INSTALL_DIR\Editor\Data\il2cpp\libil2cpp\os\Win32\LibraryLoader.cpp". Since GetAdaptersInfo is not part of it, it fails.

The reason .NET Native works is because it links in the DLL at build time. It even outputs a warning about it if you remove ExactSpelling property:

warning MCG0007: Unresolved P/Invoke method 'iphlpapi.dll!GetAdaptersInfo' for method 'System.Int32 App.MainPage.GetAdaptersInfo(System.IntPtr, System.Int64)'. Calling this method would throw exception at runtime. Please make sure the P/Invoke either points to a Windows API allowed in UWP applications, or a native DLL that is part of the package. If for some reason your P/Invoke does not satisfy those requirements, please use [DllImport(ExactSpelling=true) to indicate that you understand the implications of using non-UWP APIs.

If you want to make IL2CPP do the same, change the P/Invoke signature to this:

[DllImport("__Internal", CharSet = CharSet.Ansi, ExactSpelling = true)]
 private static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref Int64 pBufOutLen);

推荐阅读