Lazarus

Programming => General => Topic started by: Davidous on February 02, 2023, 07:05:51 pm

Title: Prevent computer logging out
Post by: Davidous on February 02, 2023, 07:05:51 pm
Hello!

I am looking for a way to prevent computer from logging out. It is because I work on a company laptop and I made a program that generates a very long protocol and it takes time to finish. Until it finishes the computer must not log out. (Since this is a company laptop, automatic logging out after 15 min is a default setting and can not be changed.)  Right now the only way to prevent it is to move the mouse from time to time but for that I need to keep that in mind and do it again and again.
Does anyone know a way to make this "preventing logging out" automatically? I read something about "SetThreadExecutionState" but I don't now the proper way to use it and couldn't find it on google.
Title: Re: Prevent computer logging out
Post by: af0815 on February 02, 2023, 07:19:20 pm
Long time ago there were some programms, they simulate a mousemove to prevent the computer to be sleeping.

Google: windows app no sleeping pc

one of some information:
https://learn.microsoft.com/en-us/windows/powertoys/awake
https://windowsreport.com/prevent-computer-from-sleeping-locking/
Title: Re: Prevent computer logging out
Post by: KodeZwerg on February 02, 2023, 07:20:28 pm
Since you not mention for what kind of OS, for Windows I can provide a solution if needed.
Title: Re: Prevent computer logging out
Post by: Thaddy on February 02, 2023, 07:25:40 pm
Me too and also for nixes.
Title: Re: Prevent computer logging out
Post by: Davidous on February 02, 2023, 07:37:56 pm
I need it for Windows. I would be very grateful for that! :D
Title: Re: Prevent computer logging out
Post by: balazsszekely on February 02, 2023, 07:56:35 pm
I need it for Windows. I would be very grateful for that! :D
Please check attached project. The program will move the mouse after the system is idle for x seconds.
Title: Re: Prevent computer logging out
Post by: af0815 on February 02, 2023, 07:56:58 pm
He said "company laptop". I have never seen another System like Windows in copanies with central IT.


@GemMem: i know this kind of software in pascal since windows have a screensaver :-) Good Shoot
Title: Re: Prevent computer logging out
Post by: dseligo on February 02, 2023, 08:15:47 pm
Please check attached project. The program will move the mouse after the system is idle for x seconds.

Line 52 should be like below if you want to set idle time with spinedit (you left hard coded 9 in that line):
Code: Pascal  [Select][+][-]
  1.   if IdleTime > spSec.Value then
Title: Re: Prevent computer logging out
Post by: KodeZwerg on February 02, 2023, 08:18:18 pm
Hello!

I am looking for a way to prevent computer from logging out. It is because I work on a company laptop and I made a program that generates a very long protocol and it takes time to finish. Until it finishes the computer must not log out. (Since this is a company laptop, automatic logging out after 15 min is a default setting and can not be changed.)  Right now the only way to prevent it is to move the mouse from time to time but for that I need to keep that in mind and do it again and again.
Does anyone know a way to make this "preventing logging out" automatically? I read something about "SetThreadExecutionState" but I don't now the proper way to use it and couldn't find it on google.
You need the SetThreadExecutionState like this:
Code: Pascal  [Select][+][-]
  1. const
  2.  ES_CONTINUOUS       = $80000000;
  3.  ES_DISPLAY_REQUIRED = $00000002;
  4.  ES_SYSTEM_REQUIRED  = $00000001;
  5.  
  6. function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';
Call it in a Timer to preserve system from those flags.
I would call those by interval
Code: Pascal  [Select][+][-]
  1.   SetThreadExecutionState( ES_SYSTEM_REQUIRED );
  2.   SetThreadExecutionState( ES_DISPLAY_REQUIRED );
Also you will need to disable ScreenSaver, either by hand or by code to patch this HKEY:
Code: Pascal  [Select][+][-]
  1. HKEY_CURRENT_USER -> '\Control Panel\Desktop' -> 'ScreenSaveActive' -> '0'
I hope you understand

Any questions left?
Title: Re: Prevent computer logging out
Post by: Davidous on February 02, 2023, 08:23:13 pm
Thank you very much! No questions yet. I will try it tomorrow! :)
Title: Re: Prevent computer logging out
Post by: balazsszekely on February 02, 2023, 08:23:48 pm
@GemMem: i know this kind of software in pascal since windows have a screensaver :-) Good Shoot
Thanks. I'm using it for years, it works fine.


Please check attached project. The program will move the mouse after the system is idle for x seconds.

Line 52 should be like below if you want to set idle time with spinedit (you left hard coded 9 in that line):
Code: Pascal  [Select][+][-]
  1.   if IdleTime > spSec.Value then
That was the easter egg.  :D


Title: Re: Prevent computer logging out
Post by: KodeZwerg on February 02, 2023, 08:34:02 pm
Since Win2k/WinXP I do used my shown way, tricky part was registry.
I programmed it for a media player, moving mouse would start show timeline so that was a no-go in my case.

For registry I can recommend, on startup of your app, collect the info from registry, it is a string, replace that string with really a "0" as string, when ending app, restore your original saved value.
Title: Re: Prevent computer logging out
Post by: Davidous on February 02, 2023, 08:46:08 pm
For the

function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';

I got the error message "cannot declare local procedure as EXTERNAL"
Do you know why this occures?
Title: Re: Prevent computer logging out
Post by: KodeZwerg on February 02, 2023, 08:48:05 pm
Do you know why this occures?
When you show your source I can tell whats wrong.
Title: Re: Prevent computer logging out
Post by: Davidous on February 02, 2023, 09:02:37 pm
My code starts with many constants, after it the procedure begins with "begin" but it is very long. I just got the error with that one line with the "SetThreadExecutionState". If I delete it, the program compiles without any complications. Is there something extra neccessary in the "Uses" part for it to work?

#procedure TForm1.Button13Click(Sender: TObject);

const
  xlBottom = -4107;
  xlTop = -4160;
  xlLeft = -4131;
  xlRight = -4152;
  xlHAlignCenter = -4108;
  xlVAlignCenter = -4108;
  xlContinuous = 1;
  xlThin = 2;
  xlEdgeBottom = 9;
  xlEdgeLeft = 7;
  xlEdgeRight = 10;
  xlEdgeTop = 8;
  xlInsideHorizontal = 12;
  xlInsideVertical = 11;
  xlDown = -4121;
  xlToLeft = -4159;
  xlToRight = -4161;
  xlUp = -4162;
  xlSortNormal = 0;
  xlAscending = 1;
  xlDescending = 2;
  xlSortValues = 1;
  xlPinYin = 1;
  xlToptoBottom = 1;
  xlPasteColumnWidths = 8;
  xlPasteFormats = -4122;
  xlPasteValues = -4163;
  xlPasteSpecialOperationNone = -4142;
  xlNone = -4142;
  xlPageBreakPreview = 2;
  xlFormulas = -4123;
  xlNext = 1;
  xlByRows = 1;
  xlWhole = 1;
  xlDelimited = 1;
  xlDoubleQuote = 1;
  xlOr = 2;
  xlPart = 2;
  xlPortrait = 1;
  xlLandscape = 2;
  xlTypePDF = 0;
  msoTrue=-1;
  xlMedium = -4138;
  xlEqual = 3;
  xlNotEqual = 4;
  xlCellValue = 1;

 ES_CONTINUOUS       = $80000000;
 ES_DISPLAY_REQUIRED = $00000002;
 ES_SYSTEM_REQUIRED  = $00000001;

function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';

begin#
Title: Re: Prevent computer logging out
Post by: KodeZwerg on February 02, 2023, 09:08:41 pm
...
Put it outside of a method(!)
Use it not in a click, use it in a timer(!)

Exemplary:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes , SysUtils , Forms , Controls , Graphics , Dialogs , ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Timer1: TTimer;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure Timer1Timer(Sender: TObject);
  18.   strict private
  19.  
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. const
  34.  ES_CONTINUOUS       = $80000000;
  35.  ES_DISPLAY_REQUIRED = $00000002;
  36.  ES_SYSTEM_REQUIRED  = $00000001;
  37.  
  38. function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.Timer1Timer(Sender: TObject);
  43. begin
  44.   SetThreadExecutionState( ES_SYSTEM_REQUIRED );
  45.   SetThreadExecutionState( ES_DISPLAY_REQUIRED );
  46. end;
  47.  
  48. procedure TForm1.FormCreate(Sender: TObject);
  49. begin
  50.   Timer1.Enabled := True;
  51.   Timer1.Interval := 5000;
  52. end;
  53.  
  54. end.
Title: Re: Prevent computer logging out
Post by: marcov on February 03, 2023, 10:36:41 am
Quote from: KodeZwerg link=topic=62135.msg469562#msg469562 date=1675365498
You need the SetThreadExecutionState like this:
[code=pascal
const
 ES_CONTINUOUS       = $80000000;
 ES_DISPLAY_REQUIRED = $00000002;
 ES_SYSTEM_REQUIRED  = $00000001;

function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';[/code]

Added to trunk.
Title: Re: Prevent computer logging out
Post by: Mr.Madguy on February 03, 2023, 11:22:06 am
...
Put it outside of a method(!)
Use it not in a click, use it in a timer(!)

Exemplary:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes , SysUtils , Forms , Controls , Graphics , Dialogs , ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Timer1: TTimer;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure Timer1Timer(Sender: TObject);
  18.   strict private
  19.  
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. const
  34.  ES_CONTINUOUS       = $80000000;
  35.  ES_DISPLAY_REQUIRED = $00000002;
  36.  ES_SYSTEM_REQUIRED  = $00000001;
  37.  
  38. function SetThreadExecutionState( EXECUTION_STATE : Cardinal ) : Cardinal; stdcall; external 'kernel32.dll' name 'SetThreadExecutionState';
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.Timer1Timer(Sender: TObject);
  43. begin
  44.   SetThreadExecutionState( ES_SYSTEM_REQUIRED );
  45.   SetThreadExecutionState( ES_DISPLAY_REQUIRED );
  46. end;
  47.  
  48. procedure TForm1.FormCreate(Sender: TObject);
  49. begin
  50.   Timer1.Enabled := True;
  51.   Timer1.Interval := 5000;
  52. end;
  53.  
  54. end.
Docs say, that Prev := SetThreadExecutionState(ES_CONTINUOUS or ES_SYSTEM_REQUIRED or ES_DISPLAY_REQUIRED) is enough, so timer isn't needed. But this state should be manually cleared via SetThreadExecutionState(Prev).
Title: Re: Prevent computer logging out
Post by: KodeZwerg on February 03, 2023, 12:28:31 pm
Added to trunk.
Good day, than please like that and not my quick and dirty version  :-*
I was not aware of your action.  :o :-[
Code: Pascal  [Select][+][-]
  1. {$IFDEF MSWINDOWS}
  2. type
  3.   EXECUTION_STATE = UINT32; //4 byte unsigned int
  4.  
  5. const
  6.   //Away mode should be used only by media-recording and media-distribution
  7.   //applications that must perform critical background processing on desktop
  8.   //computers while the computer appears to be sleeping.
  9.   ES_AWAYMODE_REQUIRED = UINT32($00000040); //Enables away mode. This value must be specified with ES_CONTINUOUS.
  10.   ES_SYSTEM_REQUIRED = UINT32($00000001); //Forces the system to be in the working state by resetting the system idle timer.
  11.   ES_DISPLAY_REQUIRED = UINT32($00000002); //Forces the display to be on by resetting the display idle timer.
  12.   ES_CONTINUOUS = UINT32($80000000);
  13.  
  14. //If the function succeeds, the return value is the previous thread execution state.
  15. //If the function fails, the return value is NULL.
  16. //Minimum requirements: Windows XP
  17. function SetThreadExecutionState(esFlags: EXECUTION_STATE): EXECUTION_STATE; stdcall; external 'kernel32.dll';
  18. {$ENDIF}
That is the more better and full variant  O:-)

so timer isn't needed
I do not know to what document you refer, it is just wrong.
Not believe?
Make a test app that set value just once and do nothing, depending on your power configuration your system will go into power saving mode.
Title: Re: Prevent computer logging out
Post by: Nicole on February 03, 2023, 01:37:28 pm
have not read all the answers and risk to give mine twice:
There is a small tool by MS called "MouseMover" or similar.
Title: Re: Prevent computer logging out
Post by: KodeZwerg on February 03, 2023, 03:29:18 pm
have not read all the answers
You not read any answer since what you suggested is already in first reply.
Title: Re: Prevent computer logging out
Post by: Mr.Madguy on February 03, 2023, 07:05:56 pm
I do not know to what document you refer, it is just wrong.
Not believe?
Make a test app that set value just once and do nothing, depending on your power configuration your system will go into power saving mode.
The most obvious docs - are Microsoft's (https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate) ones.
Title: Re: Prevent computer logging out
Post by: Davidous on February 04, 2023, 08:37:00 am
Thank you All!

I implemented KodeZwerg's code and it compiles without problem. :)
TinyPortal © 2005-2018