Skip to content

Commit

Permalink
Use NvAPI_DRS_FindApplicationByName to find the profile
Browse files Browse the repository at this point in the history
  • Loading branch information
bullno1 committed Nov 2, 2024
1 parent 750a553 commit 228d76e
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions nvapi/nvapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ typedef WINAPI int(*NvAPI_DRS_CreateProfile_t)(NvDRSSessionHandle, NVDRS_PROFILE
typedef WINAPI int(*NvAPI_DRS_CreateApplication_t)(NvDRSSessionHandle, NvDRSProfileHandle, NVDRS_APPLICATION *);
typedef WINAPI int(*NvAPI_DRS_SaveSettings_t)(NvDRSSessionHandle);
typedef WINAPI int(*NvAPI_DRS_SetSetting_t)(NvDRSSessionHandle, NvDRSProfileHandle, NVDRS_SETTING *);
typedef WINAPI int(*NvAPI_DRS_FindApplicationByName_t)(NvDRSSessionHandle hSession, NvAPI_UnicodeString appName, NvDRSProfileHandle* phProfile, NVDRS_APPLICATION* pApplication);
typedef WINAPI int(*NvAPI_DRS_FindProfileByName_t)(NvDRSSessionHandle, NvAPI_UnicodeString, NvDRSProfileHandle *);
typedef WINAPI int(*NvAPI_DRS_GetApplicationInfo_t)(NvDRSSessionHandle, NvDRSProfileHandle, NvAPI_UnicodeString, NVDRS_APPLICATION *);
typedef WINAPI int(*NvAPI_DRS_DeleteProfile_t)(NvDRSSessionHandle, NvDRSProfileHandle);
typedef WINAPI void*(*NvAPI_QueryInterface_t)(unsigned int);

Expand All @@ -44,8 +44,6 @@ nvapi_disable_threaded_optimization(const char* profile_name) {
HMODULE nvapi = LoadLibraryA("nvapi64.dll");
if (nvapi == NULL) { return false; }

size_t profile_name_utf16_len;
char16_t* profile_name_utf16 = utf8to16(profile_name, -1, &profile_name_utf16_len);
char16_t module_path[PATH_MAX];
bool success = false;
bool initialized = false;
Expand All @@ -62,38 +60,43 @@ nvapi_disable_threaded_optimization(const char* profile_name) {
NvAPI_DRS_CreateApplication_t NvAPI_DRS_CreateApplication = (NvAPI_DRS_CreateApplication_t)NvAPI_QueryInterface(0x4347A9DE);
NvAPI_DRS_SaveSettings_t NvAPI_DRS_SaveSettings = (NvAPI_DRS_SaveSettings_t)NvAPI_QueryInterface(0xFCBC7E14);
NvAPI_DRS_SetSetting_t NvAPI_DRS_SetSetting = (NvAPI_DRS_SetSetting_t)NvAPI_QueryInterface(0x577DD202);
NvAPI_DRS_FindApplicationByName_t NvAPI_DRS_FindApplicationByName = (NvAPI_DRS_FindApplicationByName_t)NvAPI_QueryInterface(0xEEE566B2);
NvAPI_DRS_FindProfileByName_t NvAPI_DRS_FindProfileByName = (NvAPI_DRS_FindProfileByName_t)NvAPI_QueryInterface(0x7E4A9A0B);
NvAPI_DRS_GetApplicationInfo_t NvAPI_DRS_GetApplicationInfo = (NvAPI_DRS_GetApplicationInfo_t)NvAPI_QueryInterface(0xED1F8C69);
/*NvAPI_DRS_DeleteProfile_t NvAPI_DRS_DeleteProfile = (NvAPI_DRS_DeleteProfile_t)NvAPI_QueryInterface(0x17093206);*/

NVAPI_CHECK(NvAPI_Initialize());
initialized = true;

NVAPI_CHECK(NvAPI_DRS_CreateSession(&session_handle));
NVAPI_CHECK(NvAPI_DRS_LoadSettings(session_handle));

NvDRSProfileHandle profile_handle;
if (NvAPI_DRS_FindProfileByName(session_handle, profile_name_utf16, &profile_handle) != 0) {
// Try to find existing profile or create if one does not exist
uint32_t module_path_len = GetModuleFileName(NULL, module_path, PATH_MAX);
NvDRSProfileHandle profile_handle = NULL;
NVDRS_APPLICATION app = { .version = NVDRS_APPLICATION_VER };
if (NvAPI_DRS_FindApplicationByName(session_handle, module_path, &profile_handle, &app) != 0) {
NVDRS_PROFILE profile_info = {
.version = NVDRS_PROFILE_VER,
.isPredefined = 0,
};

size_t profile_name_utf16_len;
char16_t* profile_name_utf16 = utf8to16(profile_name, -1, &profile_name_utf16_len);
memcpy(profile_info.profileName, profile_name_utf16, sizeof(char16_t) * profile_name_utf16_len);
free(profile_name_utf16);

NVAPI_CHECK(NvAPI_DRS_CreateProfile(session_handle, &profile_info, &profile_handle));
}
if (NvAPI_DRS_FindProfileByName(session_handle, profile_info.profileName, &profile_handle) != 0) {
NVAPI_CHECK(NvAPI_DRS_CreateProfile(session_handle, &profile_info, &profile_handle));
}

uint32_t module_path_len = GetModuleFileName(NULL, module_path, PATH_MAX);
NVDRS_APPLICATION_V4 app;
if (NvAPI_DRS_GetApplicationInfo(session_handle, profile_handle, module_path, &app) != 0) {
app = (NVDRS_APPLICATION_V4) {
app = (NVDRS_APPLICATION){
.version = NVDRS_APPLICATION_VER,
.isPredefined = 0,
};
memcpy(app.appName, module_path, sizeof(char16_t) * module_path_len);
NVAPI_CHECK(NvAPI_DRS_CreateApplication(session_handle, profile_handle, &app));
}

// Apply setting
NVDRS_SETTING setting = {
.version = NVDRS_SETTING_VER,
.settingId = OGL_THREAD_CONTROL_ID,
Expand All @@ -110,7 +113,6 @@ nvapi_disable_threaded_optimization(const char* profile_name) {
end:
if (session_handle) { NvAPI_DRS_DestroySession(session_handle); }
if (initialized) { NvAPI_Unload(); }
if (profile_name_utf16) { free(profile_name_utf16); }
if (nvapi) { FreeLibrary(nvapi); }

return success;
Expand Down

0 comments on commit 228d76e

Please sign in to comment.