0

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.

pma07pg
  • 516
  • 1
  • 4
  • 16
  • What version of unity are you using? And the error occurs in which step? When building the project in Unity or building in VS? We recommend that you provide more details about your application running on the HoloLens2 device so that we can locate the problem or find a solution, such as the MVCE(https://stackoverflow.com/help/minimal-reproducible-example) and the steps to reproduce the issue. – Hernando - MSFT Dec 18 '19 at 07:45
  • Updated the question for you! – pma07pg Dec 18 '19 at 08:26

1 Answers1

1

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);
Sunius
  • 2,789
  • 18
  • 30
  • It worked! Only seems to work on ARM (Hololens 2) but this is totally fine with me. Thank you very much, I'd have never found this out by myself. – pma07pg Dec 19 '19 at 11:15
  • "Only seems to work on ARM" where doesn't it work and what do you mean by "doesn't work"? It should work on all architectures. – Sunius Dec 19 '19 at 16:06
  • Nope, it works. Nevermind...! Must have cocked something up when building the first time. Thanks again! – pma07pg Dec 20 '19 at 12:30