News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Stop screensaver

Started by Magnum, October 19, 2009, 01:31:32 AM

Previous topic - Next topic

Magnum

I finished some code that starts the screensaver and I am now working on converting some to stop it.
I need help with the if statement.

Thanks.


;stop_SS.asm Stop windows screensaver
;
.386
.MODEL FLAT, STDCALL
OPTION CASEMAP: NONE

    include \masm32\include\windows.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\advapi32.inc
    include \masm32\include\shlwapi.inc
    include \masm32\macros\macros.asm

    includelib  \masm32\lib\kernel32.lib
    includelib  \masm32\lib\user32.lib
    includelib  \masm32\lib\advapi32.lib
    includelib  \masm32\lib\shlwapi.lib

.DATA 

hWnd    dd  0
Screen  db "Screen-saver",0
hdesk   dd  0

.CODE

start:

; Stopping a screen saver is somewhat more complex. The Microsoft-documented
; way of doing this is to look for the special screen-saver desktop, enumerate
; all windows that desktop, and close them, as follows:

invoke OpenDesktop,addr Screen,0,FALSE,DESKTOP_READOBJECTS or DESKTOP_WRITEOBJECTS
mov hdesk,eax

if (hdesk)
{
   EnumDesktopWindows (hdesk, (WNDENUMPROC)KillScreenSaverFunc, 0);
   CloseDesktop (hdesk);
}


BOOL CALLBACK KillScreenSaverFunc (HWND hwnd, LPARAM lParam)
{
   char szClass [32];
   GetClassName (hwnd, szClass, sizeof(szClass)-1);
   if (strcmpi (szClass, "WindowsScreenSaverClass") == 0)
      PostMessage (hwnd, WM_CLOSE, 0, 0);
   return TRUE;
}

invoke ExitProcess,0

end start

Have a great day,
                         Andy

BlackVortex

First things first : That OpenDesktop method doesn't work for me. It doesn't detect my screensaver on win7 x64

I have a better method :
invoke SystemParametersInfo, SPI_GETSCREENSAVERRUNNING, NULL, addr temp, NULL
cmp dword ptr [temp], 0
...

I made an example program and it detects the screensaver in my win7 x64 and my XP 32bit, so I guess it's foolproof. Also, it's so  perfect that it doesn't detect the "preview" of the screensaver, it only detects the real one !

Next step is to close it   :8)

Magnum

Thanks.

I use this to start the screensaver.
Does it work on Win 7?

Andy


invoke GetDesktopWindow  ; returns handle of the Windows desktop window
mov hWnd, eax

invoke PostMessage,hWnd,WM_SYSCOMMAND,SC_SCREENSAVE,NULL; 


 
Have a great day,
                         Andy

BlackVortex

Good, yes it works  :U   (unless of course the user has disabled the screensaver)

I just found out that it takes a while to start the screensaver, if I check for its existence immediately after starting it, it can't find it. So, for testing I've put a Sleep command of 600 milliseconds so that the screensaver has the chance to start.