Simulating the hotkey is the wrong approach. All you need to do is call the LockWorkStation
function. This has the same result as pressing Ctrl+Alt+Del and selecting "Lock Workstation", or using the Win+L hotkey, except that you can do it programmatically through code.
To call this function from a VB application, you'll need to write a declaration, like so:
Private Declare Function LockWorkStation Lib "user32.dll" () As Long
You'll want to place that declaration at the top of your module file, before any procedures are defined. Then, inside one of the procedures, you can call the function. For example:
Private Sub LockComputer()
LockWorkStation
End Sub
Even better code would check the return value of LockWorkStation
for an error code. A return value of 0 indicates an error. The standard way of checking for Win32 errors in VB, Err.LastDllError
, will give you more information about what exactly went wrong.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…