SetSystemPowerState()対応。
@@ -16,9 +16,11 @@ | ||
16 | 16 | var |
17 | 17 | UpParam: String; |
18 | 18 | CanExit: Boolean; |
19 | + HelpDisp: Boolean; | |
19 | 20 | ExtMode: Integer; |
20 | 21 | HibernateMode: Boolean; |
21 | 22 | SuspendFlag: Boolean; |
23 | + SystemPowerState: Boolean; | |
22 | 24 | VerInfo: TOSVersionInfo; |
23 | 25 | tpSv: TTokenPrivileges; |
24 | 26 | i: Integer; |
@@ -30,10 +32,14 @@ | ||
30 | 32 | SetSuspendState : TSetSuspendState; |
31 | 33 | begin |
32 | 34 | CanExit := False; |
35 | + HelpDisp := True; | |
33 | 36 | ExtMode := 0; |
34 | 37 | HibernateMode := False; |
35 | 38 | SuspendFlag := False; |
39 | + SystemPowerState := False; | |
36 | 40 | w := 0; |
41 | + VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); | |
42 | + GetVersionEx(VerInfo); | |
37 | 43 | hPowrprof := LoadLibrary('PowrProf.dll'); |
38 | 44 | if hPowrprof <> 0 then |
39 | 45 | begin |
@@ -40,6 +46,12 @@ | ||
40 | 46 | @IsPwrSuspendAllowed := GetProcAddress(hPowrprof, 'IsPwrSuspendAllowed'); |
41 | 47 | @IsPwrHibernateAllowed := GetProcAddress(hPowrprof, 'IsPwrHibernateAllowed'); |
42 | 48 | @SetSuspendState := GetProcAddress(hPowrprof, 'SetSuspendState'); |
49 | + end | |
50 | + else | |
51 | + begin | |
52 | + IsPwrSuspendAllowed := nil; | |
53 | + IsPwrHibernateAllowed := nil; | |
54 | + SetSuspendState := nil; | |
43 | 55 | end; |
44 | 56 | for i := 1 to ParamCount do |
45 | 57 | begin |
@@ -76,33 +88,64 @@ | ||
76 | 88 | end |
77 | 89 | else if UpParam = 'SUSPEND' then |
78 | 90 | begin |
79 | - if Assigned(@IsPwrSuspendAllowed) and IsPwrSuspendAllowed() then | |
80 | - begin | |
81 | - HibernateMode := False; | |
82 | - SuspendFlag := True; | |
83 | - end; | |
91 | + SystemPowerState := True; | |
92 | + HibernateMode := False; | |
93 | + SuspendFlag := True; | |
84 | 94 | end |
85 | 95 | else if UpParam = 'HIBERNATION' then |
86 | 96 | begin |
87 | - if Assigned(@IsPwrHibernateAllowed) and IsPwrHibernateAllowed() then | |
88 | - begin | |
89 | - HibernateMode := True; | |
90 | - SuspendFlag := True; | |
91 | - end; | |
92 | - end; | |
97 | + SystemPowerState := True; | |
98 | + HibernateMode := True; | |
99 | + SuspendFlag := True; | |
100 | + end; | |
93 | 101 | end; |
94 | 102 | |
95 | 103 | if SuspendFlag and Assigned(@SetSuspendState) then |
96 | 104 | begin |
97 | 105 | // サスペンド処理を追加 |
98 | - SetSuspendState(HibernateMode, False, False); | |
106 | + if SetSuspendState(HibernateMode, False, False) then | |
107 | + SystemPowerState := False; | |
108 | + HelpDisp := False; | |
109 | + end; | |
110 | + if SystemPowerState then | |
111 | + begin | |
112 | + if HibernateMode and Assigned(IsPwrHibernateAllowed) then | |
113 | + begin | |
114 | + // 休止状態の場合 | |
115 | + if not IsPwrHibernateAllowed() then | |
116 | + begin | |
117 | + SystemPowerState := False; | |
118 | + s := '休止状態に移行できませんでした。' + #13#10; | |
119 | + end; | |
120 | + end; | |
121 | + if not HibernateMode and Assigned(IsPwrSuspendAllowed) then | |
122 | + begin | |
123 | + // サスペンドの場合 | |
124 | + if not IsPwrSuspendAllowed() then | |
125 | + begin | |
126 | + SystemPowerState := False; | |
127 | + s := 'サスペンドに移行できませんでした。' + #13#10; | |
128 | + end; | |
129 | + end; | |
130 | + if SystemPowerState then | |
131 | + begin | |
132 | + if VerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then | |
133 | + GetSE_SHUTDOWN(tpSv); | |
134 | + | |
135 | + HibernateMode := not HibernateMode; | |
136 | + SetSystemPowerState(HibernateMode, False); | |
137 | + | |
138 | + if VerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then | |
139 | + ReleaseSE_SHUTDOWN(tpSv); | |
140 | + end | |
141 | + else | |
142 | + begin | |
143 | + s := s + '電源オプションの設定を確認してください。'; | |
144 | + MessageBox(0, PChar(s), '情報', MB_ICONINFORMATION); | |
145 | + end; | |
99 | 146 | end |
100 | 147 | else if CanExit then |
101 | 148 | begin |
102 | - VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); | |
103 | - | |
104 | - GetVersionEx(VerInfo); | |
105 | - | |
106 | 149 | if VerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then |
107 | 150 | GetSE_SHUTDOWN(tpSv); |
108 | 151 |
@@ -138,7 +181,7 @@ | ||
138 | 181 | if VerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then |
139 | 182 | ReleaseSE_SHUTDOWN(tpSv); |
140 | 183 | end |
141 | - else | |
184 | + else if HelpDisp then | |
142 | 185 | begin |
143 | 186 | s := 'Windows の終了を行います。' + #13#10 |
144 | 187 | + #13#10 |