locked
How to implement C# code to automatically lock/unlock screen? RRS feed

  • Question

  • Hi, 

    How to implement C# code to automatically lock/unlock screen?

    Thank you,


    Jerry

    Monday, May 28, 2012 9:40 AM

Answers

  • A code like the one they show here should work fine on .NET desktop apps:

    Lock Windows workstation programmatically in C# 

    http://stackoverflow.com/questions/1263047/lock-windows-workstation-programmatically-in-c-sharp

    But LockWorkStation API will return Access Denied on a Metro app. The reason for that is that this API is only available for Desktop apps, as you can see here in Winuser.h from the latest Windows SDK:

    #pragma region Desktop Family
    #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
    
    ...
    
    WINUSERAPI
    BOOL
    WINAPI
    LockWorkStation(
        VOID);
    #endif /* _WIN32_WINNT >= 0x0500 */
    
    …
    
    #endif /* WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) */
    #pragma endregion
    

    So you cannot lock the workstation programmatically on Metro apps.

    --------------------------------------------------------------------------------

    Alejandro Campos Magencio - Microsoft Escalation Engineer - Forum Moderator
     If my reply answers your question, please mark this post as answered.

    Monday, May 28, 2012 2:49 PM
    Moderator