From 47f33f91d9e9cbf8e39d794826c551747839869b Mon Sep 17 00:00:00 2001 From: PhyxionNL <7643972+PhyxionNL@users.noreply.github.com> Date: Wed, 29 Jun 2022 18:17:49 +0200 Subject: [PATCH] Update Interop [ci skip] --- LibreHardwareMonitorLib/Interop/AdvApi32.cs | 7 +++ LibreHardwareMonitorLib/Interop/AtiAdlxx.cs | 38 ++++++++++++-- LibreHardwareMonitorLib/Interop/CfgMgr32.cs | 6 ++- LibreHardwareMonitorLib/Interop/Ftd2xx.cs | 17 +++++-- LibreHardwareMonitorLib/Interop/Gdi32.cs | 6 ++- LibreHardwareMonitorLib/Interop/Kernel32.cs | 50 ++++++++++++++---- LibreHardwareMonitorLib/Interop/NtDll.cs | 1 + LibreHardwareMonitorLib/Interop/NvApi.cs | 42 ++++++++++------ LibreHardwareMonitorLib/Interop/NvidiaML.cs | 9 ++-- LibreHardwareMonitorLib/Interop/Ring0.cs | 31 +++--------- LibreHardwareMonitorLib/Interop/SetupApi.cs | 56 ++++++++++++--------- 11 files changed, 173 insertions(+), 90 deletions(-) diff --git a/LibreHardwareMonitorLib/Interop/AdvApi32.cs b/LibreHardwareMonitorLib/Interop/AdvApi32.cs index 7810ddf..792643a 100644 --- a/LibreHardwareMonitorLib/Interop/AdvApi32.cs +++ b/LibreHardwareMonitorLib/Interop/AdvApi32.cs @@ -15,13 +15,16 @@ namespace LibreHardwareMonitor.Interop private const string DllName = "advapi32.dll"; [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr OpenSCManager(string lpMachineName, string lpDatabaseName, SC_MANAGER_ACCESS_MASK dwDesiredAccess); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool CloseServiceHandle(IntPtr hSCObject); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr CreateService ( IntPtr hSCManager, @@ -39,17 +42,21 @@ namespace LibreHardwareMonitor.Interop string lpPassword); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, SERVICE_ACCESS_MASK dwDesiredAccess); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeleteService(IntPtr hService); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool StartService(IntPtr hService, uint dwNumServiceArgs, string[] lpServiceArgVectors); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool ControlService(IntPtr hService, SERVICE_CONTROL dwControl, ref SERVICE_STATUS lpServiceStatus); diff --git a/LibreHardwareMonitorLib/Interop/AtiAdlxx.cs b/LibreHardwareMonitorLib/Interop/AtiAdlxx.cs index 4f6abc5..a735c8c 100644 --- a/LibreHardwareMonitorLib/Interop/AtiAdlxx.cs +++ b/LibreHardwareMonitorLib/Interop/AtiAdlxx.cs @@ -25,6 +25,8 @@ namespace LibreHardwareMonitor.Interop public const int ADL_DRIVER_OK = 0; + public const int ADL_FALSE = 0; + public const int ADL_MAX_ADAPTERS = 40; public const int ADL_MAX_DEVICENAME = 32; public const int ADL_MAX_DISPLAYS = 40; @@ -32,8 +34,6 @@ namespace LibreHardwareMonitor.Interop public const int ADL_MAX_GLSYNC_PORTS = 8; public const int ADL_MAX_NUM_DISPLAYMODES = 1024; public const int ADL_MAX_PATH = 256; - - public const int ADL_FALSE = 0; public const int ADL_TRUE = 1; public const int ATI_VENDOR_ID = 0x1002; @@ -52,84 +52,111 @@ namespace LibreHardwareMonitor.Interop public delegate IntPtr Context(int size); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Main_Control_Create(ADL_Main_Memory_AllocDelegate callback, int enumConnectedAdapters); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Main_Control_Destroy(); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Adapter_AdapterInfo_Get(IntPtr info, int size); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Adapter_NumberOfAdapters_Get(); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Adapter_NumberOfAdapters_Get(ref int numAdapters); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Adapter_ID_Get(int adapterIndex, out int adapterId); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Display_AdapterID_Get(int adapterIndex, out int adapterId); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Adapter_Active_Get(int adapterIndex, out int status); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Overdrive5_ODParameters_Get(int adapterIndex, out ADLODParameters parameters); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Overdrive5_CurrentActivity_Get(int iAdapterIndex, ref ADLPMActivity activity); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Overdrive5_Temperature_Get(int adapterIndex, int thermalControllerIndex, ref ADLTemperature temperature); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_OverdriveN_Temperature_Get(IntPtr context, int adapterIndex, ADLODNTemperatureType iTemperatureType, ref int temp); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Overdrive5_FanSpeed_Get(int adapterIndex, int thermalControllerIndex, ref ADLFanSpeedValue fanSpeedValue); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Overdrive5_FanSpeedInfo_Get(int adapterIndex, int thermalControllerIndex, ref ADLFanSpeedInfo fanSpeedInfo); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Overdrive5_FanSpeedToDefault_Set(int adapterIndex, int thermalControllerIndex); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Overdrive5_FanSpeed_Set(int adapterIndex, int thermalControllerIndex, ref ADLFanSpeedValue fanSpeedValue); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_OverdriveN_PerformanceStatus_Get(IntPtr context, int adapterIndex, out ADLODNPerformanceStatus performanceStatus); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Overdrive_Caps(int adapterIndex, ref int supported, ref int enabled, ref int version); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_Overdrive6_CurrentPower_Get(IntPtr context, int adapterIndex, ADLODNCurrentPowerType powerType, ref int currentValue); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_Main_Control_Create(ADL_Main_Memory_AllocDelegate callback, int connectedAdapters, ref IntPtr context); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_Main_Control_Destroy(IntPtr context); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_New_QueryPMLogData_Get(IntPtr context, int adapterIndex, ref ADLPMLogDataOutput aDLPMLogDataOutput); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL_Graphics_Versions_Get(out ADLVersionsInfo versionInfo); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_Adapter_FrameMetrics_Caps(IntPtr context, int adapterIndex, ref int supported); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_Adapter_FrameMetrics_Get(IntPtr context, int adapterIndex, int displayIndex, ref float fps); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_Adapter_FrameMetrics_Start(IntPtr context, int adapterIndex, int displayIndex); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern ADLStatus ADL2_Adapter_FrameMetrics_Stop(IntPtr context, int adapterIndex, int displayIndex); public static bool ADL_Method_Exists(string ADL_Method) @@ -141,6 +168,7 @@ namespace LibreHardwareMonitor.Interop Kernel32.FreeLibrary(module); return result; } + return false; } @@ -148,7 +176,7 @@ namespace LibreHardwareMonitor.Interop { try { - return Kernel32.LoadLibrary(DllName) != IntPtr.Zero ? ADL_Main_Control_Create(Main_Memory_Alloc, enumConnectedAdapters) : ADLStatus.ADL_ERR; + return ADL_Method_Exists(nameof(ADL_Main_Control_Create)) ? ADL_Main_Control_Create(Main_Memory_Alloc, enumConnectedAdapters) : ADLStatus.ADL_ERR; } catch { @@ -161,7 +189,7 @@ namespace LibreHardwareMonitor.Interop int elementSize = Marshal.SizeOf(typeof(ADLAdapterInfo)); int size = info.Length * elementSize; IntPtr ptr = Marshal.AllocHGlobal(size); - var result = ADL_Adapter_AdapterInfo_Get(ptr, size); + ADLStatus result = ADL_Adapter_AdapterInfo_Get(ptr, size); for (int i = 0; i < info.Length; i++) info[i] = (ADLAdapterInfo)Marshal.PtrToStructure((IntPtr)((long)ptr + i * elementSize), typeof(ADLAdapterInfo)); @@ -382,7 +410,7 @@ namespace LibreHardwareMonitor.Interop public int FanSpeed; public int Flags; } - + [StructLayout(LayoutKind.Sequential)] internal struct ADLFanSpeedInfo { diff --git a/LibreHardwareMonitorLib/Interop/CfgMgr32.cs b/LibreHardwareMonitorLib/Interop/CfgMgr32.cs index b5ba559..9013c35 100644 --- a/LibreHardwareMonitorLib/Interop/CfgMgr32.cs +++ b/LibreHardwareMonitorLib/Interop/CfgMgr32.cs @@ -15,14 +15,16 @@ namespace LibreHardwareMonitor.Interop { internal const uint CM_GET_DEVICE_INTERFACE_LIST_PRESENT = 0; internal const int CR_SUCCESS = 0; - internal static Guid GUID_DISPLAY_DEVICE_ARRIVAL = new("1CA05180-A699-450A-9A0C-DE4FBE3DDD89"); - + internal const string DllName = "CfgMgr32.dll"; + internal static Guid GUID_DISPLAY_DEVICE_ARRIVAL = new("1CA05180-A699-450A-9A0C-DE4FBE3DDD89"); [DllImport(DllName, CharSet = CharSet.Unicode)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern uint CM_Get_Device_Interface_List_Size(out uint size, ref Guid interfaceClassGuid, string deviceID, uint flags); [DllImport(DllName, CharSet = CharSet.Unicode)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern uint CM_Get_Device_Interface_List(ref Guid interfaceClassGuid, string deviceID, char[] buffer, uint bufferLength, uint flags); } } diff --git a/LibreHardwareMonitorLib/Interop/Ftd2xx.cs b/LibreHardwareMonitorLib/Interop/Ftd2xx.cs index 23c7e17..f91ba8f 100644 --- a/LibreHardwareMonitorLib/Interop/Ftd2xx.cs +++ b/LibreHardwareMonitorLib/Interop/Ftd2xx.cs @@ -20,48 +20,60 @@ namespace LibreHardwareMonitor.Interop if (module == IntPtr.Zero) return false; - Kernel32.FreeLibrary(module); return true; } [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_CreateDeviceInfoList(out uint numDevices); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_GetDeviceInfoList([Out] FT_DEVICE_INFO_NODE[] deviceInfoNodes, ref uint length); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_Open(int device, out FT_HANDLE handle); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_Close(FT_HANDLE handle); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_SetBaudRate(FT_HANDLE handle, uint baudRate); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_SetDataCharacteristics(FT_HANDLE handle, byte wordLength, byte stopBits, byte parity); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_SetFlowControl(FT_HANDLE handle, FT_FLOW_CONTROL flowControl, byte xon, byte xoff); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_SetTimeouts(FT_HANDLE handle, uint readTimeout, uint writeTimeout); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_Write(FT_HANDLE handle, byte[] buffer, uint bytesToWrite, out uint bytesWritten); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_Purge(FT_HANDLE handle, FT_PURGE mask); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_GetStatus(FT_HANDLE handle, out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_Read(FT_HANDLE handle, [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern FT_STATUS FT_ReadByte(FT_HANDLE handle, out byte buffer, uint bytesToRead, out uint bytesReturned); public static FT_STATUS Write(FT_HANDLE handle, byte[] buffer) @@ -70,7 +82,6 @@ namespace LibreHardwareMonitor.Interop if (bytesWritten != buffer.Length) return FT_STATUS.FT_FAILED_TO_WRITE_DEVICE; - return status; } @@ -79,7 +90,6 @@ namespace LibreHardwareMonitor.Interop if (FT_GetStatus(handle, out uint amountInRxQueue, out uint _, out uint _) == FT_STATUS.FT_OK) return (int)amountInRxQueue; - return 0; } @@ -89,7 +99,6 @@ namespace LibreHardwareMonitor.Interop if (status != FT_STATUS.FT_OK || bytesReturned != 1) throw new InvalidOperationException(); - return buffer; } diff --git a/LibreHardwareMonitorLib/Interop/Gdi32.cs b/LibreHardwareMonitorLib/Interop/Gdi32.cs index 1f01eb2..8fab398 100644 --- a/LibreHardwareMonitorLib/Interop/Gdi32.cs +++ b/LibreHardwareMonitorLib/Interop/Gdi32.cs @@ -13,17 +13,21 @@ namespace LibreHardwareMonitor.Interop internal static class Gdi32 { internal const string DllName = "Gdi32.dll"; - + [DllImport(DllName, ExactSpelling = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern uint D3DKMTCloseAdapter(ref D3dkmth.D3DKMT_CLOSEADAPTER closeAdapter); [DllImport(DllName, ExactSpelling = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern uint D3DKMTOpenAdapterFromDeviceName(ref D3dkmth.D3DKMT_OPENADAPTERFROMDEVICENAME openAdapterFromDeviceName); [DllImport(DllName, ExactSpelling = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern uint D3DKMTQueryAdapterInfo(ref D3dkmth.D3DKMT_QUERYADAPTERINFO queryAdapterInfo); [DllImport(DllName, ExactSpelling = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern uint D3DKMTQueryStatistics(ref D3dkmth.D3DKMT_QUERYSTATISTICS queryStatistics); } } diff --git a/LibreHardwareMonitorLib/Interop/Kernel32.cs b/LibreHardwareMonitorLib/Interop/Kernel32.cs index abb29f1..30887e3 100644 --- a/LibreHardwareMonitorLib/Interop/Kernel32.cs +++ b/LibreHardwareMonitorLib/Interop/Kernel32.cs @@ -17,20 +17,20 @@ namespace LibreHardwareMonitor.Interop public const int ERROR_SERVICE_ALREADY_RUNNING = unchecked((int)0x80070420); public const int ERROR_SERVICE_EXISTS = unchecked((int)0x80070431); + + internal const uint BATTERY_UNKNOWN_TIME = 0xFFFFFFFF; internal const string IntelNVMeMiniPortSignature1 = "NvmeMini"; internal const string IntelNVMeMiniPortSignature2 = "IntelNvm"; + internal const uint LPTR = 0x0000 | 0x0040; + internal const int MAX_DRIVE_ATTRIBUTES = 512; internal const uint NVME_PASS_THROUGH_SRB_IO_CODE = 0xe0002000; internal const byte SMART_LBA_HI = 0xC2; - internal const byte SMART_LBA_MID = 0x4F; internal const byte SMART_LBA_HI_EXCEEDED = 0x2C; + internal const byte SMART_LBA_MID = 0x4F; internal const byte SMART_LBA_MID_EXCEEDED = 0xF4; - internal const uint LPTR = 0x0000 | 0x0040; - - internal const uint BATTERY_UNKNOWN_TIME = 0xFFFFFFFF; - private const string DllName = "kernel32.dll"; [Flags] @@ -90,10 +90,12 @@ namespace LibreHardwareMonitor.Interop } [DllImport(DllName, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern SafeFileHandle CreateFile ( [MarshalAs(UnmanagedType.LPTStr)] string lpFileName, @@ -105,6 +107,7 @@ namespace LibreHardwareMonitor.Interop IntPtr hTemplateFile); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -118,6 +121,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -131,6 +135,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -144,6 +149,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -157,6 +163,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -170,6 +177,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -183,6 +191,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -196,6 +205,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -209,6 +219,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -222,6 +233,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -235,6 +247,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -248,6 +261,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -261,6 +275,7 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool DeviceIoControl ( @@ -274,58 +289,73 @@ namespace LibreHardwareMonitor.Interop IntPtr lpOverlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr LocalAlloc(uint uFlags, ulong uBytes); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr LocalFree(IntPtr hMem); [DllImport(DllName, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern void RtlZeroMemory(IntPtr Destination, int Length); [DllImport(DllName, SetLastError = false)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern void RtlCopyMemory(IntPtr Destination, IntPtr Source, uint Length); [DllImport(DllName, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr LoadLibrary(string lpFileName); [DllImport(DllName, ExactSpelling = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr GetProcAddress(IntPtr module, string methodName); [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool FreeLibrary(IntPtr module); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern UIntPtr SetThreadAffinityMask(IntPtr handle, UIntPtr mask); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr GetCurrentThread(); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] - public static extern ushort GetActiveProcessorGroupCount(); + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + internal static extern ushort GetActiveProcessorGroupCount(); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] - public static extern bool SetThreadGroupAffinity(IntPtr thread, ref GROUP_AFFINITY groupAffinity, out GROUP_AFFINITY previousGroupAffinity); + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + internal static extern bool SetThreadGroupAffinity(IntPtr thread, ref GROUP_AFFINITY groupAffinity, out GROUP_AFFINITY previousGroupAffinity); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, MEM flAllocationType, PAGE flProtect); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern bool VirtualFree(IntPtr lpAddress, UIntPtr dwSize, MEM dwFreeType); [DllImport(DllName, CallingConvention = CallingConvention.Winapi)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern bool DeviceIoControl ( SafeFileHandle device, IOControlCode ioControlCode, - [MarshalAs(UnmanagedType.AsAny)][In] object inBuffer, + [MarshalAs(UnmanagedType.AsAny)] [In] object inBuffer, uint inBufferSize, - [MarshalAs(UnmanagedType.AsAny)][Out] object outBuffer, + [MarshalAs(UnmanagedType.AsAny)] [Out] object outBuffer, uint nOutBufferSize, out uint bytesReturned, IntPtr overlapped); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern IntPtr CreateFile ( string lpFileName, @@ -337,9 +367,11 @@ namespace LibreHardwareMonitor.Interop IntPtr hTemplateFile); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern int EnumSystemFirmwareTables(Provider firmwareTableProviderSignature, IntPtr firmwareTableBuffer, int bufferSize); [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern int GetSystemFirmwareTable(Provider firmwareTableProviderSignature, int firmwareTableID, IntPtr firmwareTableBuffer, int bufferSize); [StructLayout(LayoutKind.Sequential, Pack = 4)] diff --git a/LibreHardwareMonitorLib/Interop/NtDll.cs b/LibreHardwareMonitorLib/Interop/NtDll.cs index 969296e..9a78bc1 100644 --- a/LibreHardwareMonitorLib/Interop/NtDll.cs +++ b/LibreHardwareMonitorLib/Interop/NtDll.cs @@ -76,6 +76,7 @@ namespace LibreHardwareMonitor.Interop } [DllImport(DllName)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] internal static extern int NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, [Out] SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[] SystemInformation, int SystemInformationLength, out IntPtr ReturnLength); } } diff --git a/LibreHardwareMonitorLib/Interop/NvApi.cs b/LibreHardwareMonitorLib/Interop/NvApi.cs index 6aa3146..6dbd24b 100644 --- a/LibreHardwareMonitorLib/Interop/NvApi.cs +++ b/LibreHardwareMonitorLib/Interop/NvApi.cs @@ -17,10 +17,13 @@ namespace LibreHardwareMonitor.Interop public const int MAX_POWER_TOPOLOGIES = 4; public const int MAX_THERMAL_SENSORS_PER_GPU = 3; public const int MAX_USAGES_PER_GPU = 8; + + public const int SHORT_STRING_MAX = 64; public const int THERMAL_SENSOR_RESERVED_COUNT = 8; public const int THERMAL_SENSOR_TEMPERATURE_COUNT = 32; - public const int SHORT_STRING_MAX = 64; + private const string DllName = "nvapi.dll"; + private const string DllName64 = "nvapi64.dll"; public static readonly NvAPI_EnumNvidiaDisplayHandleDelegate NvAPI_EnumNvidiaDisplayHandle; public static readonly NvAPI_EnumPhysicalGPUsDelegate NvAPI_EnumPhysicalGPUs; @@ -52,17 +55,12 @@ namespace LibreHardwareMonitor.Interop try { + if (!DllExists()) + return; + GetDelegate(0x0150E828, out nvApiInitialize); } - catch (DllNotFoundException) - { - return; - } - catch (EntryPointNotFoundException) - { - return; - } - catch (ArgumentNullException) + catch (Exception e) when (e is DllNotFoundException or ArgumentNullException or EntryPointNotFoundException or BadImageFormatException) { return; } @@ -147,6 +145,9 @@ namespace LibreHardwareMonitor.Interop [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate NvStatus NvAPI_GPU_GetTachReadingDelegate(NvPhysicalGpuHandle gpuHandle, out int value); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate NvStatus NvAPI_GPU_GetThermalSensorsDelegate(NvPhysicalGpuHandle gpuHandle, ref NvThermalSensors nvThermalSensors); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate NvStatus NvAPI_GPU_GetThermalSettingsDelegate(NvPhysicalGpuHandle gpuHandle, int sensorIndex, ref NvThermalSettings NvThermalSettings); @@ -156,9 +157,6 @@ namespace LibreHardwareMonitor.Interop [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate NvStatus NvAPI_GPU_SetCoolerLevelsDelegate(NvPhysicalGpuHandle gpuHandle, int coolerIndex, ref NvCoolerLevels NvCoolerLevels); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public delegate NvStatus NvAPI_GPU_GetThermalSensorsDelegate(NvPhysicalGpuHandle gpuHandle, ref NvThermalSensors nvThermalSensors); - public enum NvFanControlMode : uint { Auto = 0, @@ -192,10 +190,12 @@ namespace LibreHardwareMonitor.Interop public static bool IsAvailable { get; } - [DllImport(@"nvapi.dll", EntryPoint = @"nvapi_QueryInterface", CallingConvention = CallingConvention.Cdecl, PreserveSig = true)] + [DllImport(DllName, EntryPoint = "nvapi_QueryInterface", CallingConvention = CallingConvention.Cdecl, PreserveSig = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] private static extern IntPtr NvAPI32_QueryInterface(uint interfaceId); - [DllImport(@"nvapi64.dll", EntryPoint = @"nvapi_QueryInterface", CallingConvention = CallingConvention.Cdecl, PreserveSig = true)] + [DllImport(DllName64, EntryPoint = "nvapi_QueryInterface", CallingConvention = CallingConvention.Cdecl, PreserveSig = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] private static extern IntPtr NvAPI64_QueryInterface(uint interfaceId); public static NvStatus NvAPI_GPU_GetFullName(NvPhysicalGpuHandle gpuHandle, out string name) @@ -218,7 +218,7 @@ namespace LibreHardwareMonitor.Interop private static void GetDelegate(uint id, out T newDelegate) where T : class { - IntPtr ptr = Environment.Is64BitProcess ? NvAPI64_QueryInterface(id) : NvAPI32_QueryInterface(id); + IntPtr ptr = Environment.Is64BitOperatingSystem ? NvAPI64_QueryInterface(id) : NvAPI32_QueryInterface(id); if (ptr != IntPtr.Zero) newDelegate = Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)) as T; @@ -226,6 +226,16 @@ namespace LibreHardwareMonitor.Interop newDelegate = null; } + public static bool DllExists() + { + IntPtr module = Kernel32.LoadLibrary(Environment.Is64BitOperatingSystem ? DllName64 : DllName); + if (module == IntPtr.Zero) + return false; + + Kernel32.FreeLibrary(module); + return true; + } + internal static int MAKE_NVAPI_VERSION(int ver) { return Marshal.SizeOf() | (ver << 16); diff --git a/LibreHardwareMonitorLib/Interop/NvidiaML.cs b/LibreHardwareMonitorLib/Interop/NvidiaML.cs index 0d60565..b222f50 100644 --- a/LibreHardwareMonitorLib/Interop/NvidiaML.cs +++ b/LibreHardwareMonitorLib/Interop/NvidiaML.cs @@ -21,8 +21,8 @@ namespace LibreHardwareMonitor.Interop private static WindowsNvmlGetHandleDelegate _windowsNvmlDeviceGetHandleByIndex; private static WindowsNvmlGetHandleByPciBusIdDelegate _windowsNvmlDeviceGetHandleByPciBusId; private static WindowsNvmlDeviceGetPcieThroughputDelegate _windowsNvmlDeviceGetPcieThroughputDelegate; - private static WindowsNvmlGetPowerUsageDelegate _windowsNvmlDeviceGetPowerUsage; private static WindowsNvmlDeviceGetPciInfo _windowsNvmlDeviceGetPciInfo; + private static WindowsNvmlGetPowerUsageDelegate _windowsNvmlDeviceGetPowerUsage; private static WindowsNvmlDelegate _windowsNvmlInit; private static WindowsNvmlDelegate _windowsNvmlShutdown; @@ -217,7 +217,6 @@ namespace LibreHardwareMonitor.Interop else return false; - IntPtr nvmlGetHandle = Kernel32.GetProcAddress(_windowsDll, "nvmlDeviceGetHandleByIndex_v2"); if (nvmlGetHandle != IntPtr.Zero) _windowsNvmlDeviceGetHandleByIndex = (WindowsNvmlGetHandleDelegate)Marshal.GetDelegateForFunctionPointer(nvmlGetHandle, typeof(WindowsNvmlGetHandleDelegate)); @@ -236,20 +235,18 @@ namespace LibreHardwareMonitor.Interop else return false; - IntPtr nvmlGetPcieThroughput = Kernel32.GetProcAddress(_windowsDll, "nvmlDeviceGetPcieThroughput"); if (nvmlGetPcieThroughput != IntPtr.Zero) _windowsNvmlDeviceGetPcieThroughputDelegate = (WindowsNvmlDeviceGetPcieThroughputDelegate)Marshal.GetDelegateForFunctionPointer(nvmlGetPcieThroughput, typeof(WindowsNvmlDeviceGetPcieThroughputDelegate)); else return false; - IntPtr nvmlGetHandlePciBus = Kernel32.GetProcAddress(_windowsDll, "nvmlDeviceGetHandleByPciBusId_v2"); if (nvmlGetHandlePciBus != IntPtr.Zero) _windowsNvmlDeviceGetHandleByPciBusId = (WindowsNvmlGetHandleByPciBusIdDelegate)Marshal.GetDelegateForFunctionPointer(nvmlGetHandlePciBus, typeof(WindowsNvmlGetHandleByPciBusIdDelegate)); else return false; - + IntPtr nvmlDeviceGetPciInfo = Kernel32.GetProcAddress(_windowsDll, "nvmlDeviceGetPciInfo_v2"); if (nvmlDeviceGetPciInfo != IntPtr.Zero) _windowsNvmlDeviceGetPciInfo = (WindowsNvmlDeviceGetPciInfo)Marshal.GetDelegateForFunctionPointer(nvmlDeviceGetPciInfo, typeof(WindowsNvmlDeviceGetPciInfo)); @@ -397,7 +394,7 @@ namespace LibreHardwareMonitor.Interop [DllImport(LinuxDllName, EntryPoint = "nvmlDeviceGetPcieThroughput", ExactSpelling = true)] private static extern NvmlReturn nvmlDeviceGetPcieThroughput(NvmlDevice device, NvmlPcieUtilCounter counter, out uint value); - + [DllImport(LinuxDllName, EntryPoint = "nvmlDeviceGetPciInfo_v2")] private static extern NvmlReturn nvmlDeviceGetPciInfo(NvmlDevice device, ref NvmlPciInfo pci); diff --git a/LibreHardwareMonitorLib/Interop/Ring0.cs b/LibreHardwareMonitorLib/Interop/Ring0.cs index 4eaeec9..87ae8bd 100644 --- a/LibreHardwareMonitorLib/Interop/Ring0.cs +++ b/LibreHardwareMonitorLib/Interop/Ring0.cs @@ -17,28 +17,13 @@ namespace LibreHardwareMonitor.Interop private const uint OLS_TYPE = 40000; - public static readonly Kernel32.IOControlCode - IOCTL_OLS_GET_REFCOUNT = new Kernel32.IOControlCode(OLS_TYPE, 0x801, Kernel32.IOControlCode.Access.Any); - - public static readonly Kernel32.IOControlCode - IOCTL_OLS_READ_MSR = new Kernel32.IOControlCode(OLS_TYPE, 0x821, Kernel32.IOControlCode.Access.Any); - - public static readonly Kernel32.IOControlCode - IOCTL_OLS_WRITE_MSR = new Kernel32.IOControlCode(OLS_TYPE, 0x822, Kernel32.IOControlCode.Access.Any); - - public static readonly Kernel32.IOControlCode - IOCTL_OLS_READ_IO_PORT_BYTE = new Kernel32.IOControlCode(OLS_TYPE, 0x833, Kernel32.IOControlCode.Access.Read); - - public static readonly Kernel32.IOControlCode - IOCTL_OLS_WRITE_IO_PORT_BYTE = new Kernel32.IOControlCode(OLS_TYPE, 0x836, Kernel32.IOControlCode.Access.Write); - - public static readonly Kernel32.IOControlCode - IOCTL_OLS_READ_PCI_CONFIG = new Kernel32.IOControlCode(OLS_TYPE, 0x851, Kernel32.IOControlCode.Access.Read); - - public static readonly Kernel32.IOControlCode - IOCTL_OLS_WRITE_PCI_CONFIG = new Kernel32.IOControlCode(OLS_TYPE, 0x852, Kernel32.IOControlCode.Access.Write); - - public static readonly Kernel32.IOControlCode - IOCTL_OLS_READ_MEMORY = new Kernel32.IOControlCode(OLS_TYPE, 0x841, Kernel32.IOControlCode.Access.Read); + public static readonly Kernel32.IOControlCode IOCTL_OLS_GET_REFCOUNT = new(OLS_TYPE, 0x801, Kernel32.IOControlCode.Access.Any); + public static readonly Kernel32.IOControlCode IOCTL_OLS_READ_MSR = new(OLS_TYPE, 0x821, Kernel32.IOControlCode.Access.Any); + public static readonly Kernel32.IOControlCode IOCTL_OLS_WRITE_MSR = new(OLS_TYPE, 0x822, Kernel32.IOControlCode.Access.Any); + public static readonly Kernel32.IOControlCode IOCTL_OLS_READ_IO_PORT_BYTE = new(OLS_TYPE, 0x833, Kernel32.IOControlCode.Access.Read); + public static readonly Kernel32.IOControlCode IOCTL_OLS_WRITE_IO_PORT_BYTE = new(OLS_TYPE, 0x836, Kernel32.IOControlCode.Access.Write); + public static readonly Kernel32.IOControlCode IOCTL_OLS_READ_PCI_CONFIG = new(OLS_TYPE, 0x851, Kernel32.IOControlCode.Access.Read); + public static readonly Kernel32.IOControlCode IOCTL_OLS_WRITE_PCI_CONFIG = new(OLS_TYPE, 0x852, Kernel32.IOControlCode.Access.Write); + public static readonly Kernel32.IOControlCode IOCTL_OLS_READ_MEMORY = new(OLS_TYPE, 0x841, Kernel32.IOControlCode.Access.Read); } } diff --git a/LibreHardwareMonitorLib/Interop/SetupApi.cs b/LibreHardwareMonitorLib/Interop/SetupApi.cs index a7c49c6..a8e2048 100644 --- a/LibreHardwareMonitorLib/Interop/SetupApi.cs +++ b/LibreHardwareMonitorLib/Interop/SetupApi.cs @@ -10,13 +10,41 @@ namespace LibreHardwareMonitor.Interop { internal class SetupApi { + internal const int DIGCF_DEVICEINTERFACE = 0x00000010; + internal const int DIGCF_PRESENT = 0x00000002; internal const int ERROR_INSUFFICIENT_BUFFER = 122; internal const int ERROR_NO_MORE_ITEMS = 259; - internal const int DIGCF_PRESENT = 0x00000002; - internal const int DIGCF_DEVICEINTERFACE = 0x00000010; - internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); - internal static Guid GUID_DEVICE_BATTERY = new Guid(0x72631e54, 0x78A4, 0x11d0, 0xbc, 0xf7, 0x00, 0xaa, 0x00, 0xb7, 0xb3, 0x2a); + private const string DllName = "SetupAPI.dll"; + internal static Guid GUID_DEVICE_BATTERY = new(0x72631e54, 0x78A4, 0x11d0, 0xbc, 0xf7, 0x00, 0xaa, 0x00, 0xb7, 0xb3, 0x2a); + internal static readonly IntPtr INVALID_HANDLE_VALUE = new(-1); + + [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + internal static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, uint Flags); + + [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool SetupDiEnumDeviceInterfaces + (IntPtr DeviceInfoSet, IntPtr DeviceInfoData, ref Guid InterfaceClassGuid, uint MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData); + + [DllImport(DllName, SetLastError = true, EntryPoint = "SetupDiGetDeviceInterfaceDetailW", CharSet = CharSet.Unicode)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool SetupDiGetDeviceInterfaceDetail + ( + IntPtr DeviceInfoSet, + in SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, + [Out, Optional] IntPtr DeviceInterfaceDetailData, + uint DeviceInterfaceDetailDataSize, + out uint RequiredSize, + IntPtr DeviceInfoData = default); + + [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet); [StructLayout(LayoutKind.Sequential)] internal struct SP_DEVICE_INTERFACE_DATA @@ -26,25 +54,5 @@ namespace LibreHardwareMonitor.Interop public uint Flags; public IntPtr Reserved; } - - [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] - internal static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, IntPtr Enumerator, IntPtr hwndParent, uint Flags); - - [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, ref Guid InterfaceClassGuid, uint MemberIndex, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData); - - [DllImport(DllName, SetLastError = true, EntryPoint = "SetupDiGetDeviceInterfaceDetailW", CharSet = CharSet.Unicode)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, - in SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, - [Out, Optional] IntPtr DeviceInterfaceDetailData, - uint DeviceInterfaceDetailDataSize, - out uint RequiredSize, - IntPtr DeviceInfoData = default); - - [DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet); } }