diff --git a/include/icsneo/platform/windows/internal/pcapdll.h b/include/icsneo/platform/windows/internal/pcapdll.h index 24b38ea..e936d79 100644 --- a/include/icsneo/platform/windows/internal/pcapdll.h +++ b/include/icsneo/platform/windows/internal/pcapdll.h @@ -12,8 +12,7 @@ namespace icsneo { class PCAPDLL { 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 lazyLoadHolder; - static bool lazyLoaded; + static const PCAPDLL& getInstance(); // Functions typedef int(__cdecl* PCAPFINDDEVICE)(char* source, struct pcap_rmtauth* auth, pcap_if_t** alldevs, char* errbuf); @@ -45,14 +44,14 @@ public: PCAPCREATESRCSTR createsrcstr; PCAPSETBUFF setbuff; - PCAPDLL(); - ~PCAPDLL() { closeDLL(); } - bool ok() const { return dll != nullptr; } + + ~PCAPDLL(); + bool ok() const; private: + PCAPDLL(); HINSTANCE dll; void closeDLL(); }; - } #endif \ No newline at end of file diff --git a/include/icsneo/platform/windows/pcap.h b/include/icsneo/platform/windows/pcap.h index 8b3b862..086b11f 100644 --- a/include/icsneo/platform/windows/pcap.h +++ b/include/icsneo/platform/windows/pcap.h @@ -26,7 +26,7 @@ public: bool isOpen(); bool close(); private: - PCAPDLL pcap; + const PCAPDLL& pcap; char errbuf[PCAP_ERRBUF_SIZE] = { 0 }; neodevice_t& device; uint8_t deviceMAC[6]; diff --git a/platform/windows/internal/pcapdll.cpp b/platform/windows/internal/pcapdll.cpp index e3fe895..e12bf4f 100644 --- a/platform/windows/internal/pcapdll.cpp +++ b/platform/windows/internal/pcapdll.cpp @@ -2,15 +2,31 @@ using namespace icsneo; -std::shared_ptr PCAPDLL::lazyLoadHolder; -bool PCAPDLL::lazyLoaded = false; +const PCAPDLL& PCAPDLL::getInstance() +{ + static PCAPDLL instance; + return instance; +} -PCAPDLL::PCAPDLL() { - if(!lazyLoaded) { - lazyLoaded = true; - lazyLoadHolder = std::make_shared(); - } - +PCAPDLL::~PCAPDLL() +{ + closeDLL(); +} + +void PCAPDLL::closeDLL() +{ + if (dll) + FreeLibrary(dll); + dll = nullptr; +} + +bool PCAPDLL::ok() const +{ + return dll != nullptr; +} + +PCAPDLL::PCAPDLL() +{ dll = LoadLibrary("wpcap.dll"); if(dll == NULL) { @@ -43,9 +59,3 @@ PCAPDLL::PCAPDLL() { } } } - -void PCAPDLL::closeDLL() { - if(dll) - FreeLibrary(dll); - dll = nullptr; -} \ No newline at end of file diff --git a/platform/windows/pcap.cpp b/platform/windows/pcap.cpp index 98789ab..dd9349c 100644 --- a/platform/windows/pcap.cpp +++ b/platform/windows/pcap.cpp @@ -18,7 +18,7 @@ std::vector PCAP::knownInterfaces; std::vector PCAP::FindAll() { std::vector foundDevices; - PCAPDLL pcap; + const PCAPDLL& pcap = PCAPDLL::getInstance(); if(!pcap.ok()) { EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotStart, APIEvent::Severity::Error); return std::vector(); @@ -177,7 +177,7 @@ bool PCAP::IsHandleValid(neodevice_handle_t handle) { return (netifIndex < knownInterfaces.size()); } -PCAP::PCAP(const device_eventhandler_t& err, neodevice_t& forDevice) : Driver(err), device(forDevice) { +PCAP::PCAP(const device_eventhandler_t& err, neodevice_t& forDevice) : Driver(err), device(forDevice), pcap(PCAPDLL::getInstance()) { if(IsHandleValid(device.handle)) { interface = knownInterfaces[(device.handle >> 24) & 0xFF]; interface.fp = nullptr; // We're going to open our own connection to the interface. This should already be nullptr but just in case.