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:


That's it, just a few simple lines of code to use the Win32 API to lock the workstation.

Comments (7)
  1. rafnex says:

    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


    End Sub

    An unhandled exception of type ‘System.InvalidProgramException’ occurred in lockdown.exe

    Additional information: Error: PInvoke item (field,method) must be Static.

  2. Kevin Baister says:


    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.

  3. michael says:

    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


  4. Sunil Gidwani says:

    How can i change the property PATH in the reference DLL

  5. Crouchie1998 says:

    This piece of code is in my example of Restart/Shutdown/Log off & Lock here:


  6. Jeremy says:

    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!

Comments are closed.

Skip to main content