`
923723914
  • 浏览: 635039 次
文章分类
社区版块
存档分类
最新评论

在 Visual Studio 2010 中配置SharpPcap

 
阅读更多

最近需要在C#下写一个抓取ARP包的程序,找了好几天,选择了用SharpPcap来做。它的配置简单,功能比较强大。

SharpPcap教程

我在配置的过程中遇到了一些问题,现在把这些问题的解决方法写下来,以免以后忘了,又开始各种痛苦的调试。

先来看看我的环境:win7旗舰版 、VS2010旗舰版、WinPcap4.1.3、SharpPcap4.2.0。

1.安装Winpcap4.1.3(WinPcap4.1.3下载)

2.解压SharpPcap-4.2.0.bin.zip(SharpPcap4.2.0.bin.zip&&SharpPcap4.2.0.src.zip下载)解压后打开debug文件夹,可以看到里面有两个dll文件,这就是我们程序中要用到的东西。SharpPcap-4.2.0.src.zip压缩包中,包含SharpPcap的所有源代码和一些示例程序。

3.打开VS2010,新建一个C#的控制台项目。

4.然后单击“项目”下拉菜单,选择 “添加引用”,在弹出的对话框中单击 “浏览” 选项卡,然后选择第2步中SharpPcap-4.2.0.bin.zip解压后的路径,然后将debug中的SharpPcap.dll添加进去。

5.将下面的代码,粘贴到你的项目中,测试配置是否成功,如果成功则会显示你的网络适配器的信息。

using System;
using System.Collections.Generic;

using SharpPcap;

namespace Example1
{
    /// <summary>
    /// Obtaining the device list
    /// </summary>
    public class IfListAdv
    {
        /// <summary>
        /// Obtaining the device list
        /// </summary>
        public static void Main(string[] args)
        {
            // Print SharpPcap version
            string ver = SharpPcap.Version.VersionString;
            Console.WriteLine("SharpPcap {0}, Example1.IfList.cs", ver);

            // Retrieve the device list
            var devices = CaptureDeviceList.Instance;

            // If no devices were found print an error
            if(devices.Count < 1)
            {
                Console.WriteLine("No devices were found on this machine");
                return;
            }

            Console.WriteLine("\nThe following devices are available on this machine:");
            Console.WriteLine("----------------------------------------------------\n");

            /* Scan the list printing every entry */
            foreach(var dev in devices)
                Console.WriteLine("{0}\n",dev.ToString());

            Console.Write("Hit 'Enter' to exit...");
            Console.ReadLine();
        }
    }
}


需要注意的问题:

1.如果你要用SharpPcap3.5,那么你在新建项目时.Net FrameWork 要选成3.5,(vs2010默认是4.0),否则运行时会出现错误。

2.如果你用的是SharpPcap高版本的dll,测试示例程序时最好不要用低版本的,否则可能会出错。楼主测试的时候,dll用的是SharpPcap4.2的,示例程序用的是SharpPcap3.5的,生成时会出现错误。



分享到:
评论
1 楼 dcode 2013-12-14  
楼主写的不错,正好遇到点问题,看着你的文章解决了,感谢分享。。。

相关推荐

Global site tag (gtag.js) - Google Analytics