Blocking the Windows Screen Saver in Delphi

 Delphi, Windows, Windows 10, Windows 7, Windows 8.1  Comments Off on Blocking the Windows Screen Saver in Delphi
May 222019
 

Sometimes your program needs to block the screen saver from automatically kicking in. My use case was that the program was recording data and whenever the screen saver was active, the data was lost (No idea why, it probably had something to do with the way HID is implemented in Windows.)
So I was looking for a way to fix that without forcing the user to turn off the screen saver. The methods that used to work under Windows XP no longer work in Windows 7 and later (I don’t care about Vista), so I googled and found this question on StackOverflow. The Windows API functions PowerCreateRequest + PowerSetRequest mentioned in the highest voted answer looked promising. Unfortunately they don’t seem to be available in Delphi (Delphi 2007, which I used for that project, is too old to know them, but I couldn’t find them in Delphi 10.3 either). The first task was therefore to get a function declaration for Delphi. Google didn’t help here which meant that I had to create them myself. Not a big deal:

type
  TPowerCreateRequest = function(_Context: PReasonContext): THandle; stdcall;
  TPowerSetRequest = function(_Handle: THandle; _RequestType: TPowerRequestType): LongBool; stdcall;
  TPowerClearRequest = function(_Handle: THandle; _RequestType: TPowerRequestType): LongBool; stdcall;

I prefer loading such functions at runtime rather than the program not starting because some external reference is not avaiable. These functions are exported by kernel32.dll.

  FDllHandle := SafeLoadLibrary(kernel32);
  PowerCreateRequest := GetProcAddress(FDllHandle, 'PowerCreateRequest');
  PowerSetRequest := GetProcAddress(FDllHandle, 'PowerSetRequest');
  PowerClearRequest := GetProcAddress(FDllHandle, 'PowerClearRequest');
  if not Assigned(PowerCreateRequest) or not Assigned(PowerSetRequest) or not Assigned(PowerClearRequest) then
    raise EOsFunc.Create(_('Could not initialize the PowerXxxxRequest functions from kernel32.'));

Usage is not without its own problems. First, I had to declare the constants and parameters:

const
  POWER_REQUEST_CONTEXT_VERSION = 0;
  POWER_REQUEST_CONTEXT_DETAILED_STRING = 2;
  POWER_REQUEST_CONTEXT_SIMPLE_STRING = 1;
type
  PReasonContext = ^TReasonContext;
  TReasonContext = record
    Version: ULONG;
    Flags: DWORD;
    case Boolean of
      False: (
        SimpleReasonString: PWideChar;
        );
      True: (
        Detailed: record
          LocalizedReasonModule: HMODULE;
          LocalizedReasonId: ULONG;
          ReasonStringCount: ULONG;
          ReasonStrings: PPWideChar;
        end;
        );
  end;
type
  TPowerRequestType = (
    PowerRequestDisplayRequired = 0,
    PowerRequestSystemRequired = 1,
    PowerRequestAwayModeRequired = 2,
    PowerRequestExecutionRequired = 3);

Now, how do these functions work?

The first thing to do is creating a power request with PowerCreateRequest. This function requires a PReasonContext pointer which must be initialized correctly. The Version and Flags fields are simple: Assign one of the POWER_REQUEST_CONTEXT_xxx constants declared above. But what about the other fields? I decided to go with the simple case, that is: Set Flags to POWER_REQUEST_CONTEXT_SIMPLE_STRING and provide a value for SimpleReasonString.

var
  FRequestHandle: THandle;
  FContext: TReasonContext;
  FReason: array[0..255] of WideChar;
  // [...]
  FContext.Version := POWER_REQUEST_CONTEXT_VERSION;
  FContext.Flags := POWER_REQUEST_CONTEXT_SIMPLE_STRING;
  FContext.SimpleReasonString := @FReason;
  FRequestHandle := PowerCreateRequest(@FContext);
  if FRequestHandle = INVALID_HANDLE_VALUE then
    RaiseLastOSError;

Where FReason is an array of WideChar. My tests showed that the TReasonContext record and the reason string it points to must be available through the lifetime of the reason context. If it isn’t, the reason displayed by the powercfg tool (see below) will be corrupted. Therefore I did not use a WideString but a static array.

After the power request has been created, calls to PowerSetRequest and PowerClearRequest are possible.

  Win32Check(PowerSetRequest(FRequestHandle, PowerRequestDisplayRequired));

This call prevents the screen saver from starting automatically. A call to PowerClearRequest supposedly turns that off again (but I haven’t tested it).

I mentioned the powercfg tool above. It’s a Windows command line tool that among other functionality can display processes that have active power requests. e.g.

powercfg /requests
DISPLAY:
[PROCESS] \Device\HarddiskVolume2\Source\dzlib\tests\BlockScreenSaver\BlockScreenSaver.exe
test

SYSTEM:
None.

AWAYMODE:
None.

EXECUTION:
None.

PERFBOOST:
None.

The string “test” is the reason I passed to PowerCreateRequests.

I mentioned that failing to preserver the reason string results in a corrupted message in the display. It looked like this:

powercfg /requests
DISPLAY:
[PROCESS] \Device\HarddiskVolume2\Source\dzlib\tests\BlockScreenSaver\BlockScreenSaver.exe
?a?E?I???↑?E?↑?E?↑?E?↑?E?↑

Note that this tool requires administrator privileges (but power requests don’t).

I have added this code to my dzlib. It’s in u_dzOsUtils. There is also a simple test / demo program BlockScreenSaver.

If you would like to comment on this, go to this post in the international Delphi Praxis forum.

 Posted by on 2019-05-22 at 14:15

VirtualBox and Windows 8.1: This 64-bit application couldn’t load because your PC doesn’t have a 64-bit processor

 Windows, Windows 8.1  Comments Off on VirtualBox and Windows 8.1: This 64-bit application couldn’t load because your PC doesn’t have a 64-bit processor
Aug 172017
 

I just moved a Windows 8.1 installation in Virtual Box from one computer to another. When booting up, Windows told me:

This 64-bit application couldn’t load because your PC doesn’t have a 64-bit processor

The host computer is an Intel Xeon CPU which definitely is a 64 bit CPU (the previous computer was an older AMD 6 core CPU which was also 64 bit).

Oddly enough I could not find any solution on the interweb tubes (my Google fu seems to have weakened or maybe Google search isn’t as helpful as it used to be because it tries to guess what the user is searching for rather than simply searching for what he has typed).

It took me a while to figure out what the problem was: For some reason the virtual machine’s configuration had changed on the “General” -> “Basic” page from Version = “Windows 8.1 (64-bit)” to “Windows 7 (32-bit)”. Which apparently means that the CPU reported to the OS is a 32 bit CPU. Changing this back to the original value solved the problem.

 Posted by on 2017-08-17 at 16:55

Adding a Windows 8.1 computer to a SAMBA domain

 Windows, Windows 8.1  Comments Off on Adding a Windows 8.1 computer to a SAMBA domain
Jun 262017
 

Note to self: If adding a Windows 8.1 computer to a SAMBA domain fails with the error “The specified domain either does not exist or could not be contacted” the following changes to the Registry might help:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\LanmanWorkstation\Parameters]
; Enable NT-Domain compatibility mode
; Default:
; [value not present]
; "DomainCompatibilityMode"=-
"DomainCompatibilityMode"=dword:00000001

; Disable required DNS name resolution
; Default:
; [value not present]
; "DNSNameResolutionRequired"=-
"DNSNameResolutionRequired"=dword:00000000


[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Netlogon\Parameters]
; Disable requirement of signed communication
; My Samba (3.0.33) works with signed communication enabled, so no need to disable it.
; Default:
; "RequireSignOrSeal"=dword:00000001
; Disable the usage of strong keys
; Default:
; "RequireStrongKey"=dword:00000001
"RequireStrongKey"=dword:00000000

source: the answer from gigiga in the social.technet.microsoft.com forum.

 Posted by on 2017-06-26 at 12:13

dzEditorLineEndsFix 1.0.3 released

 Delphi, Windows, Windows 8.1  Comments Off on dzEditorLineEndsFix 1.0.3 released
Jan 212017
 

I have released a new version of my dzEditorLineEndsFix tool for Delphi 2006 to 2010. There is only one change: I removed the balloon hint it used to show at startup. It started to annoy the hell out of me (and I’m probably not the only one).

The tool also now has its own page on this blog.

 Posted by on 2017-01-21 at 13:11

Enable Hibernate on Windows 8.x

 Windows, Windows 8.1  Comments Off on Enable Hibernate on Windows 8.x
Jan 202017
 

Note to self:

If Hibernate is not available on the shutdown menu and doesn’t show up in the configuration either, it can be enabled via the command line:

powercfg -h on
 Posted by on 2017-01-20 at 19:14

Filter multiple criteria in Windows Explorer

 Windows, Windows 7, Windows 8.1  Comments Off on Filter multiple criteria in Windows Explorer
Nov 092016
 

Note to self: It is possible to filter on multiple criteria – e.g. extensions – in Windows Explorer by combining them with OR:

.txt OR .doc
  • The OR must be written all upper case (AND is also possible).
  • *.txt will not work
  • It will search recursively
  • A semicolon (as in file filters) does not work.

More on filtering, grouping and searching here.

 Posted by on 2016-11-09 at 12:01

WinHlp32 for Windows 8.1

 Windows 8.1  Comments Off on WinHlp32 for Windows 8.1
Dec 202015
 

Microsoft dropped support for the old WinHelp (*.hlp) file format in Windows 7 (or was it in Vista?). They provided a download that added the missing WinHlp32.exe (and probably quite a few other files) back so we could display .hlp files again. Today I had the need to do that on Windows 8.1. Unfortunately I run into several issues:

  • There are multiple downloads and it isn’t trivial to find the right one (Update for Windows 8.1 for x64-based Systems (KB917607) worked for me)
  • The download is a .msu file which doesn’t install if your windows does not have one of the supported language packs. If you get the error “The update does not apply to your system”, install the English-US language pack (I didn’t have that, I used English-GB.)
  • The installer package takes forever to prepare, then asks you whether you want to install KB917607. After a while it looks as if it is finished. I tried to open a .hlp file and it still didn’t work. Looking closer revealed yet another window open requiring me to acknowledge the license terms. So I did that and lo and behold: I could finally view .hlp files again.

Why does Microsoft make everything so complicated?

 Posted by on 2015-12-20 at 19:37

Add a “Scan with Windows Defender” context menu to any folder or file

 Windows, Windows 8.1  Comments Off on Add a “Scan with Windows Defender” context menu to any folder or file
Nov 282015
 

Windows Defender is the virus scan tool that is included with Windows 8 and later. While it provides basic security it does not have any of the convenience functions that other virus scanners have. In particular I miss a context menu option to scan a file or folder.

ScanWithWindowsDefenderMenu

So I turned to Google and found How to add the Windows defender into Windows Explorer’s right click menu on SuperUser and How to Add Any Application Shortcut to Windows Explorer’s Context Menu on How-To Geek. Both go through great length with pictures and text to describe how to do it, but basically a simple .reg file would have been sufficient.

To add a "Scan file with Windows Defender" entry for all files, you need this:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\WindowsDefender]
"Icon"="%ProgramFiles%\\\\Windows Defender\\\\EppManifest.dll"
"MUIVerb"="Scan with Windows Defender"

[HKEY_CLASSES_ROOT\*\shell\WindowsDefender\Command]
@="\"c:\\Program Files\\Windows Defender\\MpCmdRun.exe\"  -scan -scantype 3 -SignatureUpdate -file %1"

(download link)

And for a "Scan folder with Windows Defender" entry for each folder, you need this:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\WindowsDefender]
"Icon"="%ProgramFiles%\\\\Windows Defender\\\\EppManifest.dll"
"MUIVerb"="Scan folder with Windows Defender"

[HKEY_CLASSES_ROOT\Folder\shell\WindowsDefender\Command]
@="\"c:\\Program Files\\Windows Defender\\MpCmdRun.exe\"  -scan -scantype 3 -SignatureUpdate -file %1"

(download link)

Both open a console window for the console version of Windows Defender to run. That’s a bit ugly but we aren’t in this for a design award, are we?

To add these files to the registry just double click them. Windows will warn you about possible threads. It’s a good idea to read through files like these before double clicking. Maybe I am just trying to hack your computer? 😉

It works for me on Windows 8.1, but it should also work on other Windows versions like 10.

 Posted by on 2015-11-28 at 16:34

Fighting Secure Boot

 Windows, Windows 8.1  Comments Off on Fighting Secure Boot
Oct 232014
 

I have bought an Acer Extensa notebook after reading the under 300 Euros notebooks test in the latest c’t magazine where it came up as the winner regarding battery life and the rest wasn’t too bad either. I chose the 4 GB model so it won’t thrash the hd all the time. The Extensa comes pre-installed with Windows 8.1 and – as so many computers nowadays comes without an install medium and also without a user’s manual. (The link given in the short setup guide for downloading the manual http://go.acer.com/?id=17833 leads to a non-functional site. Not very user friendly in my book.)

Now, what is the first thing you do, when you get a new computer which comes pre-installed with an operating system but does not come with an install medium? I for one, make a backup, preferably an image backup of the whole hard disk using Clonezilla. Since this is my image backup tool of choice I carry it with me on a USB stick almost all the time (Hey, I work in IT, so it’s pretty much normal to carry USB sticks and other stuff. 😉 ). So I plugged that USB stick into the notebook and booted it up. It went straight into the Windows 8.1 setup screen. 🙁

So I tried to get a boot menu. Perusing Google told me that Acer notebooks use F12 for the boot menu. Unfortunately this didn’t work. Windows 8.1 setup again. 🙁

Next, I tried to get into the BIOS, or whatever the UEFI stuff nowadays calls this tool. The usual DEL key didn’t work but after several reboots and key presses I ended up in some windows boot menu that allowed me to boot from an USB stick. Only, it didn’t. It told me there was a secure boot failure and stopped.

Turning the computer off and on again, this time I apparently got the BIOS setup key right: F2 (It didn’t work the first several times I tried it, why?) I got something called “Insydeh” which looked like a BIOS of old. And there it was: An option to turn off “secure boot”, only it was disabled. I could only switch to BIOS mode which I didn’t want to. WTF?

Google to the rescue again: To turn off secure boot, you first must set a supervisor password. So I did that, came back to the secure boot screen and lo and behold, the option to turn it off was enabled now. After turning it off, I could clear the supervisor password and the option was still enabled. Another setting I changed was the F12 boot menu. It was disabled by default so I enabled it.

Save and reboot, press F12 and – voila – a boot menu which finally allowed me to boot Clonezilla from my USB stick. The backup is running now.

Praise Microsoft for requiring PC manufacturers to have an option to turn off secure boot if they want to be Windows 8 compliant (I wonder whether that will still be a requirement for Windows 10, though.). But curse Microsoft and the bloody PC manufacturers to come up with the pretty much useless secure boot feature at all. It’s my computer, I paid for it, so it should be my choice to install whatever operating system I want on it!

 Posted by on 2014-10-23 at 20:26

Updating Windows Defender signatures (only)

 Windows 8.1  Comments Off on Updating Windows Defender signatures (only)
Apr 082014
 

One of my problems with Windows 8 is that Microsoft recommends to keep Automatic Updates on the setting “Install updates automatically” while I prefer the setting “Download updates but let me choose whether to install them”. (Actually I don’t really want to choose whether to install them but rather when to install them.)

It’s possible to change this setting, but Windows Defender then won’t download its signature updates because that’s bound to the same mechanism as Windows Update. So every day I get a message on my boot screen, that there are important updates available and that I should start Windows Update to install them. This is annoying like hell.

Thankfully with a little bit of googling I found a solution. There is a tool that that checks for signature updates (an supposedly downloads them, I couldn’t try it yet because I just updated them using Windows update). It must be called like this:

"C:\Program Files\Windows Defender\MpCmdRun.exe" -SignatureUpdate -MMPC

So adding this command to e.g. task scheduler should solve yet another Windows 8 annoyance for good.

 Posted by on 2014-04-08 at 09:54