| 7 | namespace pwr::nv |
| 8 | { |
| 9 | NvapiWrapper::NvapiWrapper() |
| 10 | { |
| 11 | // Nvapi has it's own method for finding interfaces, so we need to get the proc first |
| 12 | const auto QueryInterface = static_cast<void*(*)(unsigned int)>(dll.GetProcAddress("nvapi_QueryInterface")); |
| 13 | if (!QueryInterface) |
| 14 | { |
| 15 | throw std::runtime_error{ "Failed to get query proc in nvapi dll" }; |
| 16 | } |
| 17 | |
| 18 | // Query for all endpoints in NVW_ENDPOINT_LIST using the QueryInterface endpoint |
| 19 | // do compile-time lookup into constexpr array to get the enpoint ids |
| 20 | const auto LoadEndpoint = [QueryInterface](const std::string& endpointName) -> void* { |
| 21 | if (const auto i = std::ranges::find(nvapi_interface_table, endpointName, &NVAPI_INTERFACE_TABLE_ENTRY::func); |
| 22 | i != std::end(nvapi_interface_table)) |
| 23 | { |
| 24 | // TODO: log error that endpoint not found after query (if Query returns nullptr) |
| 25 | return QueryInterface(i->id); |
| 26 | } |
| 27 | else |
| 28 | { |
| 29 | // TODO: log error that endpoint not found in ID lookup table |
| 30 | return nullptr; |
| 31 | } |
| 32 | }; |
| 33 | #define X_(name, ...) p##name = static_cast<decltype(p##name)>(LoadEndpoint("NvAPI_"#name)); |
| 34 | NVW_NVAPI_ENDPOINT_LIST |
| 35 | #undef X_ |
| 36 | |
| 37 | // try to initialize the api |
| 38 | // if we are unable, abort wrapper construction with exception |
| 39 | if (const auto pInitialize = static_cast<NvAPI_Status(*)()>(LoadEndpoint("NvAPI_Initialize"))) |
| 40 | { |
| 41 | if (!Ok(pInitialize())) |
| 42 | { |
| 43 | throw std::runtime_error{ "Call to nvapi initialize failed" }; |
| 44 | } |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | throw std::runtime_error{ "Failed to query nvapi initialization endpoint" }; |
| 49 | } |
| 50 | |
| 51 | // load the private endpoint for unloading api |
| 52 | // TODO: log if we cannot find this endpoint, but don't throw |
| 53 | pUnload = static_cast<NvAPI_Status(*)()>(LoadEndpoint("NvAPI_Unload")); |
| 54 | } |
| 55 | |
| 56 | NvapiWrapper::~NvapiWrapper() |
| 57 | { |
nothing calls this directly
no test coverage detected