Locking the Computer from Code
My previous entry was about shutting down and restarting Windows from code. That leads to the next question which is "How do I lock the work station from code."
This is simpler process. The only thing we need to do is declare the call to the Win32 API for this:
' Lock Workstation. Click here for the API docs on this function.
<DllImport("user32.dll", ExactSpelling:=True)> _
Function LockWorkStation() As Boolean
End Function
Once we have declared the API call we can simply call the function from code like this:
LockWorkStation()
That's it, just a few simple lines of code to use the Win32 API to lock the workstation.
Comments
- Anonymous
March 02, 2005
i recieved the following error when running your code...what could be wrong?
here's my code:
Imports System.Runtime.InteropServices
<DllImport("user32.dll", ExactSpelling:=True)> _
Function LockWorkStation() As Boolean
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
LockWorkStation()
End Sub
An unhandled exception of type 'System.InvalidProgramException' occurred in lockdown.exe
Additional information: Error: PInvoke item (field,method) must be Static. - Anonymous
March 02, 2005
Hi,
If you change the function declaration to the following:
Imports System.Runtime.InteropServices
<DllImport("user32.dll", ExactSpelling:=True)> _
Shared Function LockWorkStation() As Boolean
End Function
You will no longer receive the error. - Anonymous
March 03, 2005
I'm sort of new to VB, havent spent much time with it...im a little cofused as to where i put the code above...and maybe if you could add something to fit the purpose of:
I have sevral PC's which i let my friends game on, and if i want i'd like to lock them down...
i can trun off their port on my Catalyst 1900 switch but that still allows them to play with the machine...
Can i create the program with a password lock wihtout having to log off windows, but so that only i can unlock a machine for them?
Also is thier a way to LAN this program so i can simply run it from my machine...
as i said im new to VB and im not exactley sure of what it can do
thx - Anonymous
March 03, 2005
How can i change the property PATH in the reference DLL - Anonymous
March 06, 2005
This piece of code is in my example of Restart/Shutdown/Log off & Lock here:
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=6d5ba0f2-77c8-43ce-a0ec-4a9a89d47fad - Anonymous
March 18, 2005
Hey there,
I need to implement some sort of feature that will allow me to shut down an MS Access / VB application when the workstation on which the app is running is locked. I haven't had a lot of success getting the On Timer event to work while the workstation is locked, and I wonder if you had any ideas on how to do this progammatically (i.e. without getting the net techs to manually end the tasks on the workstations).
I've been researching this for about a year so any feedback would be highly appreciated. Thanks! - Anonymous
June 15, 2009
PingBack from http://unemploymentofficeresource.info/story.php?id=16721