Skip to content

Commit 02ed9ec

Browse files
committed
fix: replace wmic with Windows API to prevent antivirus false positives
Replace wmic-based battery detection with direct kernel32.dll API calls to avoid Trojan:Win32/Wacatac false positive detections by Windows Defender. Changes: - Use GetSystemPowerStatus() P/Invoke instead of spawning wmic process - Add SYSTEM_POWER_STATUS struct for native Windows API - Maintain same functionality with zero behavioral changes - macOS and Linux implementations unchanged This resolves antivirus detection issues while preserving power management functionality across all platforms.
1 parent cb12e04 commit 02ed9ec

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

POWER_OPTIMIZATIONS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ Implemented comprehensive power-saving optimizations to drastically reduce batte
2323
- Older: 30 minutes
2424

2525
### 3. Power Management System (`Models/PowerManagement.cs`)
26-
- **Automatic battery detection**: Works on Windows, macOS, and Linux
26+
- **Automatic battery detection**: Uses native Windows API (kernel32.dll) on Windows, system utilities on macOS/Linux
27+
- **Antivirus-safe implementation**: Direct Windows API calls instead of wmic to avoid false positives
2728
- **Three power modes**:
2829
- **Power Saver** (on battery): Minimal refresh rates, no parallel operations
2930
- **Balanced** (default on AC): Moderate refresh rates, limited parallelism

src/Models/PowerManagement.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,34 @@ private static bool CheckWindowsBattery()
108108
{
109109
try
110110
{
111-
var psi = new System.Diagnostics.ProcessStartInfo
112-
{
113-
FileName = "wmic",
114-
Arguments = "path Win32_Battery get BatteryStatus /value",
115-
RedirectStandardOutput = true,
116-
UseShellExecute = false,
117-
CreateNoWindow = true
118-
};
119-
120-
using var process = System.Diagnostics.Process.Start(psi);
121-
if (process != null)
111+
// Use Windows API via P/Invoke instead of wmic to avoid antivirus false positives
112+
var status = GetSystemPowerStatus(out SYSTEM_POWER_STATUS sps);
113+
if (status)
122114
{
123-
var output = process.StandardOutput.ReadToEnd();
124-
process.WaitForExit();
125-
126-
// BatteryStatus=2 means AC powered, 1 means battery
127-
return output.Contains("BatteryStatus=1");
115+
// ACLineStatus: 0 = Offline (on battery), 1 = Online (AC power)
116+
return sps.ACLineStatus == 0;
128117
}
129118
}
130119
catch { }
131120

132121
return false;
133122
}
134123

124+
// Windows API structures and imports for battery status
125+
[StructLayout(LayoutKind.Sequential)]
126+
private struct SYSTEM_POWER_STATUS
127+
{
128+
public byte ACLineStatus;
129+
public byte BatteryFlag;
130+
public byte BatteryLifePercent;
131+
public byte SystemStatusFlag;
132+
public int BatteryLifeTime;
133+
public int BatteryFullLifeTime;
134+
}
135+
136+
[DllImport("kernel32.dll", SetLastError = true)]
137+
private static extern bool GetSystemPowerStatus(out SYSTEM_POWER_STATUS lpSystemPowerStatus);
138+
135139
private static bool CheckMacOSBattery()
136140
{
137141
try

0 commit comments

Comments
 (0)