📜 ⬆️ ⬇️

Windows: the ability to end the session if the screen is locked

Task


You must be able to terminate the session of another user who blocked the screen in order to log in using his own, provided that:
- the screen is locked automatically after some time
- you are not a system administrator
- there is no possibility to use "fast user switching"
- you can not use the automatic completion of the session on time

And also desirable:
- to avoid the appearance of a black console window in the implementation



Decision


I will not bore you with stories about what solutions you tried and how many crutches you had to break, but a solution was found. Solving such problems is best done with scripts, but this is not always possible, as it was not possible at this time. The fact is that to solve the designated task, it was necessary to run the script as a splash screen, but the script for the splash screen cannot be delivered. To solve this problem, we will use a small C code, which, in turn, runs the vbs script.
')
winexec.cpp

#include "stdio.h" #include "shlwapi.h" void RemoveNewLine (char* str) { char* pos = strrchr(str,'\n'); if (pos) *pos = '\0'; } int main(int argc, char *argv[]) { // Get command line argument if (argc < 2) return 0; if (stricmp(argv[1],"/s")) return 0; // Init vars char config_file[MAX_PATH] = ""; char command[MAX_PATH] = ""; char args[MAX_PATH] = ""; // Get own path and set config_file GetModuleFileName (NULL,config_file,MAX_PATH); PathRemoveExtension (config_file); strcat (config_file,".cfg"); // Read config file FILE* file = fopen (config_file,"r") ; if (file == NULL) { MessageBox (NULL,"Error opening config file",NULL,0); return 1; } fgets (command,MAX_PATH,file); RemoveNewLine (command); fgets (args,MAX_PATH,file); RemoveNewLine (args); // Run process PROCESS_INFORMATION processInformation; STARTUPINFO startupInfo; memset ( &processInformation,0,sizeof (processInformation) ); memset ( &startupInfo,0,sizeof (startupInfo) ); if ( !CreateProcess (command,args,NULL,NULL,0,CREATE_NO_WINDOW,NULL,NULL,&startupInfo,&processInformation) ) { MessageBox (NULL,"ERROR: Create Process failed!",NULL,0); return 1; } // Wait until child process exits. WaitForSingleObject (processInformation.hProcess,INFINITE); // Close process and thread handles. CloseHandle (processInformation.hProcess); CloseHandle (processInformation.hThread); } 

winexec.cfg

c:\windows\system32\cscript.exe
cscript.exe c:\windows\winexec.vbs


winexec.vbs

 'Create common objects Set WshShell = CreateObject("WScript.Shell") Set WshNetwork = CreateObject("WScript.NetWork") 'Set common variables username = WshNetwork.UserName title = "  " & " " & username text = "   ?" 'Run logout dialog Button = WshShell.Popup(text,,title,36) 'Check answer If Button <> 6 Then Wscript.Quit 'Force logoff if "Yes" WshShell.Run "shutdown.exe -l",,true 


How to use it



- Putting the C code into a winexec.scr executable file using mingw
Ubuntu: i586-mingw32msvc-gcc -mwindows -s winexec.cpp -o winexec.scr -lshlwapi
i586-mingw32msvc-gcc -mwindows -s winexec.cpp -o winexec.scr -lshlwapi
Archlinux: i486-mingw32-gcc -mwindows -s winexec.cpp -o winexec.scr -lshlwapi
- Put winexec.scr, winexec.cfg and winexec.vbs in the% WINDIR% directory
- Configure the screensaver (using group policies - gpedit.msc - you can configure the screensaver for all users of the system in a unified way)


How it works


- The screen is locked on timeout and winexec.scr is started, which reads winexec.cfg (winexec.cfg contains the vbs script launch parameters)
- Next, the vbs-script is launched with the question “end the session?”. Pressing the "No" button will lead to the appearance of a lock window in which you can enter a password. Pressing "Yes" ends the session and allows you to log in as another user.
- Despite the fact that the shutdown console command is launched from the vbs-script, the black cmd window does not appear (this is achieved with the CREATE_NO_WINDOW parameter when creating a process in C code)

Comments


- winexec.scr can be used not only to launch a logout script, but also for any other program (or script) that you want to run instead of a screen saver

Source: https://habr.com/ru/post/115730/


All Articles