Skip to content

Instantly share code, notes, and snippets.

@taeguk
Last active March 26, 2024 08:41
Show Gist options
  • Save taeguk/13b607f42020981f6cf9 to your computer and use it in GitHub Desktop.
Save taeguk/13b607f42020981f6cf9 to your computer and use it in GitHub Desktop.
Lock/Unlock Session by the "computer using rule" in Windows.
/*
Lock/Unlock Session by the "computer using rule".
Support versions >= Windows 7
https://gist.github.com/taeguk/13b607f42020981f6cf9
*/
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <Wtsapi32.h>
#include <tchar.h>
#include <locale.h>
#include <string.h>
// constants
#define DEFAULT_LIMIT_COMPUTE_SECONDS 60*40 // 40 min
#define DEFAULT_LIMIT_LOCK_SECONDS 60*10 // 10 min
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "WtsApi32.lib")
bool IsSessionLocked() {
typedef BOOL(PASCAL * WTSQuerySessionInformation)(HANDLE hServer, DWORD SessionId, WTS_INFO_CLASS WTSInfoClass, LPTSTR* ppBuffer, DWORD* pBytesReturned);
typedef void (PASCAL * WTSFreeMemory)(PVOID pMemory);
WTSINFOEXW * pInfo = NULL;
WTS_INFO_CLASS wtsic = WTSSessionInfoEx;
bool bRet = false;
LPTSTR ppBuffer = NULL;
DWORD dwBytesReturned = 0;
LONG dwFlags = 0;
WTSQuerySessionInformation pWTSQuerySessionInformation = NULL;
WTSFreeMemory pWTSFreeMemory = NULL;
HMODULE hLib = LoadLibrary(_T("wtsapi32.dll"));
if (!hLib) {
return false;
}
pWTSQuerySessionInformation = (WTSQuerySessionInformation)GetProcAddress(hLib, "WTSQuerySessionInformationW");
if (!pWTSQuerySessionInformation) {
goto EXIT;
}
pWTSFreeMemory = (WTSFreeMemory)GetProcAddress(hLib, "WTSFreeMemory");
if (pWTSFreeMemory == NULL) {
goto EXIT;
}
DWORD dwSessionID;
if (!ProcessIdToSessionId(GetCurrentProcessId(), &dwSessionID)) {
goto EXIT;
}
if (pWTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionID, wtsic, &ppBuffer, &dwBytesReturned)) {
if (dwBytesReturned > 0) {
pInfo = (WTSINFOEXW*)ppBuffer;
if (pInfo->Level == 1) {
dwFlags = pInfo->Data.WTSInfoExLevel1.SessionFlags;
}
if (dwFlags == WTS_SESSIONSTATE_LOCK) {
bRet = true;
}
}
pWTSFreeMemory(ppBuffer);
ppBuffer = NULL;
}
EXIT:
if (hLib != NULL) {
FreeLibrary(hLib);
}
return bRet;
}
int _tmain(int argc, TCHAR *argv[])
{
TCHAR buff[22];
time_t baseTime, curTime;
time_t LIMIT_COMPUTE_SECONDS = DEFAULT_LIMIT_COMPUTE_SECONDS,
LIMIT_LOCK_SECONDS = DEFAULT_LIMIT_LOCK_SECONDS;
_tsetlocale(LC_ALL, _T(""));
if (argc >= 3) {
LIMIT_COMPUTE_SECONDS = _ttoi(argv[1]);
LIMIT_LOCK_SECONDS = _ttoi(argv[2]);
}
HWND hWndConsole = GetConsoleWindow();
ShowWindow(hWndConsole, SW_HIDE);
baseTime = time(NULL);
while (true) {
curTime = time(NULL);
if (IsSessionLocked()) {
baseTime = curTime;
continue;
}
if (curTime - baseTime > LIMIT_COMPUTE_SECONDS) {
_tcsftime(buff, 20, _T("%Y-%m-%d %H:%M:%S"), localtime(&curTime));
_tprintf(_T("Lock the computer! [%s]\n"), buff);
// Lock the workstation.
if (!LockWorkStation()) {
_tprintf(_T("LockWorkStation failed with %d\n"), GetLastError());
return 1;
}
time_t lockTime = curTime;
curTime = time(NULL);
while (curTime - lockTime < LIMIT_LOCK_SECONDS) {
if (!IsSessionLocked()) {
_tcsftime(buff, 20, _T("%Y-%m-%d %H:%M:%S"), localtime(&curTime));
_tprintf(_T("Current your computer is locked!! [%s]\n"), buff);
Sleep(500);
// Lock the workstation.
if (!LockWorkStation()) {
_tprintf(_T("LockWorkStation failed with %d\n"), GetLastError());
return 1;
}
}
Sleep(300);
curTime = time(NULL);
}
_tcsftime(buff, 20, _T("%Y-%m-%d %H:%M:%S"), localtime(&curTime));
_tprintf(_T("Unlock the computer! [%s]\n"), buff);
baseTime = curTime;
}
Sleep(700);
}
return 0;
}
@juice500ml
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment