Win32 API 日本語リファレンス
ホームSystem.Power › SYSTEM_BATTERY_STATE

SYSTEM_BATTERY_STATE

構造体
サイズx64: 32 バイト / x86: 32 バイト

サイズ=各フィールドのバイト数(x64/x86 で異なる場合は x64/x86 と併記)。x64/x86 列=フィールドのバイトオフセット(HSPで dupptr / lpoke / wpoke 等に使用)。

フィールド

フィールドサイズx64x86説明
AcOnLineBOOLEAN1+0+0AC電源に接続されているかどうかを示すブール値。
BatteryPresentBOOLEAN1+1+1バッテリが装着されているかどうかを示すブール値。
ChargingBOOLEAN1+2+2バッテリが充電中かどうかを示すブール値。
DischargingBOOLEAN1+3+3バッテリが放電中かどうかを示すブール値。
Spare1BOOLEAN3+4+4将来の拡張用に予約された予備ブール配列。
TagBYTE1+7+7バッテリを識別するタグ値。
MaxCapacityDWORD4+8+8バッテリの最大容量。mWh単位で表す。
RemainingCapacityDWORD4+12+12バッテリの残容量。mWh単位で表す。
RateDWORD4+16+16充放電速度。mW単位の符号付き値で、放電時は負となる。
EstimatedTimeDWORD4+20+20残り使用可能時間の推定値。秒単位で表す。
DefaultAlert1DWORD4+24+24低残量アラート1のしきい値。mWh単位で表す。
DefaultAlert2DWORD4+28+28低残量アラート2(警告)のしきい値。mWh単位で表す。

各言語での定義

#include <windows.h>

// SYSTEM_BATTERY_STATE  (x64 32 / x86 32 バイト)
typedef struct SYSTEM_BATTERY_STATE {
    BOOLEAN AcOnLine;
    BOOLEAN BatteryPresent;
    BOOLEAN Charging;
    BOOLEAN Discharging;
    BOOLEAN Spare1[3];
    BYTE Tag;
    DWORD MaxCapacity;
    DWORD RemainingCapacity;
    DWORD Rate;
    DWORD EstimatedTime;
    DWORD DefaultAlert1;
    DWORD DefaultAlert2;
} SYSTEM_BATTERY_STATE;
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SYSTEM_BATTERY_STATE
{
    [MarshalAs(UnmanagedType.U1)] public bool AcOnLine;
    [MarshalAs(UnmanagedType.U1)] public bool BatteryPresent;
    [MarshalAs(UnmanagedType.U1)] public bool Charging;
    [MarshalAs(UnmanagedType.U1)] public bool Discharging;
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 3)] public bool[] Spare1;
    public byte Tag;
    public uint MaxCapacity;
    public uint RemainingCapacity;
    public uint Rate;
    public uint EstimatedTime;
    public uint DefaultAlert1;
    public uint DefaultAlert2;
}
Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public Structure SYSTEM_BATTERY_STATE
    <MarshalAs(UnmanagedType.U1)> Public AcOnLine As Boolean
    <MarshalAs(UnmanagedType.U1)> Public BatteryPresent As Boolean
    <MarshalAs(UnmanagedType.U1)> Public Charging As Boolean
    <MarshalAs(UnmanagedType.U1)> Public Discharging As Boolean
    <MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.U1, SizeConst:=3)> Public Spare1() As Boolean
    Public Tag As Byte
    Public MaxCapacity As UInteger
    Public RemainingCapacity As UInteger
    Public Rate As UInteger
    Public EstimatedTime As UInteger
    Public DefaultAlert1 As UInteger
    Public DefaultAlert2 As UInteger
End Structure
import ctypes
from ctypes import wintypes

class SYSTEM_BATTERY_STATE(ctypes.Structure):
    _fields_ = [
        ("AcOnLine", ctypes.c_byte),
        ("BatteryPresent", ctypes.c_byte),
        ("Charging", ctypes.c_byte),
        ("Discharging", ctypes.c_byte),
        ("Spare1", ctypes.c_byte * 3),
        ("Tag", ctypes.c_ubyte),
        ("MaxCapacity", wintypes.DWORD),
        ("RemainingCapacity", wintypes.DWORD),
        ("Rate", wintypes.DWORD),
        ("EstimatedTime", wintypes.DWORD),
        ("DefaultAlert1", wintypes.DWORD),
        ("DefaultAlert2", wintypes.DWORD),
    ]
#[repr(C)]
pub struct SYSTEM_BATTERY_STATE {
    pub AcOnLine: u8,
    pub BatteryPresent: u8,
    pub Charging: u8,
    pub Discharging: u8,
    pub Spare1: [u8; 3],
    pub Tag: u8,
    pub MaxCapacity: u32,
    pub RemainingCapacity: u32,
    pub Rate: u32,
    pub EstimatedTime: u32,
    pub DefaultAlert1: u32,
    pub DefaultAlert2: u32,
}
import "golang.org/x/sys/windows"

type SYSTEM_BATTERY_STATE struct {
	AcOnLine byte
	BatteryPresent byte
	Charging byte
	Discharging byte
	Spare1 [3]byte
	Tag byte
	MaxCapacity uint32
	RemainingCapacity uint32
	Rate uint32
	EstimatedTime uint32
	DefaultAlert1 uint32
	DefaultAlert2 uint32
}
type
  SYSTEM_BATTERY_STATE = record
    AcOnLine: ByteBool;
    BatteryPresent: ByteBool;
    Charging: ByteBool;
    Discharging: ByteBool;
    Spare1: array[0..2] of ByteBool;
    Tag: Byte;
    MaxCapacity: DWORD;
    RemainingCapacity: DWORD;
    Rate: DWORD;
    EstimatedTime: DWORD;
    DefaultAlert1: DWORD;
    DefaultAlert2: DWORD;
  end;
const SYSTEM_BATTERY_STATE = extern struct {
    AcOnLine: u8,
    BatteryPresent: u8,
    Charging: u8,
    Discharging: u8,
    Spare1: [3]u8,
    Tag: u8,
    MaxCapacity: u32,
    RemainingCapacity: u32,
    Rate: u32,
    EstimatedTime: u32,
    DefaultAlert1: u32,
    DefaultAlert2: u32,
};
type
  SYSTEM_BATTERY_STATE {.bycopy.} = object
    AcOnLine: uint8
    BatteryPresent: uint8
    Charging: uint8
    Discharging: uint8
    Spare1: array[3, uint8]
    Tag: uint8
    MaxCapacity: uint32
    RemainingCapacity: uint32
    Rate: uint32
    EstimatedTime: uint32
    DefaultAlert1: uint32
    DefaultAlert2: uint32
struct SYSTEM_BATTERY_STATE
{
    ubyte AcOnLine;
    ubyte BatteryPresent;
    ubyte Charging;
    ubyte Discharging;
    ubyte[3] Spare1;
    ubyte Tag;
    uint MaxCapacity;
    uint RemainingCapacity;
    uint Rate;
    uint EstimatedTime;
    uint DefaultAlert1;
    uint DefaultAlert2;
}

HSP用 定義

HSP3.7/3.8 は構造体機能が無いため4byte整数配列(dim)+peek/poke で操作(32/64bitでサイズ・位置が異なる場合はタブで分割)。IronHSP は NSTRUCT(#defstruct/stdim/->)で32/64bit共通。

; HSP3.7/3.8 は構造体機能が無いため、4byte整数の配列変数で操作します。(x64 レイアウト)
; SYSTEM_BATTERY_STATE サイズ: 32 バイト(x64)
dim st, 8    ; 4byte整数×8(構造体サイズ 32 / 4 切り上げ)
; AcOnLine : BOOLEAN (+0, 1byte)  poke st,0,値  /  値 = peek(st,0)
; BatteryPresent : BOOLEAN (+1, 1byte)  poke st,1,値  /  値 = peek(st,1)
; Charging : BOOLEAN (+2, 1byte)  poke st,2,値  /  値 = peek(st,2)
; Discharging : BOOLEAN (+3, 1byte)  poke st,3,値  /  値 = peek(st,3)
; Spare1 : BOOLEAN (+4, 3byte)  varptr(st)+4 を基点に操作(3byte:入れ子/配列)
; Tag : BYTE (+7, 1byte)  poke st,7,値  /  値 = peek(st,7)
; MaxCapacity : DWORD (+8, 4byte)  st.2 = 値  /  値 = st.2   (lpoke/lpeek も可)
; RemainingCapacity : DWORD (+12, 4byte)  st.3 = 値  /  値 = st.3   (lpoke/lpeek も可)
; Rate : DWORD (+16, 4byte)  st.4 = 値  /  値 = st.4   (lpoke/lpeek も可)
; EstimatedTime : DWORD (+20, 4byte)  st.5 = 値  /  値 = st.5   (lpoke/lpeek も可)
; DefaultAlert1 : DWORD (+24, 4byte)  st.6 = 値  /  値 = st.6   (lpoke/lpeek も可)
; DefaultAlert2 : DWORD (+28, 4byte)  st.7 = 値  /  値 = st.7   (lpoke/lpeek も可)
; ※4byte境界の整数は添字 st.N(N=オフセット/4)で読み書き可。それ以外は peek/poke 系を使用。
; IronHSP は NSTRUCT(構造体)をサポート。32bit/64bit どちらでも同じコードで動作します。
#defstruct global SYSTEM_BATTERY_STATE
    #field bool1 AcOnLine
    #field bool1 BatteryPresent
    #field bool1 Charging
    #field bool1 Discharging
    #field bool1 Spare1 3
    #field byte Tag
    #field int MaxCapacity
    #field int RemainingCapacity
    #field int Rate
    #field int EstimatedTime
    #field int DefaultAlert1
    #field int DefaultAlert2
#endstruct

stdim st, SYSTEM_BATTERY_STATE        ; NSTRUCT 変数を確保
st->AcOnLine = 100
mes "AcOnLine=" + st->AcOnLine