unity3d - 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.
解决方案
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);
推荐阅读
- python - 视图 app_name.views.signup 未返回 HttpResponse 对象。它返回 None 。对于现有用户名
- angularjs - 使用模态控制器访问主控制器方法
- javascript - 定期更新 iFrame 中的 CSS
- r - R按id计算日期变量的连续数
- wordpress - 如何使用 WPML 翻译 WPcalender WordPress 插件表单标签?
- c# - 为已部署的 Windows 应用程序 C# 更新系统?
- f# - seq<'a> 类型与 Collections.Generic.IEnumerable<'a> 类型不兼容
- php - 如果值存在php mysql如何检查行
- javascript - 如何在 TinyMCE 编辑器中以编程方式添加 html 标签?
- javascript - 下载表单 URL 并在 javascript 中上传到 Google Drive