取WM5.0电池电量Windows API 问题
各路高手,
我现在遇到些问题:在取电池电量的时候用到Windows API了,但是还是总是有些问题。
public class SYSTEM_POWER_STATUS_EX2
{
// Fields
public byte ACLineStatus;
public byte BackupBatteryFlag;
public uint BackupBatteryFullLifeTime;
public byte BackupBatteryLifePercent;
public uint BackupBatteryLifeTime;
public uint BackupBatteryVoltage;
public uint BatteryAverageCurrent;
public uint BatteryAverageInterval;
public byte BatteryChemistry;
public uint BatteryCurrent;
public byte BatteryFlag;
public uint BatteryFullLifeTime;
public byte BatteryLifePercent;
public uint BatteryLifeTime;
public uint BatterymAHourConsumed;
public uint BatteryTemperature;
public uint BatteryVoltage;
public byte Reserved1;
public byte Reserved2;
public byte Reserved3;
// Methods
public SYSTEM_POWER_STATUS_EX2()
{
}
}
[DllImport( "coredll ")]
private static extern uint GetSystemPowerStatusEx2(SYSTEM_POWER_STATUS_EX2 lpSystemPowerStatus, uint dwLen, bool fUpdate);
SYSTEM_POWER_STATUS_EX2 status2 = new SYSTEM_POWER_STATUS_EX2();
通过下列方法以后还是取不到电量。status2一直是null.
GetSystemPowerStatusEx2(this.status2, (uint) Marshal.SizeOf(this.status2), false)
各路高手,看看这到底是什么问题?
[解决办法]
弄那么复杂干什么呢?
Header: Winbase.h.
Link Library: Coredll.lib.
SYSTEM_POWER_STATUS_EX2 state;
GetSystemPowerStatusEx2(&state, sizeof(state), TRUE);
state.ACLineStatus
state.BatteryFlag
.........................
就可以呀。
[解决办法]
呵呵
c#可以调用coredll.dll里的BatteryGetLifeTimeInfo函数。
另外c#虽然不支持native code,但使用com之后就可以把c#运用如飞了。
所以,不懂vc的c#程序员很可怜。建议你花点时间学学吧。值