Skip to content

Instantly share code, notes, and snippets.

@piotrmaslanka
Created December 30, 2021 14:16
Show Gist options
  • Save piotrmaslanka/79ef449cc91d13b593f7182ed44d5f97 to your computer and use it in GitHub Desktop.
Save piotrmaslanka/79ef449cc91d13b593f7182ed44d5f97 to your computer and use it in GitHub Desktop.
A program that I wrote that was intended to prevent my sister from using her computer for too long.
program TOFF;
uses
Registry, Windows;
var
r: TRegistry;
h1, min1, h2: Integer;
st: _SYSTEMTIME;
godown: Boolean;
function MyExitWindows(RebootParam: Longword): Boolean;
var
TTokenHd: THandle;
TTokenPvg: TTokenPrivileges;
cbtpPrevious: DWORD;
rTTokenPvg: TTokenPrivileges;
pcbtpPreviousRequired: DWORD;
tpResult: Boolean;
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
tpResult := OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
TTokenHd);
if tpResult then
begin
tpResult := LookupPrivilegeValue(nil,
SE_SHUTDOWN_NAME,
TTokenPvg.Privileges[0].Luid);
TTokenPvg.PrivilegeCount := 1;
TTokenPvg.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
cbtpPrevious := SizeOf(rTTokenPvg);
pcbtpPreviousRequired := 0;
if tpResult then
Windows.AdjustTokenPrivileges(TTokenHd,
False,
TTokenPvg,
cbtpPrevious,
rTTokenPvg,
pcbtpPreviousRequired);
end;
Result := ExitWindowsEx(RebootParam, 0);
end;
begin
while True do
begin
r := TRegistry.Create();
r.OpenKey('\Software\Microsoft\TOFF', True);
try
h1 := r.ReadInteger('HFROM');
min1 := r.ReadInteger('MFROM');
h2 := r.ReadInteger('HTO');
except
r.WriteInteger('HFROM', 22);
r.WriteInteger('MFROM', 00);
r.WriteInteger('HTO', 6);
r.CloseKey;
r.Destroy;
continue;
end;
Windows.GetSystemTime(st);
godown := false;
r.CloseKey;
r.Destroy;
if st.wHour > h1 then
if st.wMinute > min1 then
godown := true;
godown := godown or ((st.wHour < h2));
if godown then MyExitWindows(EWX_POWEROFF or EWX_FORCE);
Sleep(60);
end;
{ TODO -oUser -cConsole Main : Insert code here }
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment