Lazily load PCAP DLL on Windows, keeping it cached afterwards

pull/4/head
Paul Hollinsky 2018-09-25 17:55:59 -04:00
parent af4f4894f5
commit dec85a4c55
2 changed files with 13 additions and 0 deletions

View File

@ -4,12 +4,17 @@
#include <Windows.h> #include <Windows.h>
#include <winsock2.h> #include <winsock2.h>
#include <pcap.h> #include <pcap.h>
#include <memory>
namespace icsneo { namespace icsneo {
// Helper loader for the PCAP DLL // Helper loader for the PCAP DLL
class PCAPDLL { class PCAPDLL {
public: public:
// The first time we use the DLL we keep it in here and it won't get freed until the user unloads us (for speed reasons)
static std::shared_ptr<PCAPDLL> lazyLoadHolder;
static bool lazyLoaded;
// Functions // Functions
typedef int(__cdecl* PCAPFINDDEVICE)(char* source, struct pcap_rmtauth* auth, pcap_if_t** alldevs, char* errbuf); typedef int(__cdecl* PCAPFINDDEVICE)(char* source, struct pcap_rmtauth* auth, pcap_if_t** alldevs, char* errbuf);
typedef pcap_t*(__cdecl* PCAPOPEN)(const char* source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth* auth, char* errbuf); typedef pcap_t*(__cdecl* PCAPOPEN)(const char* source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth* auth, char* errbuf);

View File

@ -2,7 +2,15 @@
using namespace icsneo; using namespace icsneo;
std::shared_ptr<PCAPDLL> PCAPDLL::lazyLoadHolder;
bool PCAPDLL::lazyLoaded = false;
PCAPDLL::PCAPDLL() { PCAPDLL::PCAPDLL() {
if(!lazyLoaded) {
lazyLoaded = true;
lazyLoadHolder = std::make_shared<PCAPDLL>();
}
dll = LoadLibrary("wpcap.dll"); dll = LoadLibrary("wpcap.dll");
if(dll == NULL) { if(dll == NULL) {