Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF locking workstation programmatically

I am trying to automate workstation locking after the occurence of a WPF event. The solution is supposed to work for both x64 and x86 architectures.

I've tried the method LockWorkStation() accessible through user32.dll but it works only on x86 architecture. Is there an alternative or a specific configuration to make it work on both architectures? I thought about sending the LWin + L keyboard shortcut but the SendKeys.Send method does not seem to be usable in WPF.

like image 832
Nor El Malki Avatar asked Sep 09 '26 10:09

Nor El Malki


1 Answers

string winDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Windows);
string exec = $@"{winDir}\System32\rundll32.exe";
string command = "user32.dll,LockWorkStation";

System.Diagnostics.Process.Start(exec, command);

works on my win10 x64

enter image description here

like image 150
Doc Avatar answered Sep 12 '26 18:09

Doc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!