{"record":{"id":"eee63c04d96abbfe","repo":"Orbmu2k/nvidiaProfileInspector","slug":"drs-findprofilebyname","errorCode":null,"errorMessage":"DRS_FindProfileByName","messagePattern":"DRS_FindProfileByName","errorType":"exception","errorClass":"NvapiException","httpStatus":null,"severity":"error","filePath":"nvidiaProfileInspector/Common/DrsSettingsServiceBase.cs","lineNumber":79,"sourceCode":"            if (string.IsNullOrEmpty(profileName))\n            {\n                var nvRes = nvw.Instance.DRS_GetCurrentGlobalProfile(hSession, ref hProfile);\n\n                if (hProfile == IntPtr.Zero)\n                    throw new NvapiException(\"DRS_GetCurrentGlobalProfile\", NvAPI_Status.NVAPI_PROFILE_NOT_FOUND);\n\n                if (nvRes != NvAPI_Status.NVAPI_OK)\n                    throw new NvapiException(\"DRS_GetCurrentGlobalProfile\", nvRes);\n            }\n            else\n            {\n                var nvRes = nvw.Instance.DRS_FindProfileByName(hSession, new StringBuilder(profileName), ref hProfile);\n\n                if (nvRes == NvAPI_Status.NVAPI_PROFILE_NOT_FOUND)\n                    return IntPtr.Zero;\n\n                if (nvRes != NvAPI_Status.NVAPI_OK)\n                    throw new NvapiException(\"DRS_FindProfileByName\", nvRes);\n            }\n            return hProfile;\n        }\n\n        protected IntPtr CreateProfile(IntPtr hSession, string profileName)\n        {\n            if (string.IsNullOrEmpty(profileName))\n                throw new ArgumentNullException(\"profileName\");\n\n            var hProfile = IntPtr.Zero;\n\n            var newProfile = new NVDRS_PROFILE()\n            {\n                version = nvw.NVDRS_PROFILE_VER,\n                profileName = profileName,\n            };\n\n            var nvRes = nvw.Instance.DRS_CreateProfile(hSession, ref newProfile, ref hProfile);","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/Orbmu2k/nvidiaProfileInspector/blob/2f50c388b3a4d661cade66b32746bec096d1eee1/nvidiaProfileInspector/Common/DrsSettingsServiceBase.cs#L61-L97","documentation":"NvapiException wrapping a non-OK NVAPI status from the DRS_FindProfileByName call, which looks up an NVIDIA driver-settings profile by name in an open DRS session. The library deliberately swallows NVAPI_PROFILE_NOT_FOUND (returns IntPtr.Zero so callers can create the profile), so this exception means a different, unexpected NVAPI failure occurred: typically NVAPI_API_NOT_INITIALIZED / NVAPI_SESSION_NOT_FOUND (bad or closed session handle), NVAPI_INVALID_ARGUMENT, or NVAPI_EXECUTABLE_NOT_FOUND when the NVAPI DLL could not load on a machine without NVIDIA drivers.","triggerScenarios":"Calling GetProfileHandle with a session handle that was never created (DRS_CreateSession failed or its status was ignored), a session already destroyed by DRS_DestroySession, an invalid (non-NUL-terminated or oversized) profileName StringBuilder, or running on a system where nvapi.dll is missing/stale so the P/Invoke returns NVAPI_API_NOT_INITIALIZED or NVAPI_NO_IMPLEMENTATION.","commonSituations":"Running the tool on a machine with only integrated/AMD graphics (no NVIDIA driver => nvapi.dll absent), a broken or very old NVIDIA driver install, a driver update mid-run invalidating the DRS session, or a code path that closed the DrsSessionScope before calling GetProfileHandle.","solutions":["Verify an NVIDIA GPU with current drivers is installed and nvapi.dll loads (the static constructor's SYS_GetDriverAndBranchVersion returning NVAPI_OK is a good pre-check).","Ensure DRS_CreateSession succeeded and the handle is used only inside the DrsSessionScope lifetime; never cache hSession across scopes.","Log the inner NvapiException.Status to distinguish NVAPI_API_NOT_INITIALIZED (driver problem) from NVAPI_INVALID_ARGUMENT (bad name/handle).","Update GeForce/Studio drivers; very old drivers lack parts of the DRS API and return NVAPI_NO_IMPLEMENTATION.","If the profile genuinely may not exist, rely on the IntPtr.Zero return path instead of treating the exception as 'not found'."],"exampleFix":"// before\nvar hProfile = service.GetProfileHandle(hSession, profileName); // throws NvapiException\n// after\ntry\n{\n    var hProfile = service.GetProfileHandle(hSession, profileName);\n}\ncatch (NvapiException ex) when (ex.Status == NvAPI_Status.NVAPI_API_NOT_INITIALIZED)\n{\n    // NVIDIA driver unavailable - surface a friendly message or skip DRS work\n}","handlingStrategy":"try-catch","validationCode":"if (!NvapiDrsWrapper.Instance.SYS_GetDriverAndBranchVersion(ref ver, ref branch).Equals(NvAPI_Status.NVAPI_OK))\n    throw new InvalidOperationException(\"NVIDIA driver/NVAPI unavailable\");\nif (string.IsNullOrWhiteSpace(profileName))\n    throw new ArgumentException(\"profileName required\", nameof(profileName));","typeGuard":"static bool HasValidSession(IntPtr hSession) => hSession != IntPtr.Zero;","tryCatchPattern":"try { handle = GetProfileHandle(hSession, name); }\ncatch (NvapiException ex)\n{\n    if (ex.Status == NvAPI_Status.NVAPI_API_NOT_INITIALIZED)\n        ReportDriverUnavailable();\n    else\n        throw; // do not treat as 'profile not found'\n}","preventionTips":["Pre-check NVAPI availability (SYS_GetDriverAndBranchVersion) before any DRS work.","Treat only the IntPtr.Zero return as 'profile not found'; the exception means something else.","Keep all DRS calls inside the DrsSessionScope using its handle.","Log NvapiException.Status for diagnosis.","Keep NVIDIA drivers current on machines running the tool."],"tags":["nvapi","drs","driver-settings","profile-lookup","windows"],"backgroundTag":"api-error-response","analyzedSha":"2f50c388b3a4d661cade66b32746bec096d1eee1","analyzedAt":"2026-09-15T05:23:52.218Z","contentChangedAt":"2026-09-15T05:23:52.218Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}