TNB Library
TnbWindows.h
[詳解]
1#pragma once
11#include "TnbThread.h"
12
13
14
15//TNB Library
16namespace TNB
17{
18
19
20
32{
34 CCtrlAltDel(void) {}
35
37 virtual void Run(LPARAM lParam)
38 {
39 HDESK old_desktop = ::GetThreadDesktop(::GetCurrentThreadId());
40 DWORD dwDesiredAccess = DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW
41 | DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL
42 | DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS
43 | DESKTOP_SWITCHDESKTOP | GENERIC_WRITE;
44 HDESK h = ::OpenDesktop(_T("Winlogon"), 0, FALSE, dwDesiredAccess);
45 if ( ::SetThreadDesktop(h) )
46 {
47 ::PostMessage(HWND_BROADCAST, WM_HOTKEY, 0, MAKELONG(MOD_ALT | MOD_CONTROL, VK_DELETE));
48 }
49 ::SetThreadDesktop(old_desktop);
50 }
51public:
55 static void Exec(void)
56 {
58 }
59};
60
61
62
107{
109 CPowerManager(void)
110 {
111 }
112
114 static bool ms_AddAuthority(LPCTSTR lpName = SE_SHUTDOWN_NAME)
115 {
116 // アクセストークンを開く
117 HANDLE hHandle;
118 if ( ! ::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hHandle) )
119 {
120 return false;
121 }
122 TOKEN_PRIVILEGES token;
123 token.PrivilegeCount = 1; // 数は1つ
124 if ( ! ::LookupPrivilegeValue(NULL, lpName, &(token.Privileges[0].Luid)) )
125 {
126 ::CloseHandle(hHandle);
127 return false;
128 }
129 token.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // 有効
130 if ( ! ::AdjustTokenPrivileges(hHandle, FALSE, &token, 0, NULL, NULL) )
131 {
132 ::CloseHandle(hHandle);
133 return false;
134 }
135 // アクセストークンを閉じる
136 return !! ::CloseHandle(hHandle);
137 }
138
140 static bool ms_AddShutdownAuthority(LPCTSTR lpMachineName)
141 {
142 LPCTSTR P = SE_REMOTE_SHUTDOWN_NAME;
143 if ( lpMachineName == NULL || *lpMachineName == 0 )
144 {
145 P = SE_SHUTDOWN_NAME;
146 }
147 return ms_AddAuthority(P);
148 }
149
150public:
151
166 static bool Shutdown95(UINT uFlags = EWX_POWEROFF)
167 {
168 if ( ! ms_AddAuthority() )
169 {
170 return false;
171 }
172 return !! ::ExitWindowsEx(uFlags, 0);
173 }
174
192 static bool ShutdownNt(LPCTSTR lpMachineName, LPCTSTR lpMessage, DWORD dwTimeout,
193 bool bForceAppsClosed = false, bool bRebootAfterShutdown = true)
194 {
195 if ( ! ms_AddShutdownAuthority(lpMachineName) )
196 {
197 return false;
198 }
199 BOOL r = ::InitiateSystemShutdown(
200 CSimpleStr(lpMachineName), CSimpleStr(lpMessage), dwTimeout, bForceAppsClosed, bRebootAfterShutdown);
201 return !! r;
202 }
203
217 static bool Shutdown(bool bForceAppsClosed = false, bool bRebootAfterShutdown = true)
218 {
219 bool r;
220 OSVERSIONINFO tOsVerInfo = { sizeof(OSVERSIONINFO) };
221 ::GetVersionEx(&tOsVerInfo);
222 if ( tOsVerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
223 {
224 //Windows95/98系
225 UINT uFlag = bRebootAfterShutdown ? EWX_REBOOT : EWX_POWEROFF;
226 if ( bForceAppsClosed )
227 {
228 uFlag |= EWX_FORCE;
229 }
230 r = Shutdown95(uFlag);
231 }
232 else
233 {
234 //WindowsNT系
235 r = ShutdownNt(NULL, NULL, 0, bForceAppsClosed, bRebootAfterShutdown);
236 }
237 return r;
238 }
239
249 static bool AbortShutdown(LPCTSTR lpMachineName)
250 {
251 if ( ! ms_AddShutdownAuthority(lpMachineName) )
252 {
253 return false;
254 }
255 BOOL r = ::AbortSystemShutdown(CSimpleStr(lpMachineName));
256 return !! r;
257 }
258
268 static bool Suspend(bool fSuspend = true, bool fForce = false)
269 {
270 if ( ! ms_AddAuthority() )
271 {
272 return false;
273 }
274 return !! ::SetSystemPowerState(fSuspend, fForce);
275 }
276
283 static bool GetStatus(LPSYSTEM_POWER_STATUS _lpSystemPowerStatus)
284 {
285 return !! ::GetSystemPowerStatus(_lpSystemPowerStatus);
286 }
287
302 static bool SetShutdownNotifyLevel(WORD level)
303 {
304 DWORD l;
305 DWORD f;
306 if ( ::GetProcessShutdownParameters(&l, &f) )
307 {
308 return !! ::SetProcessShutdownParameters(level, f);
309 }
310 return false;
311 }
312};
313
314
315
316};//TNB
スレッド管理関係のヘッダ
[Ctrl]+[Alt]+[Del]再現
Definition: TnbWindows.h:32
static void Exec(void)
[実行] [Ctrl]+[Alt]+[Del]再現
Definition: TnbWindows.h:55
シャットダウン
Definition: TnbWindows.h:107
static bool Shutdown(bool bForceAppsClosed=false, bool bRebootAfterShutdown=true)
[実行] シャットダウン
Definition: TnbWindows.h:217
static bool Suspend(bool fSuspend=true, bool fForce=false)
[実行] サスペンド
Definition: TnbWindows.h:268
static bool AbortShutdown(LPCTSTR lpMachineName)
[実行] シャットダウン中止.
Definition: TnbWindows.h:249
static bool Shutdown95(UINT uFlags=EWX_POWEROFF)
[実行] シャットダウン
Definition: TnbWindows.h:166
static bool SetShutdownNotifyLevel(WORD level)
[設定] シャットダウンパラメータ設定
Definition: TnbWindows.h:302
static bool GetStatus(LPSYSTEM_POWER_STATUS _lpSystemPowerStatus)
[取得] システム電源状態取得
Definition: TnbWindows.h:283
static bool ShutdownNt(LPCTSTR lpMachineName, LPCTSTR lpMessage, DWORD dwTimeout, bool bForceAppsClosed=false, bool bRebootAfterShutdown=true)
[実行] シャットダウン
Definition: TnbWindows.h:192
簡易文字列管理クラス.
Definition: TnbDef.h:772
static bool Start(LPARAM lParam, IRunner *piRunner, bool withDelete=true, int priority=THREAD_PRIORITY_NORMAL)
[設定] スレッド開始
Definition: TnbThread.h:810
TNB Library
Definition: TnbDoxyTitle.txt:2
簡易スレッド実行クラスのランナー
Definition: TnbThread.h:790