免安装WinPcap的方式建立应用程序或者安装程序

来源:互联网 发布:无主之地2mac汉化包 编辑:程序博客网 时间:2024/04/27 20:25
  •  
    2007-05-17 19:01:20
      前段时间使用WinPcap开发了一个捕获分析软件。还算顺利,但是在制作安装包的时候遇到一些问题。我当然不愿意让别人使用我的软件的时候安装两次,最好免安装绿色版,后来经过分析。发现一些规律,我使用Delphi7+WinPcap3.1可以实现绿色免安装使用。之前在网上看到很多都不太实用。在Xp和2000下测试成功。
       下面介绍我是如何制作绿色版本的。
     
    1)准备WinPcap的文件
       首先在可以先安装WinPcap3.1(好像最新到了4.0了,不过3.1稳定),然后在
    把%SystemRoot%System32目录下:
        Packet.dll,wpcap.dll,WanPacket.dll,pthreadVC.dll
    和%SystemRoot%System32/drivers目录下:
       npf.sys
    还有Winpcap安装目录下:
      daemon_mgm.exe,NetMonInstaller.exe,npf_mgm.exe,rpcapd.exe
    共9个文件复制到应用程序根目录。
     
    2)在应用程序中编写代码

    procedure TMonForm.FormCreate(Sender: TObject);
    var 

        lpBuf:array[0..254] of Char;
        ls_Driver : String;
        ls_NpfFile:String;
        ls_Lcname:String;
        ls_windows:String;
    begin

       //获取%SystemRoot%的目录

       GetWindowsDirectory(lpBuf,254);
       ls_windows := StrPas(lpbuf);
       //获取系统目录%SystemRoot%System32
       GetSystemDirectory(lpBuf,254);
       GS_SystemDir := StrPas(lpbuf);

     

       //复制npf.sys到系统目录,并删除假目录,有时候会出现目录名和文件

       //同名导致应用程序启动后看不到网卡
       ls_Driver  := GS_SystemDir + '/drivers';
       ls_NpfFile := ls_Driver + '/npf.sys';
       ls_Lcname  := ExtractFilePath(Application.ExeName) + 'npf.sys';
       RemoveDirectory(PChar(ls_NpfFile));
       RemoveDirectory(PChar(GS_SystemDir + '/Packet.dll'));
       RemoveDirectory(PChar(GS_SystemDir + '/WanPacket.dll'));
       RemoveDirectory(PChar(GS_SystemDir + '/wpcap.dll'));
       binPath := ExtractFilePath(Application.ExeName) ; //应用程序根目录
      
       //判断是否存在该系统文件
       if not FileExists(ls_Lcname) then
       begin
         Application.MessageBox(Pchar('丢失系统文件!'),

                  PChar('错误'), MB_OK + MB_ICONSTOP);
         Application.Terminate;
       end;


       RemoveDirectory(PChar(ls_NpfFile));
       CopyFile(PChar(ls_Lcname),PChar(ls_NpfFile),false);
       CopyFile(PChar(ExtractFilePath(Application.ExeName)

                + 'Packet.dll'),PChar(GS_SystemDir + '/Packet.dll'),false);
       CopyFile(PChar(ExtractFilePath(Application.ExeName)

                + 'WanPacket.dll'),PChar(GS_SystemDir + '/WanPacket.dll'),

                false);
       CopyFile(PChar(ExtractFilePath(Application.ExeName) + 'wpcap.dll'),
                PChar(GS_SystemDir + '/wpcap.dll'),false);
       CopyFile(PChar(ExtractFilePath(Application.ExeName)

                + 'pthreadVC.dll'),PChar(GS_SystemDir + '/pthreadVC.dll'),

                 false);
       CopyFile(PChar(ls_Lcname),PChar(ls_NpfFile),false);
       
        //这是关键部分--->启动捕获服务
        WinExec(PChar(binPath + '/NetMonInstaller.exe u'),SW_HIDE);
        WinExec(PChar(binPath + '/daemon_mgm.exe -u'),SW_HIDE);
        WinExec(PChar(binPath + '/npf_mgm.exe -u'),SW_HIDE);

        WinExec(PChar(binPath + '/npf_mgm.exe -r'),SW_HIDE);
        WinExec(PChar(binPath + '/daemon_mgm.exe -r'),SW_HIDE);
        WinExec(PChar(binPath + '/NetMonInstaller.exe i'),SW_HIDE);


        //启动显示界面

        //......下面就是应用程序代码,完全可以实现免安装和一体化打包
    end;