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

DevicePowerSetDeviceState

関数
指定デバイスの電源状態を設定する。
DLLPOWRPROF.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// POWRPROF.dll
#include <windows.h>

DWORD DevicePowerSetDeviceState(
    LPCWSTR DeviceDescription,
    DWORD SetFlags,
    void* SetData   // optional
);

パラメーター

名前方向説明
DeviceDescriptionLPCWSTRin変更対象となるデバイスの名前またはハードウェア識別子の文字列。
SetFlagsDWORDin

変更するデバイスのプロパティ。

意味
DEVICEPOWER_SET_WAKEENABLED
0x00000001
指定したデバイスがシステムをスリープ状態から復帰させられるようにします。
DEVICEPOWER_CLEAR_WAKEENABLED
0x00000002
指定したデバイスがシステムをスリープ状態から復帰させられないようにします。
SetDatavoid*inoptional予約済みです。NULL を指定する必要があります。

戻り値の型: DWORD

公式ドキュメント

指定したデバイスの指定したデータを変更します。

戻り値

関数が成功した場合、戻り値は 0 以外の値になります。

関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// POWRPROF.dll
#include <windows.h>

DWORD DevicePowerSetDeviceState(
    LPCWSTR DeviceDescription,
    DWORD SetFlags,
    void* SetData   // optional
);
[DllImport("POWRPROF.dll", SetLastError = true, ExactSpelling = true)]
static extern uint DevicePowerSetDeviceState(
    [MarshalAs(UnmanagedType.LPWStr)] string DeviceDescription,   // LPCWSTR
    uint SetFlags,   // DWORD
    IntPtr SetData   // void* optional
);
<DllImport("POWRPROF.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function DevicePowerSetDeviceState(
    <MarshalAs(UnmanagedType.LPWStr)> DeviceDescription As String,   ' LPCWSTR
    SetFlags As UInteger,   ' DWORD
    SetData As IntPtr   ' void* optional
) As UInteger
End Function
' DeviceDescription : LPCWSTR
' SetFlags : DWORD
' SetData : void* optional
Declare PtrSafe Function DevicePowerSetDeviceState Lib "powrprof" ( _
    ByVal DeviceDescription As LongPtr, _
    ByVal SetFlags As Long, _
    ByVal SetData As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

DevicePowerSetDeviceState = ctypes.windll.powrprof.DevicePowerSetDeviceState
DevicePowerSetDeviceState.restype = wintypes.DWORD
DevicePowerSetDeviceState.argtypes = [
    wintypes.LPCWSTR,  # DeviceDescription : LPCWSTR
    wintypes.DWORD,  # SetFlags : DWORD
    ctypes.POINTER(None),  # SetData : void* optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('POWRPROF.dll')
DevicePowerSetDeviceState = Fiddle::Function.new(
  lib['DevicePowerSetDeviceState'],
  [
    Fiddle::TYPE_VOIDP,  # DeviceDescription : LPCWSTR
    -Fiddle::TYPE_INT,  # SetFlags : DWORD
    Fiddle::TYPE_VOIDP,  # SetData : void* optional
  ],
  -Fiddle::TYPE_INT)
#[link(name = "powrprof")]
extern "system" {
    fn DevicePowerSetDeviceState(
        DeviceDescription: *const u16,  // LPCWSTR
        SetFlags: u32,  // DWORD
        SetData: *mut ()  // void* optional
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("POWRPROF.dll", SetLastError = true)]
public static extern uint DevicePowerSetDeviceState([MarshalAs(UnmanagedType.LPWStr)] string DeviceDescription, uint SetFlags, IntPtr SetData);
"@
$api = Add-Type -MemberDefinition $sig -Name 'POWRPROF_DevicePowerSetDeviceState' -Namespace Win32 -PassThru
# $api::DevicePowerSetDeviceState(DeviceDescription, SetFlags, SetData)
#uselib "POWRPROF.dll"
#func global DevicePowerSetDeviceState "DevicePowerSetDeviceState" sptr, sptr, sptr
; DevicePowerSetDeviceState DeviceDescription, SetFlags, SetData   ; 戻り値は stat
; DeviceDescription : LPCWSTR -> "sptr"
; SetFlags : DWORD -> "sptr"
; SetData : void* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "POWRPROF.dll"
#cfunc global DevicePowerSetDeviceState "DevicePowerSetDeviceState" wstr, int, sptr
; res = DevicePowerSetDeviceState(DeviceDescription, SetFlags, SetData)
; DeviceDescription : LPCWSTR -> "wstr"
; SetFlags : DWORD -> "int"
; SetData : void* optional -> "sptr"
; DWORD DevicePowerSetDeviceState(LPCWSTR DeviceDescription, DWORD SetFlags, void* SetData)
#uselib "POWRPROF.dll"
#cfunc global DevicePowerSetDeviceState "DevicePowerSetDeviceState" wstr, int, intptr
; res = DevicePowerSetDeviceState(DeviceDescription, SetFlags, SetData)
; DeviceDescription : LPCWSTR -> "wstr"
; SetFlags : DWORD -> "int"
; SetData : void* optional -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	powrprof = windows.NewLazySystemDLL("POWRPROF.dll")
	procDevicePowerSetDeviceState = powrprof.NewProc("DevicePowerSetDeviceState")
)

// DeviceDescription (LPCWSTR), SetFlags (DWORD), SetData (void* optional)
r1, _, err := procDevicePowerSetDeviceState.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(DeviceDescription))),
	uintptr(SetFlags),
	uintptr(SetData),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function DevicePowerSetDeviceState(
  DeviceDescription: PWideChar;   // LPCWSTR
  SetFlags: DWORD;   // DWORD
  SetData: Pointer   // void* optional
): DWORD; stdcall;
  external 'POWRPROF.dll' name 'DevicePowerSetDeviceState';
result := DllCall("POWRPROF\DevicePowerSetDeviceState"
    , "WStr", DeviceDescription   ; LPCWSTR
    , "UInt", SetFlags   ; DWORD
    , "Ptr", SetData   ; void* optional
    , "UInt")   ; return: DWORD
●DevicePowerSetDeviceState(DeviceDescription, SetFlags, SetData) = DLL("POWRPROF.dll", "dword DevicePowerSetDeviceState(char*, dword, void*)")
# 呼び出し: DevicePowerSetDeviceState(DeviceDescription, SetFlags, SetData)
# DeviceDescription : LPCWSTR -> "char*"
# SetFlags : DWORD -> "dword"
# SetData : void* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "powrprof" fn DevicePowerSetDeviceState(
    DeviceDescription: [*c]const u16, // LPCWSTR
    SetFlags: u32, // DWORD
    SetData: ?*anyopaque // void* optional
) callconv(std.os.windows.WINAPI) u32;
proc DevicePowerSetDeviceState(
    DeviceDescription: WideCString,  # LPCWSTR
    SetFlags: uint32,  # DWORD
    SetData: pointer  # void* optional
): uint32 {.importc: "DevicePowerSetDeviceState", stdcall, dynlib: "POWRPROF.dll".}
pragma(lib, "powrprof");
extern(Windows)
uint DevicePowerSetDeviceState(
    const(wchar)* DeviceDescription,   // LPCWSTR
    uint SetFlags,   // DWORD
    void* SetData   // void* optional
);
ccall((:DevicePowerSetDeviceState, "POWRPROF.dll"), stdcall, UInt32,
      (Cwstring, UInt32, Ptr{Cvoid}),
      DeviceDescription, SetFlags, SetData)
# DeviceDescription : LPCWSTR -> Cwstring
# SetFlags : DWORD -> UInt32
# SetData : void* optional -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t DevicePowerSetDeviceState(
    const uint16_t* DeviceDescription,
    uint32_t SetFlags,
    void* SetData);
]]
local powrprof = ffi.load("powrprof")
-- powrprof.DevicePowerSetDeviceState(DeviceDescription, SetFlags, SetData)
-- DeviceDescription : LPCWSTR
-- SetFlags : DWORD
-- SetData : void* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('POWRPROF.dll');
const DevicePowerSetDeviceState = lib.func('__stdcall', 'DevicePowerSetDeviceState', 'uint32_t', ['str16', 'uint32_t', 'void *']);
// DevicePowerSetDeviceState(DeviceDescription, SetFlags, SetData)
// DeviceDescription : LPCWSTR -> 'str16'
// SetFlags : DWORD -> 'uint32_t'
// SetData : void* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("POWRPROF.dll", {
  DevicePowerSetDeviceState: { parameters: ["buffer", "u32", "pointer"], result: "u32" },
});
// lib.symbols.DevicePowerSetDeviceState(DeviceDescription, SetFlags, SetData)
// DeviceDescription : LPCWSTR -> "buffer"
// SetFlags : DWORD -> "u32"
// SetData : void* optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t DevicePowerSetDeviceState(
    const uint16_t* DeviceDescription,
    uint32_t SetFlags,
    void* SetData);
C, "POWRPROF.dll");
// $ffi->DevicePowerSetDeviceState(DeviceDescription, SetFlags, SetData);
// DeviceDescription : LPCWSTR
// SetFlags : DWORD
// SetData : void* optional
// 構造体/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 Powrprof extends StdCallLibrary {
    Powrprof INSTANCE = Native.load("powrprof", Powrprof.class);
    int DevicePowerSetDeviceState(
        WString DeviceDescription,   // LPCWSTR
        int SetFlags,   // DWORD
        Pointer SetData   // void* optional
    );
}
@[Link("powrprof")]
lib LibPOWRPROF
  fun DevicePowerSetDeviceState = DevicePowerSetDeviceState(
    DeviceDescription : UInt16*,   # LPCWSTR
    SetFlags : UInt32,   # DWORD
    SetData : Void*   # void* optional
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef DevicePowerSetDeviceStateNative = Uint32 Function(Pointer<Utf16>, Uint32, Pointer<Void>);
typedef DevicePowerSetDeviceStateDart = int Function(Pointer<Utf16>, int, Pointer<Void>);
final DevicePowerSetDeviceState = DynamicLibrary.open('POWRPROF.dll')
    .lookupFunction<DevicePowerSetDeviceStateNative, DevicePowerSetDeviceStateDart>('DevicePowerSetDeviceState');
// DeviceDescription : LPCWSTR -> Pointer<Utf16>
// SetFlags : DWORD -> Uint32
// SetData : void* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function DevicePowerSetDeviceState(
  DeviceDescription: PWideChar;   // LPCWSTR
  SetFlags: DWORD;   // DWORD
  SetData: Pointer   // void* optional
): DWORD; stdcall;
  external 'POWRPROF.dll' name 'DevicePowerSetDeviceState';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "DevicePowerSetDeviceState"
  c_DevicePowerSetDeviceState :: CWString -> Word32 -> Ptr () -> IO Word32
-- DeviceDescription : LPCWSTR -> CWString
-- SetFlags : DWORD -> Word32
-- SetData : void* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let devicepowersetdevicestate =
  foreign "DevicePowerSetDeviceState"
    ((ptr uint16_t) @-> uint32_t @-> (ptr void) @-> returning uint32_t)
(* DeviceDescription : LPCWSTR -> (ptr uint16_t) *)
(* SetFlags : DWORD -> uint32_t *)
(* SetData : void* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library powrprof (t "POWRPROF.dll"))
(cffi:use-foreign-library powrprof)

(cffi:defcfun ("DevicePowerSetDeviceState" device-power-set-device-state :convention :stdcall) :uint32
  (device-description (:string :encoding :utf-16le))   ; LPCWSTR
  (set-flags :uint32)   ; DWORD
  (set-data :pointer))   ; void* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $DevicePowerSetDeviceState = Win32::API::More->new('POWRPROF',
    'DWORD DevicePowerSetDeviceState(LPCWSTR DeviceDescription, DWORD SetFlags, LPVOID SetData)');
# my $ret = $DevicePowerSetDeviceState->Call($DeviceDescription, $SetFlags, $SetData);
# DeviceDescription : LPCWSTR -> LPCWSTR
# SetFlags : DWORD -> DWORD
# SetData : void* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。