Locking the Computer

jose_ajish
04-19-2002, 05:07 AM
Hi,
Can I lock my Computer through Vb. If any one who has worked in Unix .. there is a command in Unix to Lock the Terminal when the user is away.
How can I do the same thing in WIndows using Vb. The Ctlr+Alt+Del key should not work when this command or a button which i specifie is hit.
Thaks in Advance
Ajish

sweetpea
04-19-2002, 05:11 AM
hmmmmmmmmmmm not sure, i guess you could make it password protected.

Flyguy
04-19-2002, 05:26 AM
You can make your program to act as a screensaver, this will disable CTRL-ALT-DEL
This will not work on Windows NT (2000/XP)

matticus_99
04-19-2002, 07:14 AM
I have XP Pro and if I want to lock my computer I usually press
Windows Key + L. Now I guess you could make vb do that, I don't know.

reboot
04-19-2002, 08:13 AM
Here's one way.

Start a new project. Put a command button on the form. Open the code window for that form and paste this code into it.

Option Explicit

Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Sub Command1_Click()

Dim myWinDir As String

myWinDir = String$(255, Chr$(0))

GetWindowsDirectory myWinDir, 255

myWinDir = StripNulls(myWinDir)

Shell myWinDir & "\System32\rundll32.exe User32.dll,LockWorkStation"

End Sub

Private Function StripNulls(OriginalStr As String) As String

'strip nulls from a string
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If

StripNulls = OriginalStr

End Function
Run the project. Click the button. This will lock the workstation, but it won't disable ctl-alt-del.

[edit]

Oops... I look at this again and realize it should be using GetSystemDirectory() and not GetWindowsDirectory(). Like

Option Explicit

Public Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Sub Command1_Click()

Dim mySysDir As String

mySysDir = String$(222, Chr$(0))

GetSystemDirectory mySysDir, 255

mySysDir = StripNulls(mySysDir)

Shell mySysDir & "\rundll32.exe User32.dll,LockWorkStation"

End Sub

Private Function StripNulls(OriginalStr As String) As String

'strip nulls from a string
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If

StripNulls = OriginalStr

End Function

Derek Stone
04-19-2002, 09:44 AM
Just use the LockWorkStation API function. Quite a bit easier.

Good Luck
-CL

reboot
04-19-2002, 10:05 AM
Well... I was originally trying to give him a non api solution, like,

Shell "rundll32.exe User32.dll,LockWorkStation"

then realized I couldn't count on rundll32.exe being in the path, so I added the api call to find the system directory. This, of course, pretty much defeats the whole purpose of the non api solution. Which I guess is a roundabout way of saying, yes, you're right, good catch. :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum