0

I want to implement an app that locks my open session on Windows 10, when an event occurs. I saw more or less the same question here , and the answer seems to say, there is no way to lock the Windows screen programatically. However, I already saw some applications do lock Windows screen (for example, Nymi Companion Device Application).

Do you know how to implement the locker? Or which module allow to achieve the task?

saeed
  • 2,477
  • 2
  • 23
  • 40

2 Answers2

1

This is a complete sample code in c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern bool LockWorkStation();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LockWorkStation();
        }
    }
}
saeed
  • 2,477
  • 2
  • 23
  • 40
  • Hi Saeed, thank you. I did try this and the function always returns false. – Tri Vu Khac Sep 14 '17 at 12:44
  • @TriVuKhac have you tried `Process.Start(@"C:\WINDOWS\system32\rundll32.exe", "user32.dll,LockWorkStation");`? – Professor of programming Feb 20 '18 at 15:49
  • Yes, I did. This does not work on UWP. We can look at the function SecondaryAuthenticationFactorRegistration.IsDevicePresenceMonitoringSupported() and the function SecondaryAuthenticationFactorRegistration.RegisterDevicePresenceMonitoringAsync()... This is a start point for properly locking a session. – Tri Vu Khac Feb 21 '18 at 16:13
  • Confirmed this function is not working in UWP. I got error code 5, which is ERROR_ACCESS_DENIED – laishiekai Mar 05 '19 at 22:31
  • That’s for a long time ago , I expect that worked. – saeed Mar 06 '19 at 11:26
  • I used the same code in Desktop Extension then in worked. It just doesn't work in UWP projects. – laishiekai Mar 06 '19 at 22:40
  • There might be an other solution google more and share it here for others – saeed Mar 07 '19 at 20:59
0

This works:

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
public static extern void LockWorkStation();
Avo Nappo
  • 620
  • 4
  • 9
  • Avo, I did try this function, but it always returns false and Windows screen stays unlocked. I am using Windows 10 with SDK and UWP – Tri Vu Khac Sep 14 '17 at 13:24
  • Confirmed this function is not working in UWP. I got error code 5, which is ERROR_ACCESS_DENIED – laishiekai Mar 05 '19 at 22:31