Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
sasha0552 authored Sep 4, 2024
1 parent ea15d89 commit 223ba6d
Showing 1 changed file with 6 additions and 71 deletions.
77 changes: 6 additions & 71 deletions src/nvapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
#include <stddef.h>
#include <stdio.h>

#ifdef _WIN32
#include <windows.h>
#elif __linux__
#include <dlfcn.h>
#endif

#include "nvapi.h"

/***** ***** ***** ***** ***** TYPES ***** ***** ***** ***** *****/
Expand All @@ -22,8 +16,6 @@ typedef NvAPI_Status (*NvAPI_Unload_t)();

/***** ***** ***** ***** ***** VARIABLES ***** ***** ***** ***** *****/

static void * lib;

static NvAPI_EnumPhysicalGPUs_t _NvAPI_EnumPhysicalGPUs;
static NvAPI_GPU_SetForcePstate_t _NvAPI_GPU_SetForcePstate;
static NvAPI_GetErrorMessage_t _NvAPI_GetErrorMessage;
Expand Down Expand Up @@ -65,53 +57,6 @@ NvAPI_Status NvAPI_GetErrorMessage(NvAPI_Status nr, NvAPI_ShortString szDesc) {
}

NvAPI_Status NvAPI_Initialize() {
// Check the platform and load the appropriate NvAPI library
#ifdef _WIN32
if (!lib) {
lib = LoadLibrary("nvapi64.dll");
}

if (!lib) {
lib = LoadLibrary("nvapi.dll");
}
#elif __linux__
if (!lib) {
lib = dlopen("libnvidia-api.so.1", RTLD_LAZY);
}

if (!lib) {
lib = dlopen("libnvidia-api.so", RTLD_LAZY);
}
#endif

// If the library handle is still not initialized, loading the library failed
if (!lib) {
// Print an error message indicating failure to load the NvAPI library
fprintf(stderr, "Unable to load NvAPI library\n");

// Return an error status indicating that the library was not found
return NVAPI_LIBRARY_NOT_FOUND;
}

// Declare a function pointer for nvapi_QueryInterface
nvapi_QueryInterface_t nvapi_QueryInterface;

// Get the address of the nvapi_QueryInterface function from the loaded library
#ifdef _WIN32
nvapi_QueryInterface = (nvapi_QueryInterface_t) GetProcAddress((HMODULE) lib, "nvapi_QueryInterface");
#elif __linux__
nvapi_QueryInterface = (nvapi_QueryInterface_t) dlsym(lib, "nvapi_QueryInterface");
#endif

// If the function pointer is still null, gathering the address failed
if (!nvapi_QueryInterface) {
// Print an error message indicating failure to gather the function address
fprintf(stderr, "Unable to retrieve nvapi_QueryInterface function\n");

// Return an error status indicating that the library was not found
return NVAPI_LIBRARY_NOT_FOUND;
}

// Retrieve the addresses of specific NvAPI functions using nvapi_QueryInterface
_NvAPI_EnumPhysicalGPUs = (NvAPI_EnumPhysicalGPUs_t) nvapi_QueryInterface(0xe5ac921f);
_NvAPI_GPU_SetForcePstate = (NvAPI_GPU_SetForcePstate_t) nvapi_QueryInterface(0x025bfb10);
Expand All @@ -135,22 +80,12 @@ NvAPI_Status NvAPI_Unload() {

// If the function call was successful, proceed with cleanup
if (ret == NVAPI_OK) {
// If the library handle is initialized
if (lib) {
// Nullify all the function pointers to prevent further use
_NvAPI_EnumPhysicalGPUs = NULL;
_NvAPI_GPU_SetForcePstate = NULL;
_NvAPI_GetErrorMessage = NULL;
_NvAPI_Initialize = NULL;
_NvAPI_Unload = NULL;

// Free the loaded library based on the platform
#ifdef _WIN32
FreeLibrary((HMODULE) lib);
#elif __linux__
dlclose(lib);
#endif
}
// Nullify all the function pointers to prevent further use
_NvAPI_EnumPhysicalGPUs = NULL;
_NvAPI_GPU_SetForcePstate = NULL;
_NvAPI_GetErrorMessage = NULL;
_NvAPI_Initialize = NULL;
_NvAPI_Unload = NULL;
}

// Return the status of the function call
Expand Down

0 comments on commit 223ba6d

Please sign in to comment.