Sign in to follow this  
Followers 0

Lock Windows Device

11 posts in this topic

#1 ·  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



#2 ·  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

#3 ·  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

#4 ·  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

#5 ·  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.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#6 ·  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

#7 ·  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.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#8 ·  Posted

I will def look into it.

Ill post my finding tmrw.

Share this post


Link to post
Share on other sites

#9 ·  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

#10 ·  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.

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#11 ·  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