ホーム › System.Power › GetDevicePowerState
GetDevicePowerState
関数指定デバイスが現在オン状態かどうかを取得する。
シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL GetDevicePowerState(
HANDLE hDevice,
BOOL* pfOn
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hDevice | HANDLE | in | デバイス上のオブジェクト(ファイルやソケットなど)へのハンドル、またはデバイス自体へのハンドル。 |
| pfOn | BOOL* | out | 電源状態を受け取る変数へのポインター。デバイスが動作状態の場合、この値は TRUE になります。それ以外の場合は FALSE です。 |
戻り値の型: BOOL
公式ドキュメント
指定したデバイスの現在の電源状態を取得します。
戻り値
関数が成功した場合、戻り値は 0 以外の値です。
関数が失敗した場合、戻り値は 0 です。
解説(Remarks)
アプリケーションは GetDevicePowerState を使用して、デバイスが動作状態にあるか低電力状態にあるかを判別できます。デバイスが低電力状態にある場合、そのデバイスにアクセスすると、I/O 要求がキューに入れられる、または失敗する、あるいはデバイスが動作状態へ遷移することがあります。正確な動作はデバイスの実装に依存します。
ノートパソコンでバッテリー寿命を最大化するには、 GetDevicePowerState を使用して消費電力を抑えてください。たとえば、ディスクが現在パワーダウンしている場合、そのディスクにアクセスするとスピンアップが発生し、消費電力が増加してバッテリー寿命が短くなります。
システムがバッテリー電源で動作している間は、可能な限りデバイスへのアクセスを遅延または制限する必要があります。システムがバッテリー電源で動作しているかどうか、およびバッテリーの残量を判別するには、 GetSystemPowerStatus 関数を使用してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
BOOL GetDevicePowerState(
HANDLE hDevice,
BOOL* pfOn
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern bool GetDevicePowerState(
IntPtr hDevice, // HANDLE
out bool pfOn // BOOL* out
);<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Function GetDevicePowerState(
hDevice As IntPtr, ' HANDLE
<Out> ByRef pfOn As Boolean ' BOOL* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hDevice : HANDLE
' pfOn : BOOL* out
Declare PtrSafe Function GetDevicePowerState Lib "kernel32" ( _
ByVal hDevice As LongPtr, _
ByRef pfOn As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
GetDevicePowerState = ctypes.windll.kernel32.GetDevicePowerState
GetDevicePowerState.restype = wintypes.BOOL
GetDevicePowerState.argtypes = [
wintypes.HANDLE, # hDevice : HANDLE
ctypes.c_void_p, # pfOn : BOOL* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
GetDevicePowerState = Fiddle::Function.new(
lib['GetDevicePowerState'],
[
Fiddle::TYPE_VOIDP, # hDevice : HANDLE
Fiddle::TYPE_VOIDP, # pfOn : BOOL* out
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn GetDevicePowerState(
hDevice: *mut core::ffi::c_void, // HANDLE
pfOn: *mut i32 // BOOL* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll")]
public static extern bool GetDevicePowerState(IntPtr hDevice, out bool pfOn);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetDevicePowerState' -Namespace Win32 -PassThru
# $api::GetDevicePowerState(hDevice, pfOn)#uselib "KERNEL32.dll"
#func global GetDevicePowerState "GetDevicePowerState" sptr, sptr
; GetDevicePowerState hDevice, varptr(pfOn) ; 戻り値は stat
; hDevice : HANDLE -> "sptr"
; pfOn : BOOL* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global GetDevicePowerState "GetDevicePowerState" sptr, var ; res = GetDevicePowerState(hDevice, pfOn) ; hDevice : HANDLE -> "sptr" ; pfOn : BOOL* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global GetDevicePowerState "GetDevicePowerState" sptr, sptr ; res = GetDevicePowerState(hDevice, varptr(pfOn)) ; hDevice : HANDLE -> "sptr" ; pfOn : BOOL* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL GetDevicePowerState(HANDLE hDevice, BOOL* pfOn) #uselib "KERNEL32.dll" #cfunc global GetDevicePowerState "GetDevicePowerState" intptr, var ; res = GetDevicePowerState(hDevice, pfOn) ; hDevice : HANDLE -> "intptr" ; pfOn : BOOL* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL GetDevicePowerState(HANDLE hDevice, BOOL* pfOn) #uselib "KERNEL32.dll" #cfunc global GetDevicePowerState "GetDevicePowerState" intptr, intptr ; res = GetDevicePowerState(hDevice, varptr(pfOn)) ; hDevice : HANDLE -> "intptr" ; pfOn : BOOL* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procGetDevicePowerState = kernel32.NewProc("GetDevicePowerState")
)
// hDevice (HANDLE), pfOn (BOOL* out)
r1, _, err := procGetDevicePowerState.Call(
uintptr(hDevice),
uintptr(pfOn),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction GetDevicePowerState(
hDevice: THandle; // HANDLE
pfOn: Pointer // BOOL* out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'GetDevicePowerState';result := DllCall("KERNEL32\GetDevicePowerState"
, "Ptr", hDevice ; HANDLE
, "Ptr", pfOn ; BOOL* out
, "Int") ; return: BOOL●GetDevicePowerState(hDevice, pfOn) = DLL("KERNEL32.dll", "bool GetDevicePowerState(void*, void*)")
# 呼び出し: GetDevicePowerState(hDevice, pfOn)
# hDevice : HANDLE -> "void*"
# pfOn : BOOL* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn GetDevicePowerState(
hDevice: ?*anyopaque, // HANDLE
pfOn: [*c]i32 // BOOL* out
) callconv(std.os.windows.WINAPI) i32;proc GetDevicePowerState(
hDevice: pointer, # HANDLE
pfOn: ptr int32 # BOOL* out
): int32 {.importc: "GetDevicePowerState", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int GetDevicePowerState(
void* hDevice, // HANDLE
int* pfOn // BOOL* out
);ccall((:GetDevicePowerState, "KERNEL32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{Int32}),
hDevice, pfOn)
# hDevice : HANDLE -> Ptr{Cvoid}
# pfOn : BOOL* out -> Ptr{Int32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t GetDevicePowerState(
void* hDevice,
int32_t* pfOn);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetDevicePowerState(hDevice, pfOn)
-- hDevice : HANDLE
-- pfOn : BOOL* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetDevicePowerState = lib.func('__stdcall', 'GetDevicePowerState', 'int32_t', ['void *', 'int32_t *']);
// GetDevicePowerState(hDevice, pfOn)
// hDevice : HANDLE -> 'void *'
// pfOn : BOOL* out -> 'int32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
GetDevicePowerState: { parameters: ["pointer", "pointer"], result: "i32" },
});
// lib.symbols.GetDevicePowerState(hDevice, pfOn)
// hDevice : HANDLE -> "pointer"
// pfOn : BOOL* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t GetDevicePowerState(
void* hDevice,
int32_t* pfOn);
C, "KERNEL32.dll");
// $ffi->GetDevicePowerState(hDevice, pfOn);
// hDevice : HANDLE
// pfOn : BOOL* out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
boolean GetDevicePowerState(
Pointer hDevice, // HANDLE
IntByReference pfOn // BOOL* out
);
}@[Link("kernel32")]
lib LibKERNEL32
fun GetDevicePowerState = GetDevicePowerState(
hDevice : Void*, # HANDLE
pfOn : Int32* # BOOL* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef GetDevicePowerStateNative = Int32 Function(Pointer<Void>, Pointer<Int32>);
typedef GetDevicePowerStateDart = int Function(Pointer<Void>, Pointer<Int32>);
final GetDevicePowerState = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<GetDevicePowerStateNative, GetDevicePowerStateDart>('GetDevicePowerState');
// hDevice : HANDLE -> Pointer<Void>
// pfOn : BOOL* out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetDevicePowerState(
hDevice: THandle; // HANDLE
pfOn: Pointer // BOOL* out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'GetDevicePowerState';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetDevicePowerState"
c_GetDevicePowerState :: Ptr () -> Ptr CInt -> IO CInt
-- hDevice : HANDLE -> Ptr ()
-- pfOn : BOOL* out -> Ptr CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let getdevicepowerstate =
foreign "GetDevicePowerState"
((ptr void) @-> (ptr int32_t) @-> returning int32_t)
(* hDevice : HANDLE -> (ptr void) *)
(* pfOn : BOOL* out -> (ptr int32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("GetDevicePowerState" get-device-power-state :convention :stdcall) :int32
(h-device :pointer) ; HANDLE
(pf-on :pointer)) ; BOOL* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GetDevicePowerState = Win32::API::More->new('KERNEL32',
'BOOL GetDevicePowerState(HANDLE hDevice, LPVOID pfOn)');
# my $ret = $GetDevicePowerState->Call($hDevice, $pfOn);
# hDevice : HANDLE -> HANDLE
# pfOn : BOOL* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f GetSystemPowerStatus — AC電源やバッテリーなどシステムの電源状態を取得する。