locked
Lock PC RRS feed

  • Question

  • Hi

    I use "ExitWindowsEx" and other Libs:

        Public Enum EnumExitWindows
            WE_LOGOFF = 0
            WE_SHUTDOWN = 1
            WE_REBOOT = 2
            WE_POWEROFF = 8
        End Enum

    Usage:

    ExitWindowsEx(EnumExitWindows.xxx Or EWX_FORCE, &HFFFFS)

    Where xxx can be:

    WE_POWEROFF * WE_LOGOFF * WE_REBOOT

    These 3 work fine! But I need to add "Lock PC/Windows" which I cannot find how?

    Exactly like we "Lock" Windows in Shutdown menu!

    Saturday, May 30, 2020 10:11 PM

Answers

  • Use this, verified it works on Windows 10

    Imports System.Runtime.InteropServices
    
    Public Module Code
        <DllImport("user32.dll")>
        Public Sub LockWorkStation()
        End Sub
    End Module
    Simple call LockWorkStation in a button click or where ever you want.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by OSVBNET Saturday, May 30, 2020 10:42 PM
    Saturday, May 30, 2020 10:21 PM

All replies

  • Use this, verified it works on Windows 10

    Imports System.Runtime.InteropServices
    
    Public Module Code
        <DllImport("user32.dll")>
        Public Sub LockWorkStation()
        End Sub
    End Module
    Simple call LockWorkStation in a button click or where ever you want.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by OSVBNET Saturday, May 30, 2020 10:42 PM
    Saturday, May 30, 2020 10:21 PM
  •    
    <DllImport("User32.dll", SetLastError:=True)> 
    Public Shared Function LockWorkStation() As Boolean  
    End Function




    • Edited by Castorix31 Saturday, May 30, 2020 10:22 PM
    • Proposed as answer by simonb549 Saturday, May 30, 2020 10:56 PM
    Saturday, May 30, 2020 10:22 PM
  • Thx dear Karen, I always like your helpful solutions, it reminds me of Sponge Bob, Karen! :D
    Saturday, May 30, 2020 10:42 PM
  • Thx dear Karen, I always like your helpful solutions, it reminds me of Sponge Bob, Karen! :D

    the definition  is wrong

    it is a function and returns a boolean ; see the definition  : https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-lockworkstation

    Saturday, May 30, 2020 10:56 PM
  • Thx dear Karen, I always like your helpful solutions, it reminds me of Sponge Bob, Karen! :D

    the definition  is wrong

    it is a function and returns a boolean ; see the definition  : https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-lockworkstation

    The method works both as a procedure if you don't want to know the return value which I would not but if you did then use the function definition. No matter it works as delivered. 

    No different then writing a function and not use the return value

    Private Sub Button1_Click(sender As Object, e As EventArgs) _
        Handles Button1.Click
    
        DoSomething()
    
    End Sub
    
    Private Function DoSomething() As Boolean
        If Now.DayOfWeek = DayOfWeek.Saturday Then
            '
            ' Do something for saturday
            '
            Return True
        Else
            '
            ' Do something for all other days
            '
            Return False
        End If
    End Function


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, May 30, 2020 11:40 PM