Packet Sniffer SDK VCL Edition

HNFileAdapter :: Methods :: OpenFile

 Previous Next

Opens the CAP file.

Syntax:

function OpenFile(FileName: String) : DWORD;

Parameters:

FileName

[in] A path to a file.

Return values:

HNERR_OKCAP file opening has been completed successfully.
HNERR_INCOMPATIBLE_MODEIncompatible mode of HNFileAdapter. Occurs when HNFileAdapter.AutoMode was set, but HNQueue object was not predetermined (see HNFileAdapter.Queue).
HNERR_MEM_ALLOC_ERRORThe memory for the traffic processing is not allocated. This can happen either because of the insufficient operating system resources or in case of HNQueue component using (see HNQueue.AllocItems).
HNERR_OPEN_FILECAP file opening error.
HNERR_READ_FILEFile reading error.
HNERR_BAD_FILE_FORMATUnknown CAP file format.
HNERR_FORMAT_VERThis version of CAP file is not supported.
HNERR_BAD_MEDIUM_TYPEThe adapter type, in format of which packets was dumped to CAP file, is not supported.
HNERR_ADAPTER_THREAD_ERRPacket Sniffer SDK internal thread creating error.

Description:

Use OpenFile to open an existing CAP file. If the function succeeds, you can begin to dump network traffic to this CAP file using HNFileAdapter.Get_NextPacket function.

When HNFileAdapter object works in AutoMode using of HNFileAdapter.Get_NextPacket function is impossible.

If HNFileAdapter.Queue property was set before the network adapter opening, and HNFileAdapter object Automode was set (see HNFileAdapter.AutoMode), then the traffic will be processed in the HNFileAdapter internal thread context together with HNQueue component.

Also you may gather traffic statistics and filter CAP file content if you're using BPF filter (see. HNFileAdapter.UserFilter, HNFileAdapter.Get_RecvCount, HNFileAdapter.Get_AcceptCount, HNFileAdapter.Get_RejectCount, HNFileAdapter.Get_ProcessCount).

On executing HNFileAdapter.OpenFile function HNFileAdapter component attempts to close the CAP file which was opened earlier. So, one instance of HNFileAdapter object can open only one CAP file.

If you need to open/create several CAP files simultaneously, you may create several HNFileAdapter objects with appropriate settings.

To create a new CAP file please use HNFileAdapter.CreateFile function.

Example of using HNFileAdpater object AutoMode for reading packets from the first CAP file and dumping packets to the second CAP file:

// OnFileClose event handler
procedure TForm1.HNFileAdapter1FileClose(Sender: TObject; ThParam: Pointer; Result: DWORD)
begin
    if (Result == HNERR_FILE_SIZE_LIMIT)
    begin
      ...
    end;
end;
...
// Create the first HNFileAdapter object
HNFileAdapter1 = HNFileAdapter.Create(); 
// Create the second HNFileAdapter object
HNFileAdapter2 = HNFileAdapter.Create(); 
// Create HNQueue object
HNQueue1 = HNQueue.Create(); 
...
// Set HNQueue object to HNFileAdapter objects
HNFileAdapter1.Queue := HNQueue1.Handle;
HNFileAdapter2.Queue := HNQueue1.Handle;

// HNFileAdapter additional settings
HNFileAdapter1.AutoMode := TRUE;
HNFileAdapter2.AutoMode := TRUE;

HNFileAdapter1.SizeLimit := 1000000;
HNFileAdapter1.OnFileClose := TForm1.HNFileAdapter1FileClose;

// Create CAP file
Res = HNFileAdapter1.CreateFile("packets2.cap");
...
// Start internal queue thread
Res = HNQueue1.Start();
...
// Open CAP file
Res = HNFileAdapter2.OpenFile("packets1.cap");
...
See also:HNFileAdapter, HNQueue, HNFileAdapter.Queue, HNFileAdapter.AutoMode, HNFileAdapter.UserFilter, HNFileAdapter.Get_RecvCount, HNFileAdapter.Get_AcceptCount, HNFileAdapter.Get_RejectCount, HNFileAdapter.Get_ProcessCount HNFileAdapter.CreateFile