加载动态库的问题。加载库部分有效,但 GetProcAddress 始终返回 nil。下面我写的小简单。如果 pcap_findalldevs 为 nil,它会打印出来,在我的情况下它总是 nil 并且与任何其他 libpcap 函数相同的问题。
program Project7;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, pcap;
var
h: THandle;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
begin
h := LoadLibrary('libpcap.dylib');
if h <> 0 then
begin
pcap_findalldevs := GetProcAddress(h, 'pcap_findalldevs');
Writeln('Is pcap_findalldevs loaded ' +
BoolToStr(Assigned(pcap_findalldevs)));
Readln;
end;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
我不确定是否需要 Pcap 单元源,所以我还是要发布它。
unit Pcap;
interface
uses
{$IFDEF WIN32}
Windows, WinSock, {$IFEND} Bpf, System.SysUtils;
// ----------------------------------------------------------------------------
// Constant Definition
// ----------------------------------------------------------------------------
const
PCAP_VERSION_MAJOR = 2;
PCAP_VERSION_MINOR = 4;
PCAP_ERRBUF_SIZE = 256;
PCAP_IF_LOOPBACK = $00000001; { interface is loopback }
RPCAP_RMTAUTH_NULL = 0;
RPCAP_RMTAUTH_PWD = 1;
PCAP_OPENFLAG_PROMISCUOUS = 1;
PCAP_OPENFLAG_SERVEROPEN_DP = 2;
PCAP_OPENFLAG_UDP_DP = 4;
MODE_CAPT = 0;
MODE_STAT = 1;
MODE_MON = 2;
// ----------------------------------------------------------------------------
// Type Definition
// ----------------------------------------------------------------------------
type
u_char = Char;
u_short = Word;
u_int = LongWord;
u_long = LongWord;
pu_char = ^byte;
pu_short = ^Word;
pu_int = ^LongWord;
pu_long = ^LongWord;
SunB = record
s_b1, s_b2, s_b3, s_b4: u_char;
end;
SunW = record
s_w1, s_w2: u_short;
end;
PInAddr = ^TInAddr;
TInAddr = record
case integer of
0:
(S_un_b: SunB);
1:
(S_un_w: SunW);
2:
(S_addr: u_long);
end;
in_addr = TInAddr;
PSockAddrIn = ^TSockAddrIn;
TSockAddrIn = record
case integer of
0:
(sin_family: Word;
sin_port: Word;
sin_addr: TInAddr;
sin_zero: array [0 .. 7] of Char);
1:
(sa_family: Word;
sa_data: array [0 .. 13] of Char)
end;
sockaddr_in = TSockAddrIn;
ppcap_t = ^pcap_t;
pcap_t = integer;
pbpf_u_int32 = ^bpf_u_int32;
bpf_u_int32 = integer; // gilgil temp 2003.07.20
long = integer;
// u_int = LongWord;
ppchar = ^pAnsichar; // gilgil temp 2003.07.20
ppcap_addr_t = ^pcap_addr_t;
pcap_addr_t = packed record
next: ppcap_addr_t;
addr: PSockAddrIn; { address }
netmask: PSockAddrIn; { netmask for that address }
broadaddr: PSockAddrIn; { broadcast address for that address }
dstaddr: PSockAddrIn; { P2P destination address for that address }
end;
ppcap_if_t = ^pcap_if_t;
pppcap_if_t = ^ppcap_if_t;
pcap_if_t = packed record
next: ppcap_if_t;
name: pAnsichar;
description: pAnsichar;
address: ppcap_addr_t;
flags: bpf_u_int32;
end;
ppkt_header = ^pkt_header;
pkt_header = integer; // gilgil temp 2003.07.20
ppcap_rmtauth = ^pcap_rmtauth;
pcap_rmtauth = packed record
type_: integer;
username: pAnsichar;
password: pAnsichar;
end;
timeval = packed record
tv_sec: long;
tv_usec: long;
end;
ppcap_pkthdr = ^pcap_pkthdr;
pcap_pkthdr = packed record
ts: timeval;
caplen: bpf_u_int32;
len: bpf_u_int32 end;
ppcap_dumper_t = ^pcap_dumper_t;
pcap_dumper_t = integer; // gilgil temp 2003.07.26
pcap_handler =
procedure(p: pAnsichar; header: ppkt_header; data: pAnsichar);
cdecl;
// ----------------------------------------------------------------------------
// Function Definition
// ----------------------------------------------------------------------------
var
pcap_findalldevs: function(alldevs: pppcap_if_t; errbuf: pAnsichar)
: integer; cdecl;
pcap_freealldevs: procedure(alldevs: ppcap_if_t); cdecl;
pcap_open_live: function(device: pAnsichar; snaplen: integer;
promisc: integer; to_ms: integer; ebuf: pAnsichar): ppcap_t; cdecl;
pcap_close: procedure(p: ppcap_t); cdecl;
pcap_loop: function(p: ppcap_t; cnt: integer; ppcap_handler: pointer;
user: pAnsichar): integer; cdecl;
pcap_next_ex: function(p: ppcap_t; pkt_header: ppcap_pkthdr;
pkt_data: ppchar): integer; cdecl;
pcap_lookupnet: function(device: pAnsichar; netp: pbpf_u_int32;
maskp: pbpf_u_int32; errbuf: pAnsichar): integer; cdecl;
pcap_compile: function(p: ppcap_t; fp: pbpf_program; str: pAnsichar;
optimize: integer; netmask: bpf_u_int32): integer; cdecl;
pcap_setfilter: function(p: ppcap_t; fp: pbpf_program): integer; cdecl;
pcap_geterr: function(p: ppcap_t): pAnsichar; cdecl;
pcap_dump_open: function(p: ppcap_t; fname: pAnsichar)
: ppcap_dumper_t; cdecl;
pcap_dump: procedure(p: ppcap_dumper_t; h: ppcap_pkthdr;
sp: pAnsichar); cdecl;
pcap_dump_close: procedure(p: ppcap_dumper_t); cdecl;
pcap_sendpacket: function(p: ppcap_t; buf: pAnsichar; size: integer)
: integer; cdecl;
pcap_lib_version: function(): pAnsichar; cdecl;
implementation
end.