IL2CPP 在 ARM 上使用 Windows 个系统 DLL 编译 UWP 应用程序

IL2CPP compiled UWP app using Windows System DLLs on ARM

我正在尝试在 Hololens 2 上的 Unity3d UWP 应用程序中使用 iphlpapi.dll。当我创建一个独立的 UWP C# 应用程序并在 HL 上 运行 它时,它工作正常,我可以使用dll.

当我编译Unity3d项目时,它不起作用。该 dll 由

的 Hololens 在 App 启动时自动加载

C:\Windows\SysArm\

但是我在独立 UWP 应用程序中成功使用的 DLLImport 不起作用。

抱歉缺少代码,我不在办公室,但我使用的示例来自这里:

C# - Get mac address in Universal Apps

任何指点都会很有帮助!

非常感谢, 彼得

编辑:

@赫尔南多

一旦构建了 IL2CPP,HL 设备上的 运行 时间就会出现问题。 Unity的版本是2018.4.12f1。当代码尝试使用 DLLImport:

时发生错误

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

异常是未找到 DLL,但在具有相同代码的独立 UWP 应用程序上,它可以工作。

在 UWP 上,您不能动态加载系统库。 LoadLibraryExW 仅适用于桌面应用程序。看起来某些 DllImports 到本机系统库中是可行的,但这只是 il2cpp 在 "UNITY_INSTALL_DIR\Editor\Data\il2cpp\libil2cpp\os\Win32\LibraryLoader.cpp" 中对几个流行的本机 API 进行硬编码的诡计。由于 GetAdaptersInfo 不是其中的一部分,因此它失败了。

.NET Native 工作的原因是因为它在构建时链接到 DLL 中。如果您删除 ExactSpelling 属性:

,它甚至会输出警告
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.

如果你想让 IL2CPP 做同样的事情,请将 P/Invoke 签名更改为:

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