feature/laptop #2

Merged
king merged 4 commits from feature/laptop into master 2025-04-30 17:08:10 +08:00
2 changed files with 85 additions and 19 deletions
Showing only changes of commit caa7436d51 - Show all commits
+9
View File
@@ -9,6 +9,15 @@ public static class NvmlWrapper
[DllImport("nvml.dll", EntryPoint = "nvmlShutdown")] [DllImport("nvml.dll", EntryPoint = "nvmlShutdown")]
public static extern int NvmlShutdown(); public static extern int NvmlShutdown();
// Get device count
[DllImport("nvml.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int nvmlDeviceGetCount_v2(ref uint deviceCount);
public static int NvmlDeviceGetCount(ref uint deviceCount)
{
return nvmlDeviceGetCount_v2(ref deviceCount);
}
[DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetHandleByIndex_v2")] [DllImport("nvml.dll", EntryPoint = "nvmlDeviceGetHandleByIndex_v2")]
public static extern int NvmlDeviceGetHandleByIndex(int index, out IntPtr device); public static extern int NvmlDeviceGetHandleByIndex(int index, out IntPtr device);
+76 -19
View File
@@ -190,26 +190,83 @@ namespace ResourceMonitorService
private object GetGpuUsage() private object GetGpuUsage()
{ {
NvmlWrapper.NvmlInit(); if (!IsNvidiaGpuPresent())
IntPtr device;
NvmlWrapper.NvmlDeviceGetHandleByIndex(0, out device);
NvmlWrapper.NvmlUtilization utilization;
NvmlWrapper.NvmlDeviceGetUtilizationRates(device, out utilization);
uint temperature;
NvmlWrapper.NvmlDeviceGetTemperature(device, 0, out temperature);
uint fanSpeed;
NvmlWrapper.NvmlDeviceGetFanSpeed(device, out fanSpeed);
NvmlWrapper.NvmlShutdown();
return new
{ {
Usage = utilization.Gpu, return new
Temperature = temperature, {
FanSpeed = fanSpeed Usage = 0,
}; Temperature = 0,
FanSpeed = 0,
IsAvailable = false,
Message = "No NVIDIA GPU detected"
};
}
try
{
NvmlWrapper.NvmlInit();
IntPtr device;
NvmlWrapper.NvmlDeviceGetHandleByIndex(0, out device);
NvmlWrapper.NvmlUtilization utilization;
NvmlWrapper.NvmlDeviceGetUtilizationRates(device, out utilization);
uint temperature;
NvmlWrapper.NvmlDeviceGetTemperature(device, 0, out temperature);
uint fanSpeed;
NvmlWrapper.NvmlDeviceGetFanSpeed(device, out fanSpeed);
NvmlWrapper.NvmlShutdown();
return new
{
Usage = utilization.Gpu,
Temperature = temperature,
FanSpeed = fanSpeed
};
}
catch (Exception ex)
{
return new
{
Usage = 0,
Temperature = 0,
FanSpeed = 0,
IsAvailable = false,
Error = ex.Message
};
}
}
private bool IsNvidiaGpuPresent()
{
try
{
// Method 1: Try to initialize NVML
NvmlWrapper.NvmlInit();
uint deviceCount = 0;
NvmlWrapper.NvmlDeviceGetCount(ref deviceCount);
NvmlWrapper.NvmlShutdown();
return deviceCount > 0;
}
catch
{
// Method 2: Fallback to checking using WMI
try
{
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController WHERE Name LIKE '%NVIDIA%'"))
{
var collection = searcher.Get();
return collection.Count > 0;
}
}
catch
{
return false;
}
}
} }
private object GetCurrentlyRunningGame() private object GetCurrentlyRunningGame()