using System;
using NvAPIWrapper.Native.Exceptions;
using NvAPIWrapper.Native.General;
using NvAPIWrapper.Native.General.Structures;
using NvAPIWrapper.Native.Helpers;
using NvAPIWrapper.Native.Helpers.Structures;
using NvAPIWrapper.Native.Interfaces.General;
namespace NvAPIWrapper.Native
{
///
/// Contains system and general static functions
///
public static class GeneralApi
{
///
/// This function returns information about the system's chipset.
///
/// Information about the system's chipset
/// This operation is not supported.
/// Status.InvalidArgument: Invalid argument
/// A delegate callback throws an exception.
public static IChipsetInfo GetChipsetInfo()
{
var getChipSetInfo = DelegateFactory.GetDelegate();
foreach (var acceptType in getChipSetInfo.Accepts())
{
var instance = acceptType.Instantiate();
using (var chipsetInfoReference = ValueTypeReference.FromValueType(instance, acceptType))
{
var status = getChipSetInfo(chipsetInfoReference);
if (status == Status.IncompatibleStructureVersion)
{
continue;
}
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return chipsetInfoReference.ToValueType(acceptType);
}
}
throw new NVIDIANotSupportedException("This operation is not supported.");
}
///
/// This API returns display driver version and driver-branch string.
///
/// Contains the driver-branch string after successful return.
/// Returns driver version
/// Status.ApiNotInitialized: NVAPI not initialized
/// Status.Error: Miscellaneous error occurred
public static uint GetDriverAndBranchVersion(out string branchVersion)
{
var status = DelegateFactory.GetDelegate()(
out var driverVersion, out var branchVersionShortString);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
branchVersion = branchVersionShortString.Value;
return driverVersion;
}
///
/// This function converts an NvAPI error code into a null terminated string.
///
/// The error code to convert
/// The string corresponding to the error code
// ReSharper disable once FlagArgument
public static string GetErrorMessage(Status statusCode)
{
statusCode =
DelegateFactory.GetDelegate()(statusCode, out var message);
if (statusCode != Status.Ok)
{
return null;
}
return message.Value;
}
///
/// This function returns a string describing the version of the NvAPI library. The contents of the string are human
/// readable. Do not assume a fixed format.
///
/// User readable string giving NvAPI version information
/// See NVIDIAApiException.Status for the reason of the exception.
public static string GetInterfaceVersionString()
{
var status =
DelegateFactory.GetDelegate()(out var version);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return version.Value;
}
///
/// This function returns the current lid and dock information.
///
/// Current lid and dock information
/// Status.Error: Generic error
/// Status.NotSupported: Requested feature not supported
/// Status.HandleInvalidated: Handle is no longer valid
/// Status.ApiNotInitialized: NvAPI_Initialize() has not been called
public static LidDockParameters GetLidAndDockInfo()
{
var dockInfo = typeof(LidDockParameters).Instantiate();
var status = DelegateFactory.GetDelegate()(ref dockInfo);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return dockInfo;
}
///
/// This function initializes the NvAPI library (if not already initialized) but always increments the ref-counter.
/// This must be called before calling other NvAPI_ functions.
///
/// Status.Error: Generic error
/// Status.LibraryNotFound: nvapi.dll can not be loaded
public static void Initialize()
{
var status = DelegateFactory.GetDelegate()();
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
}
///
/// PRIVATE - Requests to restart the display driver
///
public static void RestartDisplayDriver()
{
var status = DelegateFactory.GetDelegate()();
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
}
///
/// Decrements the ref-counter and when it reaches ZERO, unloads NVAPI library.
/// This must be called in pairs with NvAPI_Initialize.
/// Note: By design, it is not mandatory to call NvAPI_Initialize before calling any NvAPI.
/// When any NvAPI is called without first calling NvAPI_Initialize, the internal ref-counter will be implicitly
/// incremented. In such cases, calling NvAPI_Initialize from a different thread will result in incrementing the
/// ref-count again and the user has to call NvAPI_Unload twice to unload the library. However, note that the implicit
/// increment of the ref-counter happens only once.
/// If the client wants unload functionality, it is recommended to always call NvAPI_Initialize and NvAPI_Unload in
/// pairs.
/// Unloading NvAPI library is not supported when the library is in a resource locked state.
/// Some functions in the NvAPI library initiates an operation or allocates certain resources and there are
/// corresponding functions available, to complete the operation or free the allocated resources. All such function
/// pairs are designed to prevent unloading NvAPI library.
/// For example, if NvAPI_Unload is called after NvAPI_XXX which locks a resource, it fails with NVAPI_ERROR.
/// Developers need to call the corresponding NvAPI_YYY to unlock the resources, before calling NvAPI_Unload again.
///
/// Status.Error: Generic error
///
/// Status.ApiInUse: At least an API is still being called hence cannot unload NVAPI
/// library from process
///
public static void Unload()
{
var status = DelegateFactory.GetDelegate()();
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
}
}
}