NVIDIA / nvapi

NVAPI is NVIDIA's core software development kit that allows direct access to NVIDIA GPUs and drivers on supported platforms.
Other
89 stars 8 forks source link

Missing DLL Files Windows #5

Open jredfox opened 5 months ago

jredfox commented 5 months ago

If you are using C# program you need the DLL version not the .Lib. There are no way to get the DLL file

Saancreed commented 1 month ago

@jredfox In case you haven't figured this out by now, the lack of shared library in this repository is not a mistake. It's shipped as a part of NVIDIA Display Driver and you must use the version installed with the driver package. Therefore, you have to load this library at runtime and manually query function pointers from it to call into.

With the release of R560 SDK everything you need is now in this repository (probably). First, you need to take a look at nvapi_interface.h to figure out function IDs/hashes you wish to use, then retrieve address of nvapi_QueryInterface function from native shared library and call it with appropriate ID. For example (ignoring error handling to simplify):

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using unsafe NvapiQueryInterface = delegate* unmanaged[Cdecl]<uint, nint>;
using unsafe NvapiEnumPhysicalGpus = delegate* unmanaged[Cdecl]<out NvPhysicalGpuHandles, out uint, NvapiStatus>;
using unsafe NvapiGpuGetFullName = delegate* unmanaged[Cdecl]<NvPhysicalGpuHandle, out NvapiShortString, NvapiStatus>;

unsafe
{
    var nvapi = NativeLibrary.Load(OperatingSystem.IsWindows() ? "nvapi64.dll" : "libnvidia-api.so.1");
    var queryInterface = (NvapiQueryInterface)NativeLibrary.GetExport(nvapi, "nvapi_QueryInterface");

    var initialize = (delegate* unmanaged[Cdecl]<NvapiStatus>)queryInterface(0x0150e828u);
    var unload = (delegate* unmanaged[Cdecl]<NvapiStatus>)queryInterface(0xd22bdd7eu);
    var enumPhysicalGpus = (NvapiEnumPhysicalGpus)queryInterface(0xe5ac921fu);
    var gpuGetFullName = (NvapiGpuGetFullName)queryInterface(0xceee8e9fu);

    initialize();
    enumPhysicalGpus(out var handles, out var count);

    for (var i = 0; i < count; ++i)
    {
        gpuGetFullName(handles[i], out var name);
        Console.WriteLine($"GPU {i}: {Marshal.PtrToStringUTF8(new(&name[0]))}");
    }

    unload();
    NativeLibrary.Free(nvapi);
}

enum NvapiStatus
{
    Ok = 0,
}

readonly record struct NvPhysicalGpuHandle(nint Value);

[InlineArray(64)]
struct NvPhysicalGpuHandles
{
    private NvPhysicalGpuHandle _;
}

[InlineArray(64)]
struct NvapiShortString
{
    private byte _;
}