Skip to content

Commit 4988c49

Browse files
committed
动态查找NvidiaApi.dll,避免AMD平台也加载。
1 parent fbd7f61 commit 4988c49

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

Program.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ static void Main(string[] args) {
119119
Application.SetCompatibleTextRenderingDefault(false);
120120

121121
AppDomain.CurrentDomain.AssemblyResolve += ResolveEmbeddedAssembly;
122-
ExtractAndPreloadNativeDll("NvidiaApi.dll");
122+
hasAmdGpu = HasAmdGpu();
123+
if (!hasAmdGpu)
124+
ExtractAndPreloadNativeDll("NvidiaApi.dll");
123125

124126
powerOnline = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online;
125127
monitorQuery();
@@ -129,7 +131,6 @@ static void Main(string[] args) {
129131
alreadyReadCode = new Random(int.Parse(versionString)).Next(1000, 10000);
130132

131133
isTwoBytePL4 = IsTwoBytePL4Supported();
132-
hasAmdGpu = HasAmdGpu();
133134

134135
// Initialize tray icon
135136
platformSettings = LoadPlatformSettingsFromDll();
@@ -191,11 +192,19 @@ static void Main(string[] args) {
191192
}
192193
}
193194

194-
[DllImport("NvidiaApi.dll", CallingConvention = CallingConvention.Cdecl)]
195-
private static extern int NvidiaAPI_SYS_UIControl(bool on);
195+
[DllImport("kernel32.dll", SetLastError = true)]
196+
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
197+
198+
[DllImport("kernel32.dll", SetLastError = true)]
199+
private static extern IntPtr GetModuleHandle(string lpModuleName);
196200

197201
public static int LaunchDDS() {
198-
return NvidiaAPI_SYS_UIControl(true);
202+
IntPtr hModule = GetModuleHandle("NvidiaApi.dll");
203+
if (hModule == IntPtr.Zero) return -1;
204+
IntPtr proc = GetProcAddress(hModule, "NvidiaAPI_SYS_UIControl");
205+
if (proc == IntPtr.Zero) return -1;
206+
var fn = Marshal.GetDelegateForFunctionPointer<Func<bool, int>>(proc);
207+
return fn(true);
199208
}
200209

201210
public static bool HasAmdGpu() {

0 commit comments

Comments
 (0)