dotpcap / sharppcap

Official repository - Fully managed, cross platform (Windows, Mac, Linux) .NET library for capturing packets
1.33k stars 268 forks source link

System.DllNotFoundException: wpcap #59

Closed sbrl closed 5 years ago

sbrl commented 6 years ago

Hello again! I've found another issue.

In a different project to the one in my last issue (#33), I've just installed _SharpPcap - and I've hit upon an error - I'm attaching the stack trace to the bottom of this issue.

Looking around, I can't find a sharppcap.dll.config file anywhere.

I would have thought that on Linux I wouldn't need wpcap?

Stack Trace

Unhandled Exception:
System.DllNotFoundException: wpcap
  at (wrapper managed-to-native) SharpPcap.LibPcap.LibPcapSafeNativeMethods.pcap_findalldevs(intptr&,System.Text.StringBuilder)
  at SharpPcap.LibPcap.LibPcapLiveDeviceList.GetDevices () [0x00017] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.LibPcap.LibPcapLiveDeviceList.Refresh () [0x0000c] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.LibPcap.LibPcapLiveDeviceList..ctor () [0x0000b] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.LibPcap.LibPcapLiveDeviceList.get_Instance () [0x00007] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.CaptureDeviceList..ctor () [0x00032] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.CaptureDeviceList.get_Instance () [0x00007] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SomeProject.PacketCapturer+<EnumerateAvailableDevices>d__13.MoveNext () [0x00026] in <f1e5d62f4a874c329b34965ead8a0115>:0 
  at SomeProject.PacketCapturer.LogDeviceList () [0x00033] in <f1e5d62f4a874c329b34965ead8a0115>:0 
  at SomeProjectGUI.MainClass.Main (System.String[] args) [0x00092] in <3cdaac40bfd14c7cb2e859040ad39640>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.DllNotFoundException: wpcap
  at (wrapper managed-to-native) SharpPcap.LibPcap.LibPcapSafeNativeMethods.pcap_findalldevs(intptr&,System.Text.StringBuilder)
  at SharpPcap.LibPcap.LibPcapLiveDeviceList.GetDevices () [0x00017] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.LibPcap.LibPcapLiveDeviceList.Refresh () [0x0000c] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.LibPcap.LibPcapLiveDeviceList..ctor () [0x0000b] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.LibPcap.LibPcapLiveDeviceList.get_Instance () [0x00007] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.CaptureDeviceList..ctor () [0x00032] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SharpPcap.CaptureDeviceList.get_Instance () [0x00007] in <dc338158b63443d1985e66f367bc0c3c>:0 
  at SomeProject.PacketCapturer+<EnumerateAvailableDevices>d__13.MoveNext () [0x00026] in <f1e5d62f4a874c329b34965ead8a0115>:0 
  at SomeProject.PacketCapturer.LogDeviceList () [0x00033] in <f1e5d62f4a874c329b34965ead8a0115>:0 
  at SomeProjectGUI.MainClass.Main (System.String[] args) [0x00092] in <3cdaac40bfd14c7cb2e859040ad39640>:0 

System Details

chmorgan commented 6 years ago

Hi @sbrl. You'll want to put a sharppcap.dll.config file in place to map from wpcap to libpcap. Btw, are you using mono or dotnet on this platform? Dotnet doesn't have a way to do dll mapping at the moment so I've been considering adding runtime dll mapping but haven't had a need to use SharpPcap myself in a while so it hasn't gotten on the todo list yet.

sbrl commented 6 years ago

Ah, I see. I copied across the sharppcap.dll.config file from my old project and set it to Copy if Newer - and that seemed to fix it. I'm using Mono - I'm still very unsure about dotnet and how it's different to (or better than?) Mono.

chmorgan commented 6 years ago

Dotnet is the Microsoft official cross-platform runtime vs. mono which was an unofficial one. Mono improvements are being folded into dotnet. Once dotnet can properly provide an api to map dlls I'll be adopting that for sharppcap to resolve this issue, although that may then become incompatible with mono...

sbrl commented 6 years ago

I see. I'm not sure that Mono is past-tense yet though, as I'm still getting frequent updates and 73 pull requests were merged into Mono in the last week.

If the plan is to make this incompatible with Mono, perhaps a major version number at least would be appropriate? If not a separate package :thinking:

chmorgan commented 6 years ago

@sbrl I appreciate that feedback. Whatever solution is arrived at for dotnet we'll want to maintain mono compatibility. I'm not sure what that looks like at the moment though. If you have any ideas I'd be interested in knowing what you think.

sbrl commented 6 years ago

I'm not entirely sure how dll mapping works, actually. I'm guessing that the .dll.config file is the one that does the job, though. I find it hard to believe that dotnet would cook up a solution to the issue that's incompatible with mono.

Does bundling that .dll.config file I've got on Linux cause issues on Windows? I guess I'll have to do some testing. If not, then perhaps it can be included in the NuGet package? Again, I'm not sure how that works either.

chmorgan commented 6 years ago

@sbrl I’d accept a pr to add the .config file to the nuget package. I work with nuget so infrequently that it’s a few hours for what someone more familiar with nuget might take just a few minutes.

brianjkennedy commented 5 years ago

Not sure if this is the correct thread to add too, or if a new one should be created, so I apologize in advance. I'm getting the same error as above and I have included the .dll.config file mapping. I am actually using Unity 2018.2.15 which uses Mono and everything works fine in Windows. When I run for OSX though, I get the error above and have been stuck on this for a couple of days now. Any suggestions? I'm just trying to get a list of devices when the application loads.

<configuration>
  <dllmap dll="wpcap" target="libpcap.dylib" os="osx" />
</configuration>
public static List<string> GetDevices()
{
     var sNames = new List<string>();

     foreach (var device in CaptureDeviceList.Instance)
     {
          var live = device as LibPcapLiveDevice;
          sNames.Add(live.Interface.FriendlyName);
     }

     return sNames;
}

ERROR: wpcap at (wrapper managed-to-native) SharpPcap.LibPcap.LibPcapSafeNativeMethods.pcap_findalldevs(intptr&,System.Text.StringBuilder) at SharpPcap.LibPcap.LibPcapLiveDeviceList.GetDevices () [0x00017] in <505310bf735b4607871626bd2750b6a1>:0 at SharpPcap.LibPcap.LibPcapLiveDeviceList.Refresh () [0x00008] in <505310bf735b4607871626bd2750b6a1>:0 at SharpPcap.LibPcap.LibPcapLiveDeviceList..ctor () [0x0000b] in <505310bf735b4607871626bd2750b6a1>:0 at SharpPcap.LibPcap.LibPcapLiveDeviceList.get_Instance () [0x0000a] in <505310bf735b4607871626bd2750b6a1>:0 at SharpPcap.CaptureDeviceList..ctor () [0x0003b] in <505310bf735b4607871626bd2750b6a1>:0 at SharpPcap.CaptureDeviceList.get_Instance () [0x0000a] in <505310bf735b4607871626bd2750b6a1>:0 at Prism.Controllers.UIApplicationSettings.Initialize (Prism.Models.ApplicationConfiguration AppConfig) [0x00077] in <552486821c434d5cacd210e1a7c48db2>:0 at Prism.Scenes.SharedScene.Start () [0x000a2] in <552486821c434d5cacd210e1a7c48db2>:0

sbrl commented 5 years ago

@brianjkennedy Did you ensure that it is copying said .dll.config file to the output directory?

brianjkennedy commented 5 years ago

@sbrl It is not doing it by default so I manually have to copy it there but yes, I am placing it in the directory where the .dll resides.

Fox4148 commented 5 years ago

@brianjkennedy Is the SharpPcap.dll referenced correctly in your application?

sbrl commented 5 years ago

You can configure it to do so by selecting it in your IDE, and changing the build action from "none" to "copy if newer".

brianjkennedy commented 5 years ago

@Fox4148 I would assume so since everything works correctly on Windows.

@sbrl I'm using Unity as my IDE which opens either mono develop or visual studio as the code IDE. I am using visual studio and have that set, but that doesn't seem to fix the issue.

Fox4148 commented 5 years ago

try manually deleting bin/obj folder, maybe a dependency was broken with the importation, then rebuild

sbrl commented 5 years ago

Looks like dotnet is the implementation of .NET Core - which is mainly for ASP.NET web applications. Mono is still alive and well as your regular .NET Framework and runtime for cross-platform purposes. I've been experimenting with dotnet for my University course this semester, and ironically it looks like Mono is more cross-platform and easier to sue that dotnet........

chmorgan commented 5 years ago

@sbrl, that may be the case. I'd imagine mono would pull in as much of dotnet as possible and increase that going forward to converge the two implementation.

There is a pull request open now that supports windows and linux/Mac and shouldn't need sharppcap.dll.config for mono even. Would you mind trying it out to see how it works for you on mono?

sbrl commented 5 years ago

Absolutely!

I've just cloned the repository and opened the solution file I found in the repository root in Monodevelop. After building, I ran it with the start button. I got this error:

example03 basiccap example3 basiccap cs monodevelop_132

sbrl commented 5 years ago

Erm never mind! I just needed to run it with sudo.

chmorgan commented 5 years ago

@sbrl where is that parameter 's'? I don't see it there, is that hidden under the popup? Maybe Console.ReadLine() returned null??

sbrl commented 5 years ago

Nevermind - it looks like I was silly. I needed to run it with sudo, and now it works fine:

terminal_133

chmorgan commented 5 years ago

Hi @sbrl. closing this out as you seem to have found the solution here. We've made some big library improvements for cross platform support thanks to another developer contribution. Please try newer nuget packages if you get a chance.