C# Windows 11 Lock Screen Form
Budget: $30 – $250 AUD
I'm looking for a C# example of how to load a form on the Windows 10 and 11 lock screens. This form will be used to display custom messages sourced from a database through a Web API call. I don't need any database work done I just cannot make the form appear on the Windows lock screen. I am a network admin at a school and am trying to do a small project to simplify our audit management.
I need to do something like this example I found on the net :
https://learn.microsoft.com/en-us/answers/questions/668625/creating-windows-on-the-lock-screen
KEY Requirement
- I need the executable to run when the Windows session locks. (It might need a service to trigger this? I have been testing with PSEXEC)
- Display the form on the Windows lock screen.
- Include the underlying code so that I can add the database logic.
- I need it to be simple and reproducible, I am just switching to C# after years of VB.net
I understand that :
- The code will be operating in Session 0
- The code has to be manifested to uiAccess=true such as <requestedExecutionLevel level="requireAdministrator" uiAccess="true" />
- The executable needs to be digitally signed with a trusted certificate (I have created a test certificate and signed it)
- the certificate needs to be added to the trusted Root Certification Authorities store.
- The executable is placed in a secure directory: C:\Program Files
Nothing fancy is required I just need it to be simple and reproduceable.
This is as far as I have gotten:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Program
{
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags, bool fInherit, uint dwDesiredAccess);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetThreadDesktop(IntPtr hDesktop);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool CloseDesktop(IntPtr hDesktop);
private const uint DESKTOP_ACCESS = 0x0100 | 0x0020 | 0x0008 | 0x0010 | 0x0040; // GENERIC_ALL access
[STAThread]
static void Main()
{
IntPtr hWinlogonDesktop = OpenDesktop("Winlogon", 0, false, DESKTOP_ACCESS); //This was my attempt to run in session 0
if (hWinlogonDesktop != IntPtr.Zero)
{
if (SetThreadDesktop(hWinlogonDesktop))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LockScreenForm());
}
else
{
int error = Marshal.GetLastWin32Error();
MessageBox.Show($"Failed to set thread desktop. Error: {error}");
}
CloseDesktop(hWinlogonDesktop);
}
else
{
int error = Marshal.GetLastWin32Error();
MessageBox.Show($"Failed to open Winlogon desktop. Error: {error}");
}
}
}
public class LockScreenForm : Form
{
public LockScreenForm()
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
BackColor = System.Drawing.Color.Black;
Opacity = 0.8;
Label message = new Label
{
Text = "Attention Please!",
Font = new System.Drawing.Font("Arial", 48, System.Drawing.FontStyle.Bold),
ForeColor = System.Drawing.Color.White,
Dock = DockStyle.Fill,
TextAlign = System.Drawing.ContentAlignment.MiddleCenter
};
Controls.Add(message);
}
}
I need to do something like this example I found on the net :
https://learn.microsoft.com/en-us/answers/questions/668625/creating-windows-on-the-lock-screen
KEY Requirement
- I need the executable to run when the Windows session locks. (It might need a service to trigger this? I have been testing with PSEXEC)
- Display the form on the Windows lock screen.
- Include the underlying code so that I can add the database logic.
- I need it to be simple and reproducible, I am just switching to C# after years of VB.net
I understand that :
- The code will be operating in Session 0
- The code has to be manifested to uiAccess=true such as <requestedExecutionLevel level="requireAdministrator" uiAccess="true" />
- The executable needs to be digitally signed with a trusted certificate (I have created a test certificate and signed it)
- the certificate needs to be added to the trusted Root Certification Authorities store.
- The executable is placed in a secure directory: C:\Program Files
Nothing fancy is required I just need it to be simple and reproduceable.
This is as far as I have gotten:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Program
{
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags, bool fInherit, uint dwDesiredAccess);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetThreadDesktop(IntPtr hDesktop);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool CloseDesktop(IntPtr hDesktop);
private const uint DESKTOP_ACCESS = 0x0100 | 0x0020 | 0x0008 | 0x0010 | 0x0040; // GENERIC_ALL access
[STAThread]
static void Main()
{
IntPtr hWinlogonDesktop = OpenDesktop("Winlogon", 0, false, DESKTOP_ACCESS); //This was my attempt to run in session 0
if (hWinlogonDesktop != IntPtr.Zero)
{
if (SetThreadDesktop(hWinlogonDesktop))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LockScreenForm());
}
else
{
int error = Marshal.GetLastWin32Error();
MessageBox.Show($"Failed to set thread desktop. Error: {error}");
}
CloseDesktop(hWinlogonDesktop);
}
else
{
int error = Marshal.GetLastWin32Error();
MessageBox.Show($"Failed to open Winlogon desktop. Error: {error}");
}
}
}
public class LockScreenForm : Form
{
public LockScreenForm()
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
BackColor = System.Drawing.Color.Black;
Opacity = 0.8;
Label message = new Label
{
Text = "Attention Please!",
Font = new System.Drawing.Font("Arial", 48, System.Drawing.FontStyle.Bold),
ForeColor = System.Drawing.Color.White,
Dock = DockStyle.Fill,
TextAlign = System.Drawing.ContentAlignment.MiddleCenter
};
Controls.Add(message);
}
}