NvAPI_DRS_SaveSettings() fails with NVAPI_ERROR on some applications

Hi,
I have written some code with nvapi to create an application profile so that it would force the application to use nvidia card over the integrated graphics card. The code works in one application but fails to work in another application. In the instance when it fails, the code runs smoothly until it hits NvAPI_DRS_SaveSettings() before destroying the session. The error code it returns is NVAPI_ERROR, which isn’t very helpful for debugging. Does anyone have any ideas on how to debug this? Or why it works in one application but not another? Thank you in advance!

NvAPI_Status status = NVAPI_OK;
    status = NvAPI_Initialize();
    if (status != NVAPI_OK)
    {
        return false;
    }
    NvDRSSessionHandle hSession = 0;
    status = NvAPI_DRS_CreateSession(&hSession);
    if (status != NVAPI_OK)
    {
        return false;
    }
    status = NvAPI_DRS_LoadSettings(hSession);
    if (status != NVAPI_OK)
    {
        return false;
    }
    NvDRSProfileHandle hProfile = 0;
    NvAPI_UnicodeString profile_name;
    memcpy_s(profile_name, sizeof(profile_name), L"my_profile", 16 * sizeof(wchar_t));
    status = NvAPI_DRS_FindProfileByName(hSession, profile_name, &hProfile);
    if (status == NVAPI_OK) {
        status = NvAPI_DRS_DeleteProfile(hSession, hProfile);
        if (status != NVAPI_OK)
        {
            return false;
        }
        status = NvAPI_DRS_SaveSettings(hSession);
        if (status != NVAPI_OK)
        {
            return false;
        }
        status = NvAPI_DRS_LoadSettings(hSession);
        if (status != NVAPI_OK)
        {
            return false;
        }
    }

    NVDRS_PROFILE drsProfile = { 0 };
    drsProfile.version = NVDRS_PROFILE_VER;
    drsProfile.isPredefined = 0;
    memcpy_s(drsProfile.profileName, sizeof(drsProfile.profileName), L"my_profile", 16 * sizeof(wchar_t));
    status = NvAPI_DRS_CreateProfile(hSession, &drsProfile, &hProfile);
    if (status != NVAPI_OK)
    {
        return false;
    }
    NVDRS_APPLICATION app;
    app.version = NVDRS_APPLICATION_VER;
    app.isPredefined = 0;
    wchar_t current_app[2048];
    if (GetModuleFileNameW(0, current_app, 2048)) {

        memcpy_s(app.appName, sizeof(app.appName), current_app, 2048 * sizeof(wchar_t));
        memcpy_s(app.userFriendlyName, sizeof(app.appName), current_app, 2048 * sizeof(wchar_t));
        status = NvAPI_DRS_CreateApplication(hSession, hProfile, &app);
        if (status != NVAPI_OK)
        {
            return false;
        }
    }

    NVDRS_SETTING drsSetting = { 0 };
    drsSetting.version = NVDRS_SETTING_VER;
    drsSetting = NVDRS_SETTING();
    drsSetting.version = NVDRS_SETTING_VER;
    drsSetting.settingId = SHIM_MCCOMPAT_ID;
    drsSetting.settingType = NVDRS_DWORD_TYPE;
    drsSetting.u32CurrentValue = SHIM_MCCOMPAT_ENABLE;
    status = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
    if (status != NVAPI_OK)
    {
        return false;
    }

    drsSetting = NVDRS_SETTING();
    drsSetting.version = NVDRS_SETTING_VER;
    drsSetting.settingId = SHIM_RENDERING_MODE_ID;
    drsSetting.settingType = NVDRS_DWORD_TYPE;
    drsSetting.u32CurrentValue = SHIM_RENDERING_MODE_ENABLE;
    status = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
    if (status != NVAPI_OK)
    {
        return false;
    }

    drsSetting = NVDRS_SETTING();
    drsSetting.version = NVDRS_SETTING_VER;
    drsSetting.settingId = SHIM_RENDERING_OPTIONS_ID;
    drsSetting.settingType = NVDRS_DWORD_TYPE;
    drsSetting.u32CurrentValue = SHIM_RENDERING_OPTIONS_DEFAULT_RENDERING_MODE;
    status = NvAPI_DRS_SetSetting(hSession, hProfile, &drsSetting);
    if (status != NVAPI_OK)
    {
        return false;
    }
    status = NvAPI_DRS_SaveSettings(hSession);
    if (status != NVAPI_OK)
    {
        return false;
    }
    status = NvAPI_DRS_DestroySession(hSession);
    if (status != NVAPI_OK)
    {
        return false;
    }
    hSession = 0;