-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlipSS.cpp
285 lines (253 loc) · 7.95 KB
/
FlipSS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <windows.h>
#include <atlstr.h>
const char szAppName[] = "Flip Screen Saver";
#define AMOUNT_TO_MOVE_CURSOR 1
// Experiment with C++20 concepts to restrict an integer value to only the valid ones the API accepts
template<WORD val>
concept EvtLogType = (val == EVENTLOG_SUCCESS) ||
(val == EVENTLOG_ERROR_TYPE) ||
(val == EVENTLOG_WARNING_TYPE) ||
(val == EVENTLOG_INFORMATION_TYPE) ||
(val == EVENTLOG_AUDIT_SUCCESS) ||
(val == EVENTLOG_AUDIT_FAILURE);
template <WORD etVal> requires EvtLogType<etVal>
static void MyReportEvent( HANDLE hEvtLog, LPCTSTR pMsg )
{
if ( hEvtLog != NULL )
{
ReportEvent( hEvtLog, etVal, 0, 0, nullptr, 1, 0, &pMsg, nullptr );
}
}
static void MyReportError( HANDLE hEvtLog, LPCTSTR pMsg )
{
MyReportEvent<EVENTLOG_ERROR_TYPE>( hEvtLog, pMsg );
}
static void MyReportInfo( HANDLE hEvtLog, LPCTSTR pMsg )
{
MyReportEvent<EVENTLOG_INFORMATION_TYPE>( hEvtLog, pMsg );
}
static void ReportUsageAndState()
{
BOOL bActive;
// Get the active/inactive setting (which doesn't appear to have any
// visibility in the Windows 10 UI, but currently still works to
// enable/disable the idle timer aspect of the screen saver)
SystemParametersInfo( SPI_GETSCREENSAVEACTIVE, 0, &bActive, 0 );
// This: https://mskb.pkisolutions.com/kb/318781 this is how to determine whether a screen saver is set
char szScreenSaver[260];
{
DWORD dwLen = _countof( szScreenSaver ) - 1;
// If this exists, this is the screen saver
if ( RegGetValue( HKEY_CURRENT_USER, "Control Panel\\Desktop", "SCRNSAVE.EXE", RRF_RT_REG_SZ, NULL, szScreenSaver, &dwLen ) != ERROR_SUCCESS )
{
lstrcpy( szScreenSaver, "None" );
}
}
CString sMsg;
sMsg.Format( "Usage: FlipSS [/on, /off, /start, or /stop] [/debug (logging to the Windows event log)]\n\n"
"The screen saver '%s' is currently %s",
szScreenSaver,
bActive ? "active" : "inactive" );
MessageBox( NULL, sMsg, szAppName, MB_OK );
}
int CALLBACK WinMain( _In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR lpCmdLine, _In_ int /*nShowCmd*/ )
{
/* Parse the command line, looking for a switch */
LPCSTR pCmd = lpCmdLine;
enum class OptionSwitch { Report, On, Off, Start, Stop } SwitchOn = OptionSwitch::Report; // Default = report current state
bool bDebug = false;
for ( ; *pCmd != '\0'; pCmd++ )
{
/* Is it a switch character? */
if ( *pCmd == '/' )
{
/* OK, what's the command? */
if ( 0 == _strnicmp( pCmd+1, "ON", _countof("ON")-1 ) )
{
pCmd += _countof( "ON" );
SwitchOn = OptionSwitch::On;
}
else
if ( 0 == _strnicmp( pCmd+1, "OFF", _countof( "OFF" )-1 ) )
{
pCmd += _countof( "OFF" );
SwitchOn = OptionSwitch::Off;
}
else
if ( 0 == _strnicmp( pCmd+1, "START", _countof("START")-1 ) )
{
pCmd += _countof( "START" );
SwitchOn = OptionSwitch::Start;
}
else
if ( 0 == _strnicmp( pCmd+1, "STOP", _countof("STOP")-1 ) )
{
pCmd += _countof( "STOP" );
SwitchOn = OptionSwitch::Stop;
}
else
if ( 0 == _strnicmp( pCmd+1, "DEBUG", _countof("DEBUG")-1 ) )
{
pCmd += _countof( "DEBUG" );
bDebug = true;
}
else
{
CString sMsg;
sMsg.Format( "Unrecognised command line parameter - %s", pCmd );
MessageBox( NULL, sMsg, szAppName, MB_OK );
}
}
}
// If not debugging, the log handle will be NULL which is taken to mean "don't log"
auto evtLog = bDebug ?
RegisterEventSource( NULL, "FlipSS" ) :
NULL;
{
CString sMsg;
sMsg.Format( "FlipSS %d", SwitchOn );
MyReportInfo( evtLog, sMsg );
}
int RetCode{ 0 };
switch ( SwitchOn )
{
case OptionSwitch::Report:
/* If we don't have a command line switch, report usage & current state of the saver */
ReportUsageAndState();
break;
case OptionSwitch::On:
case OptionSwitch::Off:
{
const BOOL bActive{ SwitchOn == OptionSwitch::On };
// Although this doesn't show any visible effect in the Windows 10
// UI, it still seems to have the effect of enabling/disabling the
// screen saver timer. The screen saver can still be started using
// /start, so this only affects the automatic timer aspect.
if ( !SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, bActive, 0, SPIF_SENDWININICHANGE ) )
{
RetCode = GetLastError();
CString sMsg;
sMsg.Format( "Failed SystemParametersInfo. Error 0x%x", RetCode );
MyReportError( evtLog, sMsg );
}
}
break;
case OptionSwitch::Stop:
{
// Try to switch to the current desktop (the screen saver one)
// before attempting to do things, otherwise they fail.
const auto hdsk = OpenInputDesktop( 0, FALSE, READ_CONTROL | DESKTOP_SWITCHDESKTOP );
if ( hdsk != NULL )
{
if ( !SetThreadDesktop( hdsk ) )
{
RetCode = GetLastError();
CString sMsg;
sMsg.Format( "Failed SetThreadDesktop. Error 0x%x", RetCode );
MyReportError( evtLog, sMsg );
}
}
else
{
RetCode = GetLastError();
CString sMsg;
sMsg.Format( "Failed OpenInputDesktop. Error 0x%x", RetCode );
MyReportError( evtLog, sMsg );
}
{
POINT pt;
/* Clear the screen saver by simulating mouse cursor movement */
if ( GetCursorPos( &pt ) )
{
// Loop a number of times, moving the cursor because a single move doesn't seem to do the trick!
for ( int cnt = 0; cnt < 7; ++cnt )
{
if ( SetCursorPos( pt.x + cnt*AMOUNT_TO_MOVE_CURSOR, pt.y + cnt*AMOUNT_TO_MOVE_CURSOR ) )
{
Sleep( 20 );
}
else
{
RetCode = GetLastError();
CString sMsg;
sMsg.Format( "Failed SCP. Error 0x%x", RetCode );
MyReportError( evtLog, sMsg );
break;
}
}
// Move it back again
SetCursorPos( pt.x, pt.y );
}
else
{
RetCode = GetLastError();
CString sMsg;
sMsg.Format( "Failed GCP. Error 0x%x", RetCode );
MyReportError( evtLog, sMsg );
}
/* Try this old method if its still running */
{
HWND hSaver = FindWindow( "WindowsScreenSaverClass", NULL );
if ( hSaver != NULL )
{
PostMessage( hSaver, WM_CLOSE, 0, 0 );
MyReportInfo( evtLog, "Close screen saver window" );
}
}
}
if ( hdsk != NULL )
{
CloseDesktop( hdsk );
}
}
break;
case OptionSwitch::Start:
/* Start the screen saver */
{
char szScreenSaver[260];
DWORD dwLen = _countof( szScreenSaver )-1;
if ( RegGetValue( HKEY_CURRENT_USER, "Control Panel\\Desktop", "SCRNSAVE.EXE", RRF_RT_REG_SZ, NULL, szScreenSaver, &dwLen ) == ERROR_SUCCESS )
{
// Initialise COM for any possible case where ShellExecute may require it
// (though it's not normally needed to invoke a screen saver).
// Note: Intentionally discard the return value, if it fails, so be it,
// there's nothing critical if it does!
static_cast<void>( CoInitializeEx( NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE ) );
const int ErrVal = reinterpret_cast<int>( ShellExecute( NULL, NULL, szScreenSaver, NULL, NULL, SW_NORMAL ) );
if ( ErrVal < 32 )
{
// If it's this error, it's likely because the path is system32 and a 32-bit
// application gets redirected to where there is no copy of the file, so...
if ( ErrVal == ERROR_FILE_NOT_FOUND )
{
// Replace system32 with sysnative to allow 32-bit application to side-step file system redirection
CString str{szScreenSaver};
str.MakeUpper();
str.Replace( "SYSTEM32", "sysnative" );
/*int ErrVal = */reinterpret_cast<int>(ShellExecute( NULL, NULL, str, NULL, NULL, SW_NORMAL ));
}
}
CoUninitialize();
}
else
{
HWND hWnd;
//hWnd = GetConsoleWindow();
//wsprintf( szMsg, "Console hWnd 0x%x", hWnd );
//MyReportInfo( evtLog, szMsg, bDebug );
hWnd = GetForegroundWindow();
CString sMsg;
sMsg.Format( "Foreground hWnd 0x%p", hWnd );
MyReportInfo( evtLog, sMsg );
if ( !PostMessage( hWnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0 ) )
{
RetCode = GetLastError();
sMsg.Format( "Failed PostMessage. Error 0x%x", RetCode );
MyReportError( evtLog, sMsg );
}
}
}
break;
}
return RetCode;
}