Skip to content

Commit

Permalink
Add support for newer versions of powerbattery.dll
Browse files Browse the repository at this point in the history
  • Loading branch information
reagcz committed Nov 17, 2022
1 parent 5781e0e commit e03fbfd
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions IdeapadToolkit/Services/LenovoPowerSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ public class LenovoPowerSettingsService : ILenovoPowerSettingsService
[DllImport("PowerBattery.dll", EntryPoint = "?GetChargingMode@CChargingMode@PowerBattery@@QEBAHXZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int GetChargingMode(ref CChargingMode var1);


[DllImport("PowerBattery.dll", EntryPoint = "?SetChargingMode@CChargingMode@PowerBattery@@QEBAHH@Z", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int SetChargingMode(ref CChargingMode var1, int var2);

[DllImport("PowerBattery.dll", EntryPoint = "?SetChargingMode@CChargingMode@PowerBattery@@QEBAHH_N@Z", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
// I don't know what the newly added boolean does, but it doesn't seem to matter
internal static extern int SetChargingModeFallBack(ref CChargingMode var1, int var2, bool var3);

[DllImport("PowerBattery.dll", EntryPoint = "??0CUSBCharger@PowerBattery@@QEAA@XZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern CUSBCharger CUSBCharger(ref CUSBCharger var1);

Expand Down Expand Up @@ -90,7 +93,14 @@ public void SetChargingMode(ChargingMode chargingMode)
{
CChargingMode instance = new();
instance = CChargingMode(ref instance);
_ = SetChargingMode(ref instance, (int)chargingMode);
try
{
_ = SetChargingMode(ref instance, (int)chargingMode);
}
catch (SystemException)
{
_ = SetChargingModeFallBack(ref instance, (int)chargingMode, false);
}
}

public bool IsAlwaysOnUsbEnabled()
Expand Down

0 comments on commit e03fbfd

Please sign in to comment.