In fact, I recall writing an Assembly version that was even simpler, circa 1998. Return SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_MONITORPOWER, 2) or return SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0) If (LockWorkStation != NULL) (LockWorkStation)() PROC LockWorkStation = (PROC) GetProcAddress(hinstLib, "LockWorkStation")
HINSTANCE hinstLib = LoadLibrary("user32")
Int WINAPI WinMain(HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow) In fact I found my old C code, dated 2001, that does this plus locks the workstation from the command line: #define WIN 32_LEAN_AND_MEAN The shortcut can be assigned properties like a shortcut key (hotkey) too: Simply create a new shortcut (all entered in one line): powershell (Add-Type 'public static extern int PostMessage(int h,int m,int w,int l) ' -Name a -Pas)::PostMessage(-1,0x0112,0xF170,2) However, instead of SendMessage, use PostMessage, lest the script get stuck waiting for a reply. The method is based on the idea here - running a PowerShell script from the command line (to avoid PowerShell script security).
Hmmm, seems Windows has never improved, because I recall having this requirement back-in-the-day - haha, that was during my first project ever, when I was involved in some Win32 development. I wanted to turn off my monitor in Windows 10, while listening to music, rather than wait for the Power plan “Turn off the display” timer to kick in.