问题
For testing purposes I need to force Windows to think that it has battery on workstation PC.
Is there the way to fake/mock/emulate/simulate battery state/low level/charging/discharging in Windows workstation without battery device?
Can I set battery level programmatically on Windows?
回答1:
tl;dr: Use Windows Driver Testing Framework (WDTF) Runtime Libraries
from Windows Driver Kit (WDK)
Long instruction:
Install Visual Studio with Windows Driver Kit (WDK)
Create virtual machine
Seach for
WDK Test Target Setup x64-x64_en-us.msi
inC:\Program Files (x86)\Windows Kits
Install
WDK Test Target Setup x64-x64_en-us.msi
on virtual machine.In Visual Studio, in main menu Driver > Test > Configure Devices add virtual machine to install (WDTF) Runtime Libraries.
From virtual machine copy folder
C:\Program Files (x86)\Windows Kits\10\Testing\Runtimes\WDTF\RunTime
on your workstationFrom
RunTime
folder run as administratorRegisterWDTF.exe
Output:
C:\RunTime>RegisterWDTF.exe
-Delete registry state.
-Get registry path.
-Register COM DLLs.
Process "C:\RunTime\Actions\Deprecated\DeviceManagement.dll".
Process "C:\RunTime\Actions\DeviceSupport\WDTFDeviceSupportAction.dll".
Process "C:\RunTime\Actions\DrvPkgLibExt.dll".
Not a COM DLL
Process "C:\RunTime\Actions\EDT\WDTFEDTAction.dll".
Process "C:\RunTime\Actions\IoSpy\WDTFIoAttackAction.dll".
Process "C:\RunTime\Actions\IoSpy\WDTFIoSpyAction.dll".
Process "C:\RunTime\Actions\SimpleIO\SimpleIO_D3DTest.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFAnySimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFAudioSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFBluetoothSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFCDROMSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFMobileBroadbandSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFMUTT2STRESSSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFMUTT2TCDSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFNetworkSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFVolumeSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFWebcamSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFWirelessSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFWPDSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimulatedBattery\WDTFSimulatedBatterySystemAction.dll".
Process "C:\RunTime\Actions\System\WDTFSystemAction.dll".
Process "C:\RunTime\Actions\WDTFConcurrentIOAction.dll".
Process "C:\RunTime\Actions\WDTFCpuUtilizationSystemAction.dll".
Process "C:\RunTime\Actions\WDTFDriverPackageAction.dll".
Process "C:\RunTime\Actions\WDTFDriverSetupDeviceAction.dll".
Process "C:\RunTime\Actions\WDTFDriverSetupSystemAction.dll".
Process "C:\RunTime\Actions\WDTFDriverVerifierSystemAction.dll".
Process "C:\RunTime\Actions\WDTFFuzzTestAction.dll".
Process "C:\RunTime\Actions\WDTFInterfaces\WDTFInterfaces.dll".
Process "C:\RunTime\Actions\WDTFPNPAction.dll".
Process "C:\RunTime\Actions\WDTFSUPAction.dll".
Process "C:\WDTF.DLL".
-Update registry state.
-Install Drivers.
-Done
WDTF Registration Successful
Now you can use COM object "WDTF2.WDTF" in your program language like in this VBScript example:
'
' Variable defenition
'
Dim WDTF
Dim SimulatedBatterySystemSystemAction
'
' Create WDTF object
'
Set WDTF = CreateObject("WDTF2.WDTF")
'
' Get your newly created SimulatedBatterySystem action interface
'
Set SimulatedBatterySystemSystemAction = WDTF.SystemDepot.ThisSystem.GetInterface("SimulatedBatterySystem")
' enable the simulated battery
SimulatedBatterySystemSystemAction.EnableSimulatedBattery()
' disable real batteries
SimulatedBatterySystemSystemAction.DisableRealBatteries()
' set to DC power status
SimulatedBatterySystemSystemAction.SetSimulatedBatteryToDC()
' set to 50% charge level
SimulatedBatterySystemSystemAction.SetSimulatedBatteryChargePercentage(50)
WScript.Sleep 10000
' set to AC power status
SimulatedBatterySystemSystemAction.SetSimulatedBatteryToAC()
' enable real batteries
SimulatedBatterySystemSystemAction.EnableRealBatteries()
' disable the simulated battery
SimulatedBatterySystemSystemAction.DisableSimulatedBattery()
Here all WDTF reference: https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/_dtf/
Just in case, Linux Fake battery module: https://github.com/hoelzro/linux-fake-battery-module
回答2:
Oleg's answer was fairly straightforward to follow but I was unable to make it simulate a battery - the actions to disable and enable the real battery did succeed.
PS C:\RunTime> .\RegisterWDTF.exe
This version of WDTF is compatible with Windows Build 17763.
-Delete registry state.
-Get registry path.
-Register COM DLLs.
Process "C:\RunTime\Actions\Deprecated\DeviceManagement.dll".
Process "C:\RunTime\Actions\DeviceSupport\WDTFDeviceSupportAction.dll".
Process "C:\RunTime\Actions\DrvPkgLibExt.dll".
Not a COM DLL
Process "C:\RunTime\Actions\EDT\WDTFEDTAction.dll".
Process "C:\RunTime\Actions\IoSpy\WDTFIoAttackAction.dll".
Process "C:\RunTime\Actions\IoSpy\WDTFIoSpyAction.dll".
Process "C:\RunTime\Actions\SimpleIO\SimpleIO_D3DTest.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFAnySimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFAudioSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFBluetoothSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFCDROMSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFMobileBroadbandSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFMUTT2STRESSSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFMUTT2TCDSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFNetworkSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\wdtfsensorsiosimpleioaction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFVolumeSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFWebcamSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFWirelessSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimpleIO\WDTFWPDSimpleIoAction.dll".
Process "C:\RunTime\Actions\SimulatedBattery\WDTFSimulatedBatterySystemAction.dll".
Process "C:\RunTime\Actions\System\WDTFSystemAction.dll".
Process "C:\RunTime\Actions\WDTFConcurrentIOAction.dll".
Process "C:\RunTime\Actions\WDTFCpuUtilizationSystemAction.dll".
Process "C:\RunTime\Actions\WDTFDriverPackageAction.dll".
Process "C:\RunTime\Actions\WDTFDriverSetupDeviceAction.dll".
Process "C:\RunTime\Actions\WDTFDriverSetupSystemAction.dll".
Process "C:\RunTime\Actions\WDTFDriverVerifierSystemAction.dll".
Process "C:\RunTime\Actions\WDTFFuzzTestAction.dll".
Process "C:\RunTime\Actions\WDTFInterfaces\WDTFInterfaces.dll".
Process "C:\RunTime\Actions\WDTFPNPAction.dll".
Process "C:\RunTime\Actions\WDTFSUPAction.dll".
Process "C:\RunTime\WDTF.DLL".
-Update registry state.
-Install Drivers.
-Done
WDTF Registration Successful
PS C:\RunTime> cscript.exe .\fake-battery.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
Loading Wex.Logger.dll from systemdata.cpp linep 922 failed.
WDTF_TARGET : INFO : - GetInterface("SimulatedBatterySystem")
WDTF_TARGET : INFO : Target: ORANGE
WDTF_SIMBATT : INFO : DisableRealBatteries()
WDTF_SIMBATT : INFO : EnableSimulatedBattery()
WDTF_SIMBATT : ERROR : SimulatedBatterySystem critical error installing battbus device. HRESULT=0xE0000203
C:\RunTime\fake-battery.vbs(22, 1) WDTF.SimulatedBatterySystem.1: SimulatedBatterySystem critical error installing battbus device. HRESULT=0xE0000203
Unfortunately there is no information that I could find on the public Internet about these errors. Hopefully someone out there comes across this and knows what to do next!
--
edit: it appears as though Visual Studio installs various drivers in addition to the testing framework so simply copying RunTime folder is insufficient. I ended up adding my workstation as a full target using VS from another workstation. This then let me simulate the battery.
来源:https://stackoverflow.com/questions/50658822/how-to-programmatically-emulate-battery-in-windows-workstation-without-battery-d