MichaCo / DnsClient.NET

DnsClient.NET is a simple yet very powerful and high performant open source library for the .NET Framework to do DNS lookups
https://dnsclient.michaco.net
Apache License 2.0
781 stars 137 forks source link

Missing advapi32.dll on Windows UWP ARM64 #149

Closed camnewnham closed 2 years ago

camnewnham commented 2 years ago

Continuation from https://github.com/sipsorcery-org/sipsorcery/issues/688

I've been using DnsClient via https://github.com/sipsorcery-org/sipsorcery and run into an interesting issue. The usage here is in Unity via IL2CPP, targeting Windows Universal ARM64 (HoloLens 2).

Everything functions correctly on the target platform when using the .NET Framework build, but the .NET Standard build throws an exception originating at NameServer.ResolveNameServer, ultimately System.DllNotFoundException: Unable to load DLL 'advapi32.dll': The specified module could not be found.

System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Error resolving name servers ---> System.DllNotFoundException: Unable to load DLL 'advapi32.dll': The specified module could not be found.
  at Microsoft.Win32.Win32RegistryApi.OpenSubKey (Microsoft.Win32.RegistryKey rkey, System.String keyName, System.Boolean writable) [0x00000] in <00000000000000000000000000000000>:0 
  at Microsoft.Win32.RegistryKey.OpenSubKey (System.String name, System.Boolean writable) [0x00000] in <00000000000000000000000000000000>:0 
  at Microsoft.Win32.RegistryKey.OpenSubKey (System.String name) [0x00000] in <00000000000000000000000000000000>:0 
  at DnsClient.Windows.NameResolutionPolicy.Resolve (System.Boolean includeGenericServers, System.Boolean includeDirectAccessServers) [0x00000] in <00000000000000000000000000000000>:0 
  at DnsClient.NameServer.ResolveNameResolutionPolicyServers () [0x00000] in <00000000000000000000000000000000>:0 
  at DnsClient.NameServer.ResolveNameServers (System.Boolean skipIPv6SiteLocal, System.Boolean fallbackToGooglePublicDns) [0x00000] in <00000000000000000000000000000000>:0 
  at DnsClient.LookupClient..ctor (DnsClient.LookupClientOptions options, DnsClient.DnsMessageHandler udpHandler, DnsClient.DnsMessageHandler tcpHandler) [0x00000] in <00000000000000000000000000000000>:0 
  at DnsClient.LookupClient..ctor (DnsClient.LookupClientOptions options) [0x00000] in <00000000000000000000000000000000>:0 
  at DnsClient.LookupClient..ctor () [0x00000] in <00000000000000000000000000000000>:0 
  at SIPSorcery.Net.RtpIceChannel.StartGathering () [0x00000] in <00000000000000000000000000000000>:0 

I'm not particularly familiar with the Windows APIs in question, but thoughts would be:

Related issues (unable to access advapi.dll on this Windows flavour):

A possible solution could be to check the proccessor architecture if running on windows to determine if running on ARM/ARM64.

Alternatively just putting the calls that require this library (OpenSubKey) in a try/catch and reverting to a fallback code path could work?

Any thoughts? Happy to do some further investigation/testing.

camnewnham commented 2 years ago

This PR implements the second solution, simply wrapping Registry.LocalMachine.OpenSubKey in a try/catch for DllNotFoundException, and resolves the issue in my scenario.

MichaCo commented 2 years ago

Hi @camnewnham, Thanks for reporting the issue, that's a bit unfortunate how that Windows registry Nuget doesn't handle errors by itself...

I'll have a look into that. Thanks for the PR, too btw. Might take a bit until that gets released though

Cheesebaron commented 2 years ago

This is also an issue for Xamarin.iOS targets. It fails linking.

MichaCo commented 2 years ago

@Cheesebaron & @camnewnham, this should be fixed in 1.6.1 which just released.