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;