Sign in to follow this  
Followers 0
goldenix

Global Const Q

9 posts in this topic

Look at the Loop, It turns off the monitor, but it does not turn it back unless I hit the esc or 1. I was wondering if it is because of the global Const values ?

I read thte help file, but I cant understand how is global $var different from global Const $var & how is global $var different from local $var different from? :)

#NoTrayIcon

Global Const $lciWM_SYSCommand = 274
Global Const $lciSC_MonitorPower = 61808
Global Const $lciPower_Off = 2
Global Const $lciPower_On = -1

Global $MonitorIsOff = False

HotKeySet("0", "_Monitor_OFF")
HotKeySet("1", "_Monitor_ON")
HotKeySet("{Esc}", "_Quit")

MsgBox(64, "Monitor On/Off", "Press 0 to turn off the monitor." & @LF & _
                            "Press 1 to turn on the monitor back." & @LF & _
                            "Press ESC to turn on the monitor and exit program.")

While 1

Sleep(3000)

_Monitor_OFF()

    Sleep(3000)
    
_Monitor_ON()

WEnd

Func _Monitor_ON()
    $MonitorIsOff = False
    Local $Progman_hwnd = WinGetHandle('[CLASS:Progman]')
   
    DllCall('user32.dll', 'int', 'SendMessage', _
                                                'hwnd', $Progman_hwnd, _
                                                'int', $lciWM_SYSCommand, _
                                                'int', $lciSC_MonitorPower, _
                                                'int', $lciPower_On)
EndFunc

Func _Monitor_OFF()
    $MonitorIsOff = True
    Local $Progman_hwnd = WinGetHandle('[CLASS:Progman]')
   
    While $MonitorIsOff = True
        DllCall('user32.dll', 'int', 'SendMessage', _
                                                    'hwnd', $Progman_hwnd, _
                                                    'int', $lciWM_SYSCommand, _
                                                    'int', $lciSC_MonitorPower, _
                                                    'int', $lciPower_Off)
        _IdleWaitCommit(0)
        Sleep(20)
    WEnd
EndFunc

Func _IdleWaitCommit($idlesec)
    Local $iSave, $LastInputInfo = DllStructCreate ("uint;dword")
    DllStructSetData ($LastInputInfo, 1, DllStructGetSize ($LastInputInfo))
    DllCall ("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr ($LastInputInfo))
    Do
        $iSave = DllStructGetData ($LastInputInfo, 2)
        Sleep(60)
        DllCall ("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr ($LastInputInfo))
    Until (DllStructGetData ($LastInputInfo, 2)-$iSave) > $idlesec Or $MonitorIsOff = False
    Return DllStructGetData ($LastInputInfo, 2)-$iSave
EndFunc

Func _Quit()
    _Monitor_ON()
    Exit
EndFunc

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.1
 Author:         myName
 
 http://www.autoitscript.com/forum/index.php?showtopic=57559

 Script Function:
his example shows how you can turn off your monitor, so even mouse move can not turn it on back.
The main function to turn on/off monitor is taken from Help file example, and was changed for this example's needs.
The function _IdleWaitCommit() i think written by amel27, and also have been changed.

#ce ----------------------------------------------------------------------------

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites



I do not see MonitorIsOff getting set back to False resulting in an infinite while loop in Func _Monitor_OFF()

Nuff said.

Share this post


Link to post
Share on other sites

Everything appears to be right if _IdleWaitCommit() is omitted. Find problem there.

#NoTrayIcon

Global Const $lciWM_SYSCommand = 274
Global Const $lciSC_MonitorPower = 61808
Global Const $lciPower_Off = 2
Global Const $lciPower_On = -1

Global $MonitorIsOff = False

Global $Progman_hwnd = WinGetHandle('[CLASS:Progman]')



HotKeySet("0", "_Monitor_OFF")
HotKeySet("1", "_Monitor_ON")
HotKeySet("{Esc}", "_Quit")

MsgBox(64, "Monitor On/Off", "Press 0 to turn off the monitor." & @LF & _
        "Press 1 to turn on the monitor back." & @LF & _
        "Press ESC to turn on the monitor and exit program.")

While 1

    _Monitor_OFF()

    Sleep(3000)

    _Monitor_ON()

    Sleep(4000)

WEnd

Func _Monitor_ON()

    ;Local $Progman_hwnd = WinGetHandle('[CLASS:Progman]')

    DllCall('user32.dll', 'int', 'SendMessage', _
            'hwnd', $Progman_hwnd, _
            'int', $lciWM_SYSCommand, _
            'int', $lciSC_MonitorPower, _
            'int', $lciPower_On)
    
    $MonitorIsOff = False

EndFunc  

Func _Monitor_OFF()

    ;Local $Progman_hwnd = WinGetHandle('[CLASS:Progman]')

    ;While $MonitorIsOff = True
    DllCall('user32.dll', 'int', 'SendMessage', _
            'hwnd', $Progman_hwnd, _
            'int', $lciWM_SYSCommand, _
            'int', $lciSC_MonitorPower, _
            'int', $lciPower_Off)
    ;_IdleWaitCommit(0)
    ;Sleep(20)
    ;WEnd
    
    $MonitorIsOff = True

EndFunc   

#cs
Func _IdleWaitCommit($idlesec)
    Local $iSave, $LastInputInfo = DllStructCreate("uint;dword")
    DllStructSetData($LastInputInfo, 1, DllStructGetSize($LastInputInfo))
    DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($LastInputInfo))
    Do
        $iSave = DllStructGetData($LastInputInfo, 2)
        Sleep(60)
        DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($LastInputInfo))
    Until (DllStructGetData($LastInputInfo, 2) - $iSave) > $idlesec Or $MonitorIsOff = False
    Return DllStructGetData($LastInputInfo, 2) - $iSave
EndFunc   
#ce


Func _Quit()
    _Monitor_ON()
    Exit
EndFunc

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This works just fine, Hmm...I wonder why the original code was made so complicated.

Thanx trancexx

Can someone answer the Q about globals const?

#NoTrayIcon

Global  $lciWM_SYSCommand = 274
Global  $lciSC_MonitorPower = 61808
Global  $lciPower_Off = 2
Global  $lciPower_On = -1

Global $Progman_hwnd = WinGetHandle('[CLASS:Progman]')

HotKeySet("0", "_Monitor_OFF")
HotKeySet("1", "_Monitor_ON")
HotKeySet("{Esc}", "_Quit")

MsgBox(64, "Monitor On/Off", "Press 0 to turn off the monitor." & @LF & _
        "Press 1 to turn on the monitor back." & @LF & _
        "Press ESC to turn on the monitor and exit program.")
While 1
    _Monitor_OFF()

    Sleep(3000)

    _Monitor_ON()

   ExitLoop
WEnd

Func _Monitor_ON()
    DllCall('user32.dll', 'int', 'SendMessage', _
            'hwnd', $Progman_hwnd, _
            'int', $lciWM_SYSCommand, _
            'int', $lciSC_MonitorPower, _
            'int', $lciPower_On)
EndFunc 

Func _Monitor_OFF()
    DllCall('user32.dll', 'int', 'SendMessage', _
            'hwnd', $Progman_hwnd, _
            'int', $lciWM_SYSCommand, _
            'int', $lciSC_MonitorPower, _
            'int', $lciPower_Off)
EndFunc   

Func _Quit()
    _Monitor_ON()
    Exit
EndFunc
Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites

This works just fine, Hmm...I wonder why the original code was made so complicated.

Thanx trancexx

Can someone answer the Q about globals const?

#NoTrayIcon

Global  $lciWM_SYSCommand = 274
Global  $lciSC_MonitorPower = 61808
Global  $lciPower_Off = 2
Global  $lciPower_On = -1

Global $Progman_hwnd = WinGetHandle('[CLASS:Progman]')

HotKeySet("0", "_Monitor_OFF")
HotKeySet("1", "_Monitor_ON")
HotKeySet("{Esc}", "_Quit")

MsgBox(64, "Monitor On/Off", "Press 0 to turn off the monitor." & @LF & _
        "Press 1 to turn on the monitor back." & @LF & _
        "Press ESC to turn on the monitor and exit program.")
While 1
    _Monitor_OFF()

    Sleep(3000)

    _Monitor_ON()

   ExitLoop
WEnd

Func _Monitor_ON()
    DllCall('user32.dll', 'int', 'SendMessage', _
            'hwnd', $Progman_hwnd, _
            'int', $lciWM_SYSCommand, _
            'int', $lciSC_MonitorPower, _
            'int', $lciPower_On)
EndFunc 

Func _Monitor_OFF()
    DllCall('user32.dll', 'int', 'SendMessage', _
            'hwnd', $Progman_hwnd, _
            'int', $lciWM_SYSCommand, _
            'int', $lciSC_MonitorPower, _
            'int', $lciPower_Off)
EndFunc   

Func _Quit()
    _Monitor_ON()
    Exit
EndFunc
You shouldn't thank me but florisch. The answer is in his post.

Constant cannot be modified.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You shouldn't thank me but florisch. The answer is in his post.

Constant cannot be modified.

Thank you both :)

Its difficult to understand why would someone want to declare a global var, if it cant be modified. ( so that the other process wont be able to change its value? )

About local:

If I put $var = $nr=1 in the beginning of a function, then the var will be declared in the function scope, why should I use local $var = $nr=1 instead?

Edited by goldenix

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites

Thank you both :)

Its difficult to understand why would someone want to declare a global var, if it cant be modified. ( so that the other process wont be able to change its value? )

About local:

If I put $var = $nr=1 in the beginning of a function, then the var will be declared in the function scope, why should I use local $var = $nr=1 instead?

Constants are constants. Constant should be declared as a constant and not as some other variable. Take that as axiom.

Declaring localy, allocated memory is released when exiting function.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Its difficult to understand why would someone want to declare a global var, if it cant be modified. ( so that the other process wont be able to change its value? )

About local:

If I put $var = $nr=1 in the beginning of a function, then the var will be declared in the function scope, why should I use local $var = $nr=1 instead?

Constants can not be changed, not even accidentally ...

Local/global:

In your way of thinking, yes, global and local are not needed, since any var used outside a proc is global and local otherwise.

But (there is always a but) you can find in the Help:

Global = Forces creation of the variable in the Global scope

Local = Forces creation of the variable in the Local/Function scope

and the latter even if a global var with the same name exists. And coding is less error-prone if you use option MustDeclareVars.

Share this post


Link to post
Share on other sites

Ah, was interested in this matter, since I use vars with same name in two functions, like in this example func c and d:

$var = 1
a()
MsgBox (0,"Test a", $var)
b()
MsgBox (0,"Test b", $var & @CR & $var2)
c()

func a()
    local $var = 8
EndFunc

func b()
    $var = 5
    global $var2 = "Global!"
EndFunc

func c()
    local $var3 = "c!"
    local $var4 = "c!"
    d()
    MsgBox (0,"Test C", $var3 & @cr & $var4)
EndFunc

func d()
    $var3 = "d!"
    local $var4 = "d!"
EndFunc

But it seems like the local in func d could be omitted, (against expectation :-).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0