前提、実現したいこと
Pythonのctypesライブラリを使って、Win32APIを操作し、アクティブな電源オプションのGUIDを取得したいです。
試したこと
以下のように取得しようと試みましたが、
print(poweroption.Data1)では0が出力され、
print(poweroption_p.contents.Data1)では、エラーが発生しました。
Python
1import ctypes 2 3class GUID(ctypes.Structure): 4 _fields_ = [("Data1", ctypes.c_ulong), 5 ("Data2", ctypes.c_ushort), 6 ("Data3", ctypes.c_ushort), 7 ("Data4", ctypes.c_ubyte * 8)] 8 9# PowrProf.dllをロード 10powersetting = ctypes.WinDLL("PowrProf") 11 12# # GUID構造体を作成 13poweroption = GUID() 14 15# GUID構造体のポインタを作成 16poweroption_p = ctypes.POINTER(GUID) 17 18# PowerGetActiveScheme関数を呼び出す 19powersetting.PowerGetActiveScheme(None, ctypes.pointer(poweroption_p(poweroption))) 20 21print(poweroption.Data1) 22# 0 23 24print(poweroption_p.contents.Data1) 25# エラー (AttributeError: 'getset_descriptor' object has no attribute 'Data1') 26
補足情報(FW/ツールのバージョンなど)
Windows 10
Python 3.11
参考にしたサイト
PowerGetActiveScheme 関数 (powersetting.h) - Win32 apps
https://learn.microsoft.com/ja-jp/windows/win32/api/powersetting/nf-powersetting-powergetactivescheme
GUID 構造体 (guiddef.h) - Win32 - Microsoft Learn
https://learn.microsoft.com/ja-jp/windows/win32/api/guiddef/ns-guiddef-guid
[C++/Windows] 現在の電源プランのGUIDを取得する - Qiita
https://qiita.com/tera1707/items/0d375158cd3d16992ce2
Python 公式ドキュメント (ctypes --- Pythonのための外部関数ライブラリ)
https://docs.python.org/ja/3/library/ctypes.html
追記
PowerGetActiveScheme関数の返り値は0です。
ご教示願います。
また、以下のようにして電源プランの変更は行えるようです。
python
1from ctypes import * 2 3class GUID(Structure): 4 _fields_ = [("Data1", c_ulong), 5 ("Data2", c_ushort), 6 ("Data3", c_ushort), 7 ("Data4", c_ubyte * 8)] 8 9 10# PowrProf.dllをロード 11powersetting = WinDLL("PowrProf") 12 13# GUID構造体を作成 14array = c_ubyte*8 15high_performance = GUID(c_ulong(0x8c5e7fda), 16 0xe8bf, 17 0x4a96, 18 array(0x9a,0x85,0xa6,0xe2,0x3a,0x8c,0x63,0x5c)) 19 20power_save = GUID(c_ulong(0xa1841308), 21 0x3541, 22 0x4fab, 23 array(0xbc,0x81,0xf7,0x15,0x56,0xf2,0x0b,0x4a)) 24 25balance = GUID(c_ulong(0x381b4222), 26 0xf694, 27 0x41f0, 28 array(0x96,0x85,0xff,0x5b,0xb2,0x60,0xdf,0x2e)) 29 30# 電源プランを高パフォーマンスに設定 31powersetting.PowerSetActiveScheme(None,byref(high_performance))
回答1件