Skip to content

Commit

Permalink
Pull inline code into ReportUsageAndState.
Browse files Browse the repository at this point in the history
Some renaming & refactoring for clarity.
  • Loading branch information
David Lowndes authored and David Lowndes committed Dec 10, 2023
1 parent fb6c759 commit 2a7228d
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions FlipSS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,37 @@ static void MyReportInfo( HANDLE hEvtLog, LPCTSTR pMsg )
MyReportEvent<EVENTLOG_INFORMATION_TYPE>( hEvtLog, pMsg );
}

int CALLBACK WinMain( _In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR lpCmdLine, _In_ int /*nShowCmd*/ )
static void ReportUsageAndState()
{
int RetCode{ 0 };
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
Expand Down Expand Up @@ -95,37 +122,13 @@ int CALLBACK WinMain( _In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR lpCmdLine,
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 */
{
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 szMsg[260];
{
DWORD dwLen = _countof( szMsg ) - 1;
// If this exists, this is the screen saver
if ( RegGetValue( HKEY_CURRENT_USER, "Control Panel\\Desktop", "SCRNSAVE.EXE", RRF_RT_REG_SZ, NULL, szMsg, &dwLen ) != ERROR_SUCCESS )
{
lstrcpy( szMsg, "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",
szMsg,
bActive ? "active" : "inactive" );

MessageBox( NULL, sMsg, szAppName, MB_OK );
}
ReportUsageAndState();
break;

case OptionSwitch::On:
Expand All @@ -149,20 +152,12 @@ int CALLBACK WinMain( _In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR lpCmdLine,

case OptionSwitch::Stop:
{
auto hdsk = OpenInputDesktop( 0, FALSE, READ_CONTROL | DESKTOP_SWITCHDESKTOP );

if ( hdsk == NULL )
{
RetCode = GetLastError();
CString sMsg;
sMsg.Format( "Failed OpenInputDesktop. Error 0x%x", RetCode );
MyReportError( evtLog, sMsg );
}
// 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 )
{
// Switch to the current desktop (the screen saver one)
// before attempting to do things, otherwise they fail.
if ( !SetThreadDesktop( hdsk ) )
{
RetCode = GetLastError();
Expand All @@ -171,6 +166,13 @@ int CALLBACK WinMain( _In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR lpCmdLine,
MyReportError( evtLog, sMsg );
}
}
else
{
RetCode = GetLastError();
CString sMsg;
sMsg.Format( "Failed OpenInputDesktop. Error 0x%x", RetCode );
MyReportError( evtLog, sMsg );
}

{
POINT pt;
Expand Down Expand Up @@ -228,25 +230,25 @@ int CALLBACK WinMain( _In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR lpCmdLine,
case OptionSwitch::Start:
/* Start the screen saver */
{
char szMsg[260];
DWORD dwLen = _countof( szMsg )-1;
if ( RegGetValue( HKEY_CURRENT_USER, "Control Panel\\Desktop", "SCRNSAVE.EXE", RRF_RT_REG_SZ, NULL, szMsg, &dwLen ) == ERROR_SUCCESS )
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 ) );

int ErrVal = reinterpret_cast<int>( ShellExecute( NULL, NULL, szMsg, NULL, NULL, SW_NORMAL ) );
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{szMsg};
CString str{szScreenSaver};
str.MakeUpper();
str.Replace( "SYSTEM32", "sysnative" );
/*int ErrVal = */reinterpret_cast<int>(ShellExecute( NULL, NULL, str, NULL, NULL, SW_NORMAL ));
Expand Down

0 comments on commit 2a7228d

Please sign in to comment.