【问题标题】:IL2CPP compiled UWP app using Windows System DLLs on ARMIL2CPP 使用 ARM 上的 Windows 系统 DLL 编译 UWP 应用程序
【发布时间】:2020-04-10 04:30:03
【问题描述】:

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

当我编译 Unity3d 项目时,它不起作用。 dll 会在应用启动时由 Hololens 从

自动加载

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 应用上,它可以工作。

【问题讨论】:

  • 你使用的是什么版本的统一?错误发生在哪一步?在 Unity 中构建项目或在 VS 中构建项目时?我们建议您提供有关在 HoloLens2 设备上运行的应用程序的更多详细信息,以便我们定位问题或找到解决方案,例如 MVCE(stackoverflow.com/help/minimal-reproducible-example) 和重现问题的步骤。
  • 为你更新了问题!

标签: unity3d uwp dllimport il2cpp


【解决方案1】:

在 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);

【讨论】:

  • 成功了!似乎只适用于 ARM(Hololens 2),但这对我来说完全没问题。非常感谢,我自己永远不会发现这一点。
  • “似乎只能在 ARM 上工作”在哪里不起作用,“不起作用”是什么意思?它应该适用于所有架构。
  • 不,它有效。没关系...!第一次建造时一定有什么问题。再次感谢!
猜你喜欢
  • 2016-02-07
  • 2015-09-12
  • 2021-10-02
  • 2015-04-20
  • 1970-01-01
  • 2015-12-15
  • 2018-11-28
  • 2011-06-01
  • 2020-10-17
相关资源
最近更新 更多