Sign in to follow this  
Followers 0

Lock Windows Device

11 posts in this topic

Posted

hi all,

i am currently creating a gui has three buttons:

1) Lock

2) Hibernate

3) Shutdown

buttons 2 and 3 to work i just cant figure out how to lock windows.

any help would be much appreciated.

Share this post


Link to post
Share on other sites



Posted

Hi

You should be able to call the following command but not sure how well it will work in AutoIT:

rundll32.exe user32.dll,LockWorkStation

Hope that helps

Adam

Share this post


Link to post
Share on other sites

Posted

rundll32.exe user32.dll,LockWorkStation

Yup. You can either use the Run() command with the above, or direct DllCall.

DllCall("user32.dll", "bool", "LockWorkStation")

Share this post


Link to post
Share on other sites

Posted

thank you all for your quick response.

one more question.

i cant get a yes or no button to work when the user presses the shutdown button.

here is my code, i know that it can use some fine tuning but hey that's why i am here.

thank you all again.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$formMain = GUICreate("Actions", 177, 122, -1, -1)
$btnHibernate = GUICtrlCreateButton("Hibernate", 39, 16, 99, 25)
$btnLock = GUICtrlCreateButton("Lock", 39, 48, 99, 25)
$btnShutdown = GUICtrlCreateButton("Shutdown", 39, 80, 99, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Dim $Hibernate, $Lock, $Shutdown, $answer
While 1
WEnd
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $btnHibernate
   Shutdown (64)
  Case $btnLock
   DllCall("user32.dll", "int", "LockWorkStation")
  Case $btnShutdown
   $answer = MsgBox(4, "Message", "Are you sure you want to Shutdown your device?")
   Switch $answer
    Case 2
    Else
	 Case 1
	 Shutdown (9)
   EndSwitch
EndSwitch
WEnd

Share this post


Link to post
Share on other sites

Posted

There are many errors with that code. First, you have an else in the middle of the second Switch statement that doesn't apply to anything so this script won't even run as is.

Second, you're not reading the buttons at all because you have the code for it after the While loop, so it's never run.

Third, look at the help file for what MsgBox returns when various buttons are pressed because your switch statements for the return from that aren't right.

Share this post


Link to post
Share on other sites

Posted (edited)

after changing some things i still cant get it to work, now the gui just opens and then closes.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$formMain = GUICreate("Actions", 177, 122, -1, -1)
$btnHibernate = GUICtrlCreateButton("Hibernate", 39, 16, 99, 25)
$btnLock = GUICtrlCreateButton("Lock", 39, 48, 99, 25)
$btnShutdown = GUICtrlCreateButton("Shutdown", 39, 80, 99, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Dim $Hibernate, $Lock, $Shutdown, $answer
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $btnHibernate
Shutdown (64)
Case $btnLock
DllCall("user32.dll", "int", "LockWorkStation")
Case $btnShutdown
$answer = MsgBox(4, "Message", "Are you sure you want to Shutdown your device?")
Switch $answer
Case 6
	 Shutdown(9)
Case 7
	 MsgBox(0, "Message", "You pressed no")
EndSwitch
EndSwitch
Edited by antmar904

Share this post


Link to post
Share on other sites

Posted

You removed the While loop, there's nothing to keep the script running. Wrap the section that checks the buttons inside a While loop, look at the help file for GUICreate and you'll see what I am referring to.

Also, try to avoid using the Dim keyword, you should use Global/Local instead.

Share this post


Link to post
Share on other sites

Posted

I will def look into it.

Ill post my finding tmrw.

Share this post


Link to post
Share on other sites

Posted

i think i got it working.

how does this look?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$formMain = GUICreate("Actions", 177, 122, -1, -1)
$btnHibernate = GUICtrlCreateButton("Hibernate", 39, 16, 99, 25)
$btnLock = GUICtrlCreateButton("Lock", 39, 48, 99, 25)
$btnShutdown = GUICtrlCreateButton("Shutdown", 39, 80, 99, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $Hibernate, $Lock, $Shutdown, $answer
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $btnHibernate
   Shutdown (64)
  Case $btnLock
   DllCall("user32.dll", "int", "LockWorkStation")
  Case $btnShutdown
   $answer = MsgBox(4, "Message", "Are you sure you want to Shutdown your device?")
   Switch $answer
    Case 6
	 Shutdown(9)
    Case 7
   EndSwitch
EndSwitch
WEnd

Share this post


Link to post
Share on other sites

Posted

Looks ok now, although the Case 7 isn't really needed unless you're planning on using it for something later. Other than that it works as it should.

antmar904 likes this

Share this post


Link to post
Share on other sites

Posted

ill remove it thanks again :)

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