Share via

Why LockWorkStation function is not locking the screen, when I restart the system from Windows Start Menu?

Sireesha Pallela 41 Reputation points
2022-08-23T08:29:53.423+00:00

I have a problem with LockWorkStation function that this function main process is to lock the system whenever we call the function. But it is not reflecting in few scenarios even if all conditions are satisfied. If I am trying to lock the screen after restart by using this function it is not locking the screen but the logs are showing as the function is success. "When I restart the system by using the keyword (alt + f4), the function is called and it's working, but whenever I try to restart the system using start menu, the screen is not locked even if the function succeeded. I also tried to make the process in interactive mode but still it won't work." And When I use a MessageBox before this function, It will work in all scenarios. But without MessageBox it's not working in manual Windows restart from start menu.

This is the code I use:

#include<Windows.h>  
    #include <iostream>  
      
    using namespace std;  
    int main()  
    {  
      
        BOOL result = LockWorkStation();  
        if (!result)  
        {  
            cout << GetLastError();  
      
        }  
    }  

When I place a messageBox in the following code it works:

 #include<Windows.h>  
   #include <iostream>  
      
    using namespace std;  
    int main()  
    {  
        MessageBox(NULL, L"Before", L"Status", MB_OK);  
        BOOL result = LockWorkStation();  
        if (!result)  
        {  
            cout << GetLastError();  
        }  
    }  

I am calling this .exe from my source code as below:

CreateProcessAsUser(  
                    hUserToken,  
                    L"D:\\New folder\\LockScreen\\x64\\Release\\LockScreen.exe",  
                    (LPWSTR)L" ",  
                    NULL,  
                    NULL,  
                    FALSE,  
                    CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_CONSOLE,  
                    pEnv,  
                    NULL,  
                    &si,  
                    &pi  
                );  

Now, I want to successfully run this code in all cases without using any MessageBox.

Windows development | Windows API - Win32
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Answer accepted by question author

RLWA32 52,566 Reputation points
2022-09-01T08:28:27.917+00:00

As I said before when you restart the system from the start menu the system will automatically logon the last interactive use and then lock the workstation. When you enter the password you are actually unlocking the workstation. This behavior can be controlled by group policy. You can see and edit the related policy with the group policy editor (gpedit.msc). It looks like this on my Windows 10 21H1 system -

236807-policy.png

An Alt+F4 restart does not seem to cause the policy to be applied. No, I don't know why there is a difference. But the policy is the cause of the difference between restart methods and whether or not there is an automatic logon and lock.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.