Hardware/Nvidia/NVAPI.cs
author moel.mich
Sat, 31 Dec 2011 17:31:04 +0000
changeset 324 c6ee430d6995
parent 195 0ee888c485d5
child 334 013e148e8f12
permissions -rw-r--r--
Modified and extended version of the patch v4 by Roland Reinl (see Issue 256). Main differences to the original patch: DeviceIoControl refactorings removed, SmartAttribute is now descriptive only and does not hold any state, report is written as one 80 columns table, sensors are created only for meaningful values and without duplicates (remaining life, temperatures, host writes and reads). Also the current implementation should really preserve all the functionality of the old system. Additionally there is now a simple SMART devices emulation class (DebugSmart) that can be used in place of WindowsSmart for testing with reported data.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@309
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2011
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@309
    23
  Christian Vallières
moel@1
    24
moel@1
    25
  Alternatively, the contents of this file may be used under the terms of
moel@1
    26
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    27
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    28
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    29
  of those above. If you wish to allow use of your version of this file only
moel@1
    30
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    31
  use your version of this file under the terms of the MPL, indicate your
moel@1
    32
  decision by deleting the provisions above and replace them with the notice
moel@1
    33
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    34
  the provisions above, a recipient may use your version of this file under
moel@1
    35
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    36
 
moel@1
    37
*/
moel@1
    38
moel@1
    39
using System;
moel@1
    40
using System.Runtime.InteropServices;
moel@1
    41
using System.Text;
moel@1
    42
moel@1
    43
namespace OpenHardwareMonitor.Hardware.Nvidia {
moel@1
    44
moel@165
    45
  internal enum NvStatus {
moel@1
    46
    OK = 0,
moel@1
    47
    ERROR = -1,
moel@1
    48
    LIBRARY_NOT_FOUND = -2,
moel@1
    49
    NO_IMPLEMENTATION = -3,
moel@1
    50
    API_NOT_INTIALIZED = -4,
moel@1
    51
    INVALID_ARGUMENT = -5,
moel@1
    52
    NVIDIA_DEVICE_NOT_FOUND = -6,
moel@1
    53
    END_ENUMERATION = -7,
moel@1
    54
    INVALID_HANDLE = -8,
moel@1
    55
    INCOMPATIBLE_STRUCT_VERSION = -9,
moel@1
    56
    HANDLE_INVALIDATED = -10,
moel@1
    57
    OPENGL_CONTEXT_NOT_CURRENT = -11,
moel@1
    58
    NO_GL_EXPERT = -12,
moel@1
    59
    INSTRUMENTATION_DISABLED = -13,
moel@1
    60
    EXPECTED_LOGICAL_GPU_HANDLE = -100,
moel@1
    61
    EXPECTED_PHYSICAL_GPU_HANDLE = -101,
moel@1
    62
    EXPECTED_DISPLAY_HANDLE = -102,
moel@1
    63
    INVALID_COMBINATION = -103,
moel@1
    64
    NOT_SUPPORTED = -104,
moel@1
    65
    PORTID_NOT_FOUND = -105,
moel@1
    66
    EXPECTED_UNATTACHED_DISPLAY_HANDLE = -106,
moel@1
    67
    INVALID_PERF_LEVEL = -107,
moel@1
    68
    DEVICE_BUSY = -108,
moel@1
    69
    NV_PERSIST_FILE_NOT_FOUND = -109,
moel@1
    70
    PERSIST_DATA_NOT_FOUND = -110,
moel@1
    71
    EXPECTED_TV_DISPLAY = -111,
moel@1
    72
    EXPECTED_TV_DISPLAY_ON_DCONNECTOR = -112,
moel@1
    73
    NO_ACTIVE_SLI_TOPOLOGY = -113,
moel@1
    74
    SLI_RENDERING_MODE_NOTALLOWED = -114,
moel@1
    75
    EXPECTED_DIGITAL_FLAT_PANEL = -115,
moel@1
    76
    ARGUMENT_EXCEED_MAX_SIZE = -116,
moel@1
    77
    DEVICE_SWITCHING_NOT_ALLOWED = -117,
moel@1
    78
    TESTING_CLOCKS_NOT_SUPPORTED = -118,
moel@1
    79
    UNKNOWN_UNDERSCAN_CONFIG = -119,
moel@1
    80
    TIMEOUT_RECONFIGURING_GPU_TOPO = -120,
moel@1
    81
    DATA_NOT_FOUND = -121,
moel@1
    82
    EXPECTED_ANALOG_DISPLAY = -122,
moel@1
    83
    NO_VIDLINK = -123,
moel@1
    84
    REQUIRES_REBOOT = -124,
moel@1
    85
    INVALID_HYBRID_MODE = -125,
moel@1
    86
    MIXED_TARGET_TYPES = -126,
moel@1
    87
    SYSWOW64_NOT_SUPPORTED = -127,
moel@1
    88
    IMPLICIT_SET_GPU_TOPOLOGY_CHANGE_NOT_ALLOWED = -128,
moel@1
    89
    REQUEST_USER_TO_CLOSE_NON_MIGRATABLE_APPS = -129,
moel@1
    90
    OUT_OF_MEMORY = -130,
moel@1
    91
    WAS_STILL_DRAWING = -131,
moel@1
    92
    FILE_NOT_FOUND = -132,
moel@1
    93
    TOO_MANY_UNIQUE_STATE_OBJECTS = -133,
moel@1
    94
    INVALID_CALL = -134,
moel@1
    95
    D3D10_1_LIBRARY_NOT_FOUND = -135,
moel@1
    96
    FUNCTION_NOT_FOUND = -136
moel@165
    97
  }
moel@1
    98
moel@165
    99
  internal enum NvThermalController {
moel@1
   100
    NONE = 0,
moel@309
   101
    GPU_INTERNAL,
moel@1
   102
    ADM1032,
moel@309
   103
    MAX6649,
moel@309
   104
    MAX1617,
moel@309
   105
    LM99,
moel@309
   106
    LM89,
moel@309
   107
    LM64,
moel@1
   108
    ADT7473,
moel@1
   109
    SBMAX6649,
moel@309
   110
    VBIOSEVT,
moel@309
   111
    OS,
moel@1
   112
    UNKNOWN = -1,
moel@165
   113
  }
moel@1
   114
moel@165
   115
  internal enum NvThermalTarget {
moel@1
   116
    NONE = 0,
moel@1
   117
    GPU = 1,
moel@1
   118
    MEMORY = 2,
moel@1
   119
    POWER_SUPPLY = 4,
moel@1
   120
    BOARD = 8,
moel@1
   121
    ALL = 15,
moel@1
   122
    UNKNOWN = -1
moel@1
   123
  };
moel@1
   124
moel@61
   125
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   126
  internal struct NvSensor {
moel@1
   127
    public NvThermalController Controller;
moel@140
   128
    public uint DefaultMinTemp;
moel@140
   129
    public uint DefaultMaxTemp;
moel@140
   130
    public uint CurrentTemp;
moel@309
   131
    public NvThermalTarget Target;
moel@1
   132
  }
moel@1
   133
moel@61
   134
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   135
  internal struct NvGPUThermalSettings {
moel@140
   136
    public uint Version;
moel@140
   137
    public uint Count;
moel@309
   138
    [MarshalAs(UnmanagedType.ByValArray,
moel@1
   139
      SizeConst = NVAPI.MAX_THERMAL_SENSORS_PER_GPU)]
moel@1
   140
    public NvSensor[] Sensor;
moel@1
   141
  }
moel@1
   142
moel@61
   143
  [StructLayout(LayoutKind.Sequential)]
moel@165
   144
  internal struct NvDisplayHandle {
moel@195
   145
    private readonly IntPtr ptr;
moel@1
   146
  }
moel@1
   147
moel@61
   148
  [StructLayout(LayoutKind.Sequential)]
moel@165
   149
  internal struct NvPhysicalGpuHandle {
moel@195
   150
    private readonly IntPtr ptr;
moel@1
   151
  }
moel@1
   152
moel@140
   153
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   154
  internal struct NvClocks {
moel@140
   155
    public uint Version;
moel@140
   156
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = NVAPI.MAX_CLOCKS_PER_GPU)]
moel@140
   157
    public uint[] Clock;
moel@140
   158
  }
moel@140
   159
moel@140
   160
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   161
  internal struct NvPState {
moel@140
   162
    public bool Present;
moel@140
   163
    public int Percentage;
moel@140
   164
  }
moel@140
   165
moel@140
   166
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   167
  internal struct NvPStates {
moel@140
   168
    public uint Version;
moel@140
   169
    public uint Flags;
moel@140
   170
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = NVAPI.MAX_PSTATES_PER_GPU)]
moel@140
   171
    public NvPState[] PStates;
moel@140
   172
  }
moel@140
   173
moel@140
   174
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   175
  internal struct NvUsages {
moel@140
   176
    public uint Version;
moel@140
   177
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = NVAPI.MAX_USAGES_PER_GPU)]
moel@140
   178
    public uint[] Usage;
moel@140
   179
  }
moel@140
   180
moel@140
   181
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   182
  internal struct NvCooler {
moel@140
   183
    public int Type;
moel@140
   184
    public int Controller;
moel@140
   185
    public int DefaultMin;
moel@140
   186
    public int DefaultMax;
moel@140
   187
    public int CurrentMin;
moel@140
   188
    public int CurrentMax;
moel@140
   189
    public int CurrentLevel;
moel@140
   190
    public int DefaultPolicy;
moel@140
   191
    public int CurrentPolicy;
moel@140
   192
    public int Target;
moel@140
   193
    public int ControlType;
moel@140
   194
    public int Active;
moel@140
   195
  }
moel@140
   196
moel@140
   197
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   198
  internal struct NvGPUCoolerSettings {
moel@140
   199
    public uint Version;
moel@140
   200
    public uint Count;
moel@140
   201
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = NVAPI.MAX_COOLER_PER_GPU)]
moel@140
   202
    public NvCooler[] Cooler;
moel@140
   203
  }
moel@140
   204
moel@140
   205
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@309
   206
  internal struct NvLevel {
moel@309
   207
    public int Level;
moel@309
   208
    public int Policy;
moel@309
   209
  }
moel@309
   210
moel@309
   211
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@309
   212
  internal struct NvGPUCoolerLevels {
moel@309
   213
    public uint Version;
moel@309
   214
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = NVAPI.MAX_COOLER_PER_GPU)]
moel@309
   215
    public NvLevel[] Levels;
moel@309
   216
  }
moel@309
   217
moel@309
   218
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   219
  internal struct NvMemoryInfo {
moel@140
   220
    public uint Version;
moel@309
   221
    [MarshalAs(UnmanagedType.ByValArray, SizeConst =
moel@140
   222
      NVAPI.MAX_MEMORY_VALUES_PER_GPU)]
moel@140
   223
    public uint[] Values;
moel@140
   224
  }
moel@140
   225
moel@140
   226
  [StructLayout(LayoutKind.Sequential, Pack = 8)]
moel@165
   227
  internal struct NvDisplayDriverVersion {
moel@140
   228
    public uint Version;
moel@140
   229
    public uint DriverVersion;
moel@140
   230
    public uint BldChangeListNum;
moel@140
   231
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NVAPI.SHORT_STRING_MAX)]
moel@140
   232
    public string BuildBranch;
moel@140
   233
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NVAPI.SHORT_STRING_MAX)]
moel@140
   234
    public string Adapter;
moel@165
   235
  }
moel@140
   236
moel@165
   237
  internal class NVAPI {
moel@1
   238
moel@1
   239
    public const int MAX_PHYSICAL_GPUS = 64;
moel@140
   240
    public const int SHORT_STRING_MAX = 64;
moel@140
   241
moel@309
   242
    public const int MAX_THERMAL_SENSORS_PER_GPU = 3;
moel@140
   243
    public const int MAX_CLOCKS_PER_GPU = 0x120;
moel@140
   244
    public const int MAX_PSTATES_PER_GPU = 8;
moel@140
   245
    public const int MAX_USAGES_PER_GPU = 33;
moel@140
   246
    public const int MAX_COOLER_PER_GPU = 20;
moel@140
   247
    public const int MAX_MEMORY_VALUES_PER_GPU = 5;
moel@140
   248
moel@140
   249
    public static readonly uint GPU_THERMAL_SETTINGS_VER = (uint)
moel@1
   250
      Marshal.SizeOf(typeof(NvGPUThermalSettings)) | 0x10000;
moel@140
   251
    public static readonly uint GPU_CLOCKS_VER = (uint)
moel@140
   252
      Marshal.SizeOf(typeof(NvClocks)) | 0x20000;
moel@140
   253
    public static readonly uint GPU_PSTATES_VER = (uint)
moel@140
   254
      Marshal.SizeOf(typeof(NvPStates)) | 0x10000;
moel@140
   255
    public static readonly uint GPU_USAGES_VER = (uint)
moel@140
   256
      Marshal.SizeOf(typeof(NvUsages)) | 0x10000;
moel@140
   257
    public static readonly uint GPU_COOLER_SETTINGS_VER = (uint)
moel@140
   258
      Marshal.SizeOf(typeof(NvGPUCoolerSettings)) | 0x20000;
moel@309
   259
    public static readonly uint GPU_MEMORY_INFO_VER = (uint)
moel@140
   260
      Marshal.SizeOf(typeof(NvMemoryInfo)) | 0x20000;
moel@140
   261
    public static readonly uint DISPLAY_DRIVER_VERSION_VER = (uint)
moel@140
   262
      Marshal.SizeOf(typeof(NvDisplayDriverVersion)) | 0x10000;
moel@309
   263
    public static readonly uint GPU_COOLER_LEVELS_VER = (uint)
moel@309
   264
      Marshal.SizeOf(typeof(NvGPUCoolerLevels)) | 0x10000;
moel@309
   265
moel@1
   266
    private delegate IntPtr nvapi_QueryInterfaceDelegate(uint id);
moel@1
   267
    private delegate NvStatus NvAPI_InitializeDelegate();
moel@1
   268
    private delegate NvStatus NvAPI_GPU_GetFullNameDelegate(
moel@1
   269
      NvPhysicalGpuHandle gpuHandle, StringBuilder name);
moel@1
   270
moel@1
   271
    public delegate NvStatus NvAPI_GPU_GetThermalSettingsDelegate(
moel@309
   272
      NvPhysicalGpuHandle gpuHandle, int sensorIndex,
moel@1
   273
      ref NvGPUThermalSettings nvGPUThermalSettings);
moel@1
   274
    public delegate NvStatus NvAPI_EnumNvidiaDisplayHandleDelegate(int thisEnum,
moel@1
   275
      ref NvDisplayHandle displayHandle);
moel@1
   276
    public delegate NvStatus NvAPI_GetPhysicalGPUsFromDisplayDelegate(
moel@309
   277
      NvDisplayHandle displayHandle, [Out] NvPhysicalGpuHandle[] gpuHandles,
moel@140
   278
      out uint gpuCount);
moel@1
   279
    public delegate NvStatus NvAPI_EnumPhysicalGPUsDelegate(
moel@1
   280
      [Out] NvPhysicalGpuHandle[] gpuHandles, out int gpuCount);
moel@38
   281
    public delegate NvStatus NvAPI_GPU_GetTachReadingDelegate(
moel@38
   282
      NvPhysicalGpuHandle gpuHandle, out int value);
moel@140
   283
    public delegate NvStatus NvAPI_GPU_GetAllClocksDelegate(
moel@140
   284
      NvPhysicalGpuHandle gpuHandle, ref NvClocks nvClocks);
moel@140
   285
    public delegate NvStatus NvAPI_GPU_GetPStatesDelegate(
moel@140
   286
      NvPhysicalGpuHandle gpuHandle, ref NvPStates nvPStates);
moel@140
   287
    public delegate NvStatus NvAPI_GPU_GetUsagesDelegate(
moel@140
   288
      NvPhysicalGpuHandle gpuHandle, ref NvUsages nvUsages);
moel@140
   289
    public delegate NvStatus NvAPI_GPU_GetCoolerSettingsDelegate(
moel@140
   290
      NvPhysicalGpuHandle gpuHandle, int coolerIndex,
moel@140
   291
      ref NvGPUCoolerSettings nvGPUCoolerSettings);
moel@309
   292
    public delegate NvStatus NvAPI_GPU_SetCoolerLevelsDelegate(
moel@309
   293
      NvPhysicalGpuHandle gpuHandle, int coolerIndex,
moel@309
   294
      ref NvGPUCoolerLevels NvGPUCoolerLevels);
moel@140
   295
    public delegate NvStatus NvAPI_GPU_GetMemoryInfoDelegate(
moel@140
   296
      NvDisplayHandle displayHandle, ref NvMemoryInfo nvMemoryInfo);
moel@140
   297
    public delegate NvStatus NvAPI_GetDisplayDriverVersionDelegate(
moel@140
   298
      NvDisplayHandle displayHandle, [In, Out] ref NvDisplayDriverVersion
moel@140
   299
      nvDisplayDriverVersion);
moel@140
   300
    public delegate NvStatus NvAPI_GetInterfaceVersionStringDelegate(
moel@140
   301
      StringBuilder version);
moel@1
   302
moel@195
   303
    private static readonly bool available;
moel@195
   304
    private static readonly nvapi_QueryInterfaceDelegate nvapi_QueryInterface;
moel@195
   305
    private static readonly NvAPI_InitializeDelegate NvAPI_Initialize;
moel@309
   306
    private static readonly NvAPI_GPU_GetFullNameDelegate
moel@195
   307
      _NvAPI_GPU_GetFullName;
moel@195
   308
    private static readonly NvAPI_GetInterfaceVersionStringDelegate
moel@140
   309
      _NvAPI_GetInterfaceVersionString;
moel@1
   310
moel@309
   311
    public static readonly NvAPI_GPU_GetThermalSettingsDelegate
moel@1
   312
      NvAPI_GPU_GetThermalSettings;
moel@140
   313
    public static readonly NvAPI_EnumNvidiaDisplayHandleDelegate
moel@1
   314
      NvAPI_EnumNvidiaDisplayHandle;
moel@140
   315
    public static readonly NvAPI_GetPhysicalGPUsFromDisplayDelegate
moel@1
   316
      NvAPI_GetPhysicalGPUsFromDisplay;
moel@140
   317
    public static readonly NvAPI_EnumPhysicalGPUsDelegate
moel@1
   318
      NvAPI_EnumPhysicalGPUs;
moel@140
   319
    public static readonly NvAPI_GPU_GetTachReadingDelegate
moel@38
   320
      NvAPI_GPU_GetTachReading;
moel@140
   321
    public static readonly NvAPI_GPU_GetAllClocksDelegate
moel@140
   322
      NvAPI_GPU_GetAllClocks;
moel@140
   323
    public static readonly NvAPI_GPU_GetPStatesDelegate
moel@140
   324
      NvAPI_GPU_GetPStates;
moel@140
   325
    public static readonly NvAPI_GPU_GetUsagesDelegate
moel@140
   326
      NvAPI_GPU_GetUsages;
moel@140
   327
    public static readonly NvAPI_GPU_GetCoolerSettingsDelegate
moel@140
   328
      NvAPI_GPU_GetCoolerSettings;
moel@309
   329
    public static readonly NvAPI_GPU_SetCoolerLevelsDelegate
moel@309
   330
      NvAPI_GPU_SetCoolerLevels;
moel@140
   331
    public static readonly NvAPI_GPU_GetMemoryInfoDelegate
moel@140
   332
      NvAPI_GPU_GetMemoryInfo;
moel@140
   333
    public static readonly NvAPI_GetDisplayDriverVersionDelegate
moel@167
   334
      NvAPI_GetDisplayDriverVersion;
moel@167
   335
moel@167
   336
    private NVAPI() { }
moel@1
   337
moel@1
   338
    public static NvStatus NvAPI_GPU_GetFullName(NvPhysicalGpuHandle gpuHandle,
moel@1
   339
      out string name) {
moel@1
   340
      StringBuilder builder = new StringBuilder(SHORT_STRING_MAX);
moel@101
   341
      NvStatus status;
moel@101
   342
      if (_NvAPI_GPU_GetFullName != null)
moel@101
   343
        status = _NvAPI_GPU_GetFullName(gpuHandle, builder);
moel@101
   344
      else
moel@101
   345
        status = NvStatus.FUNCTION_NOT_FOUND;
moel@1
   346
      name = builder.ToString();
moel@1
   347
      return status;
moel@1
   348
    }
moel@1
   349
moel@140
   350
    public static NvStatus NvAPI_GetInterfaceVersionString(out string version) {
moel@140
   351
      StringBuilder builder = new StringBuilder(SHORT_STRING_MAX);
moel@140
   352
      NvStatus status;
moel@140
   353
      if (_NvAPI_GetInterfaceVersionString != null)
moel@140
   354
        status = _NvAPI_GetInterfaceVersionString(builder);
moel@140
   355
      else
moel@140
   356
        status = NvStatus.FUNCTION_NOT_FOUND;
moel@140
   357
      version = builder.ToString();
moel@140
   358
      return status;
moel@140
   359
    }
moel@140
   360
moel@1
   361
    private static string GetDllName() {
moel@1
   362
      if (IntPtr.Size == 4) {
moel@1
   363
        return "nvapi.dll";
moel@1
   364
      } else {
moel@1
   365
        return "nvapi64.dll";
moel@1
   366
      }
moel@1
   367
    }
moel@1
   368
moel@309
   369
    private static void GetDelegate<T>(uint id, out T newDelegate)
moel@309
   370
      where T : class {
moel@1
   371
      IntPtr ptr = nvapi_QueryInterface(id);
moel@38
   372
      if (ptr != IntPtr.Zero) {
moel@38
   373
        newDelegate =
moel@38
   374
          Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)) as T;
moel@38
   375
      } else {
moel@38
   376
        newDelegate = null;
moel@38
   377
      }
moel@1
   378
    }
moel@1
   379
moel@309
   380
    static NVAPI() {
moel@1
   381
      DllImportAttribute attribute = new DllImportAttribute(GetDllName());
moel@1
   382
      attribute.CallingConvention = CallingConvention.Cdecl;
moel@1
   383
      attribute.PreserveSig = true;
moel@1
   384
      attribute.EntryPoint = "nvapi_QueryInterface";
moel@1
   385
      PInvokeDelegateFactory.CreateDelegate(attribute,
moel@1
   386
        out nvapi_QueryInterface);
moel@1
   387
moel@1
   388
      try {
moel@1
   389
        GetDelegate(0x0150E828, out NvAPI_Initialize);
moel@1
   390
      } catch (DllNotFoundException) { return; } 
moel@121
   391
        catch (EntryPointNotFoundException) { return; } 
moel@1
   392
        catch (ArgumentNullException) { return; }
moel@1
   393
moel@1
   394
      if (NvAPI_Initialize() == NvStatus.OK) {
moel@1
   395
        GetDelegate(0xE3640A56, out NvAPI_GPU_GetThermalSettings);
moel@1
   396
        GetDelegate(0xCEEE8E9F, out _NvAPI_GPU_GetFullName);
moel@1
   397
        GetDelegate(0x9ABDD40D, out NvAPI_EnumNvidiaDisplayHandle);
moel@1
   398
        GetDelegate(0x34EF9506, out NvAPI_GetPhysicalGPUsFromDisplay);
moel@1
   399
        GetDelegate(0xE5AC921F, out NvAPI_EnumPhysicalGPUs);
moel@140
   400
        GetDelegate(0x5F608315, out NvAPI_GPU_GetTachReading);
moel@140
   401
        GetDelegate(0x1BD69F49, out NvAPI_GPU_GetAllClocks);
moel@140
   402
        GetDelegate(0x60DED2ED, out NvAPI_GPU_GetPStates);
moel@140
   403
        GetDelegate(0x189A1FDF, out NvAPI_GPU_GetUsages);
moel@140
   404
        GetDelegate(0xDA141340, out NvAPI_GPU_GetCoolerSettings);
moel@309
   405
        GetDelegate(0x891FA0AE, out NvAPI_GPU_SetCoolerLevels);
moel@140
   406
        GetDelegate(0x774AA982, out NvAPI_GPU_GetMemoryInfo);
moel@140
   407
        GetDelegate(0xF951A4D1, out NvAPI_GetDisplayDriverVersion);
moel@140
   408
        GetDelegate(0x01053FA5, out _NvAPI_GetInterfaceVersionString);
moel@140
   409
moel@1
   410
        available = true;
moel@1
   411
      }
moel@1
   412
    }
moel@1
   413
moel@1
   414
    public static bool IsAvailable {
moel@1
   415
      get { return available; }
moel@1
   416
    }
moel@1
   417
moel@1
   418
  }
moel@1
   419
}