#pragma once #include #include #include "util.h" class WintunDll { public: WintunDll() : dllHandle(nullptr) { auto wintunPath = GetProcessModulePath().replace_filename(L"wintun.dll"); dllHandle = LoadLibraryExW(wintunPath.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH); if (nullptr == dllHandle) { THROW_WINDOWS_ERROR(GetLastError(), "LoadLibraryExW"); } try { deleteDriver = getProcAddressOrThrow("WintunDeleteDriver"); } catch (...) { FreeLibrary(dllHandle); throw; } } ~WintunDll() { if (nullptr != dllHandle) { FreeLibrary(dllHandle); } } WINTUN_DELETE_DRIVER_FUNC *deleteDriver; private: template T getProcAddressOrThrow(const char *procName) { const T result = reinterpret_cast(GetProcAddress(dllHandle, procName)); if (nullptr == result) { THROW_WINDOWS_ERROR(GetLastError(), "GetProcAddress"); } return result; } HMODULE dllHandle; };