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.