Skip to content

Commit

Permalink
Discarded WMI API when running device not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Oct 30, 2022
1 parent 7dc2db6 commit c83c897
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 73 deletions.
2 changes: 1 addition & 1 deletion build/nsis/setup.nsi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
!define PRODUCT_NAME "GenshinWoodmen"
!define PRODUCT_VERSION "1.6.4.0"
!define PRODUCT_VERSION "1.6.5.0"
!define PRODUCT_PUBLISHER "GenshinMatrix"
!define PRODUCT_WEB_SITE "https://github.com/genshin-matrix"
!define PRODUCT_LEGAL "Licensed under MIT"
Expand Down
93 changes: 29 additions & 64 deletions src/GenshinWoodmen/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,6 @@ internal static class NativeMethods
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("dxva2.dll")]
public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);

[DllImport("dxva2.dll")]
public static extern bool SetMonitorBrightness(IntPtr hMonitor, short brightness);

[DllImport("dxva2.dll")]
public static extern bool GetMonitorBrightness(IntPtr hMonitor, ref short pdwMinimumBrightness, ref short pdwCurrentBrightness, ref short pdwMaximumBrightness);

[DllImport("user32.dll")]
public static extern IntPtr MonitorFromPoint(POINT pt, uint dwFlags);

Expand Down Expand Up @@ -230,71 +221,45 @@ public static IntPtr GetCurrentMonitor()
return MonitorFromPoint(p, MONITOR_DEFAULTTOPRIMARY);
}

[Obsolete]
public static void SetMonitorBrightness(short brightness)
{
IntPtr hMonitor = GetCurrentMonitor();
_ = SetMonitorBrightness(hMonitor, brightness);
}

[Obsolete]
public static short GetMonitorBrightness()
{
IntPtr hMonitor = GetCurrentMonitor();
short pdwMinimumBrightness = default;
short pdwCurrentBrightness = default;
short pdwMaximumBrightness = default;
_ = GetMonitorBrightness(hMonitor, ref pdwMinimumBrightness, ref pdwCurrentBrightness, ref pdwMaximumBrightness);
return pdwCurrentBrightness;
}

[Obsolete]
public static short GetMonitorMinimumBrightness()
{
IntPtr hMonitor = GetCurrentMonitor();
short pdwMinimumBrightness = default;
short pdwCurrentBrightness = default;
short pdwMaximumBrightness = default;
_ = GetMonitorBrightness(hMonitor, ref pdwMinimumBrightness, ref pdwCurrentBrightness, ref pdwMaximumBrightness);
return pdwMinimumBrightness;
}

[Obsolete]
public static short GetMonitorMaximumBrightness()
{
IntPtr hMonitor = GetCurrentMonitor();
short pdwMinimumBrightness = default;
short pdwCurrentBrightness = default;
short pdwMaximumBrightness = default;
_ = GetMonitorBrightness(hMonitor, ref pdwMinimumBrightness, ref pdwCurrentBrightness, ref pdwMaximumBrightness);
return pdwMaximumBrightness;
}

public static byte GetBrightness()
{
ManagementScope scope = new(@"root\WMI");
ObjectQuery query = new("SELECT * FROM WmiMonitorBrightness");
using ManagementObjectSearcher searcher = new(scope, query);
using ManagementObjectCollection objectCollection = searcher.Get();

foreach (ManagementObject mObj in objectCollection)
try
{
return byte.Parse(mObj["CurrentBrightness"].ToString()!);
ManagementScope scope = new(@"root\WMI");
ObjectQuery query = new("SELECT * FROM WmiMonitorBrightness");
using ManagementObjectSearcher searcher = new(scope, query);
using ManagementObjectCollection objectCollection = searcher.Get();

foreach (ManagementObject mObj in objectCollection)
{
return byte.Parse(mObj["CurrentBrightness"].ToString()!);
}
}
catch (Exception e)
{
Logger.Error(e);
}
return 0x00;
}

public static void SetBrightness(byte targetBrightness)
{
ManagementScope scope = new(@"root\WMI");
SelectQuery query = new("WmiMonitorBrightnessMethods");
using ManagementObjectSearcher searcher = new(scope, query);
using ManagementObjectCollection objectCollection = searcher.Get();

foreach (ManagementObject mObj in objectCollection)
try
{
ManagementScope scope = new(@"root\WMI");
SelectQuery query = new("WmiMonitorBrightnessMethods");
using ManagementObjectSearcher searcher = new(scope, query);
using ManagementObjectCollection objectCollection = searcher.Get();

foreach (ManagementObject mObj in objectCollection)
{
mObj.InvokeMethod("WmiSetBrightness", new object[] { uint.MaxValue, targetBrightness });
break;
}
}
catch (Exception e)
{
mObj.InvokeMethod("WmiSetBrightness", new object[] { uint.MaxValue, targetBrightness });
break;
Logger.Error(e);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/GenshinWoodmen/GenshinWoodmen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<TargetFramework>net6.0-windows10.0.18362.0</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<UseWinUI>true</UseWinUI>
<UseWindowsForms>true</UseWindowsForms>
<UseWinUI>false</UseWinUI>
<ApplicationIcon>Resources\favcion.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x64</Platforms>
<UseWindowsForms>True</UseWindowsForms>
<AssemblyVersion>1.6.4</AssemblyVersion>
<FileVersion>1.6.4</FileVersion>
<Version>$(VersionPrefix)1.6.4</Version>
<AssemblyVersion>1.6.5</AssemblyVersion>
<FileVersion>1.6.5</FileVersion>
<Version>$(VersionPrefix)1.6.5</Version>
<Authors>GenshinMatrix</Authors>
<Company>GenshinMatrix</Company>
</PropertyGroup>
Expand Down Expand Up @@ -69,7 +69,7 @@
<PackageReference Include="NAudio.Wasapi" Version="2.1.0" />
<PackageReference Include="System.IO.Abstractions" Version="17.2.3" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="YamlDotNet" Version="12.0.1" />
<PackageReference Include="YamlDotNet" Version="12.0.2" />
<PackageReference Include="SharpVectors" Version="1.8.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/GenshinWoodmenSetup/GenshinWoodmenSetup.wapproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
</ItemGroup>
<Import Project="$(WapProjPath)\Microsoft.DesktopBridge.targets" />
<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GenshinWoodmen\GenshinWoodmen.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/GenshinWoodmenSetup/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Identity
Name="c17bc862-e10e-4618-b717-e47dd2bf65c8"
Publisher="CN=ema"
Version="1.6.4.0" />
Version="1.6.5.0" />

<Properties>
<DisplayName>GenshinWoodmen</DisplayName>
Expand Down

0 comments on commit c83c897

Please sign in to comment.