* * *

Author Topic: Does anyone know how to change the lock screen via pascal?  (Read 1016 times)

vonskie

  • Full Member
  • ***
  • Posts: 127
Does anyone know how to change the lock screen via pascal?
« on: August 21, 2017, 03:59:17 pm »
I have a program that changes the wallpaper at set intervals I would also like to change the lock screen

Thaddy

  • Hero Member
  • *****
  • Posts: 4622
Re: Does anyone know how to change the lock screen via pascal?
« Reply #1 on: August 21, 2017, 04:02:18 pm »
I have a program that changes the wallpaper at set intervals I would also like to change the lock screen
You probably need a service to do that. Code will be similar.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

vonskie

  • Full Member
  • ***
  • Posts: 127
Re: Does anyone know how to change the lock screen via pascal?
« Reply #2 on: August 21, 2017, 04:21:00 pm »
I can not find an example of the code to change the lock screen via pascal anywhere


RAW

  • Hero Member
  • *****
  • Posts: 562
Re: Does anyone know how to change the lock screen via pascal?
« Reply #3 on: August 21, 2017, 04:29:43 pm »
Quote
I can not find an example of the code to change the lock screen via pascal anywhere
This is your chance !!!

BE THE FIRST ONE ...  :D
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.0RC5 FPC 3.0.4

rvk

  • Hero Member
  • *****
  • Posts: 2943
Re: Does anyone know how to change the lock screen via pascal?
« Reply #4 on: August 21, 2017, 04:45:41 pm »
I have a program that changes the wallpaper at set intervals I would also like to change the lock screen
You probably need a service to do that. Code will be similar.
A service ??????? (not sure what a service has got to do with this)

@vonskie, you've already asked that question:
https://forum.lazarus.freepascal.org/index.php?topic=35921.0

I don't think it can be done with the windows api. I think you'll need the .NET framework to set the Windows.System.UserProfile.LockScreen.SetImageFileAsync() like you mentioned in that thread. SystemParametersInfo(SPI_SETDESKWALLPAPER... (what you're probably using now) only works for the desktop image.

You could try tinkering with the registry.
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Personalization\LockScreenImage
Haven't tried it myself (I also think this will only change the default/new user lockscreen, not for existing users).

(note that this could be different per Windows version)

Thaddy

  • Hero Member
  • *****
  • Posts: 4622
Re: Does anyone know how to change the lock screen via pascal?
« Reply #5 on: August 21, 2017, 04:51:55 pm »
It can be done with the winapi and it will probably  work best as a service. (because of multiple users on same pc) I found some c++ examples
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

rvk

  • Hero Member
  • *****
  • Posts: 2943
Re: Does anyone know how to change the lock screen via pascal?
« Reply #6 on: August 21, 2017, 04:53:34 pm »
It can be done with the winapi and it will probably  work best as a service. (because of multiple users on same pc) I found some c++ examples
Did you find c++ examples for winapi win32? Or for .NET ??

(I think the Windows.System.UserProfile.LockScreen.SetImageFileAsync() are .NET examples)

vonskie

  • Full Member
  • ***
  • Posts: 127
Re: Does anyone know how to change the lock screen via pascal?
« Reply #7 on: September 11, 2017, 06:32:20 pm »
Here is what I came up with by researching online, the problem is I get an access denied when trying to access the openkey. So I am stuck...

Any other options?


procedure TMyApplication.setlockscreen(slockscreenPath: string; version:integer);
  var
    reg: TRegistry;
  begin
    try
      try
        reg := TRegistry.Create;
        reg.RootKey := hkey_local_machine;
         if reg.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background', true) then
        begin
          reg.WriteInteger('OEMBackground', 1);
        end
        else writeln(reg.LastErrorMsg);

        if not (directoryexists(SystemFolder+'\oobe\info')) then createdir(SystemFolder+'\oobe\info');
        if not (directoryexists(SystemFolder+'\oobe\info\backgrounds')) then createdir(SystemFolder+'\oobe\info\backgrounds');
        if (fileexists(SystemFolder+'\oobe\info\backgrounds\backgroundDefault.jpg')) then deletefile(SystemFolder+'\oobe\info\backgrounds\backgroundDefault.jpg');
        copyfile(slockscreenPath, SystemFolder+'\oobe\info\backgrounds\backgroundDefault.jpg');

        SysUtils.ExecuteProcess(UTF8ToSys('rundll32'),(UTF8ToSys('"USER32.DLL,UpdatePerUserSystemParameters, 1, True"')), []);

      finally
        reg.Free;
      end;
    except
      on E: Exception do
        DumpExceptionCallStack(E, 'log');

    end;

  end;           

rvk

  • Hero Member
  • *****
  • Posts: 2943
Re: Does anyone know how to change the lock screen via pascal?
« Reply #8 on: September 12, 2017, 10:13:35 am »
Here is what I came up with by researching online, the problem is I get an access denied when trying to access the openkey. So I am stuck...
First... you are trying to write to hkey_local_machine. Are you running this program "as administrator" ??
As normal user you don't have permission to write to hkey_local_machine.

Second... you are trying to write a file to the systemfolder. Again, that is not allowed as normal user and requires elevation to administrator status.

Third... are you sure the registrykey \oobe\info\backgrounds\backgroundDefault.jpg is for the "Lock screen". You state you wanted to change the lock screen. I think the registrykey you are referring to is for the Logon screen. Those are two different things.

You can try running your program "as administrator" and see if that's what you want.

Thaddy

  • Hero Member
  • *****
  • Posts: 4622
Re: Does anyone know how to change the lock screen via pascal?
« Reply #9 on: September 12, 2017, 11:16:36 am »
Here is what I came up with by researching online, the problem is I get an access denied when trying to access the openkey. So I am stuck...
First... you are trying to write to hkey_local_machine. Are you running this program "as administrator" ??
As normal user you don't have permission to write to hkey_local_machine.

Second... you are trying to write a file to the systemfolder. Again, that is not allowed as normal user and requires elevation to administrator status.

Third... are you sure the registrykey \oobe\info\backgrounds\backgroundDefault.jpg is for the "Lock screen". You state you wanted to change the lock screen. I think the registrykey you are referring to is for the Logon screen. Those are two different things.

You can try running your program "as administrator" and see if that's what you want.
That's why I wrote: a service.... And finally you came to the same conclusion.... >:D >:( >:( Big case of not listening.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

rvk

  • Hero Member
  • *****
  • Posts: 2943
Re: Does anyone know how to change the lock screen via pascal?
« Reply #10 on: September 12, 2017, 11:24:44 am »
That's why I wrote: a service.... And finally you came to the same conclusion.... >:D >:( >:( Big case of not listening.
Thaddy, you MISUNDERSTAND  >:D

For setting the global logon background you need administrator permission.

For setting the personal user lock screen background you DON'T need administrator permission.

So, it's just a question what the user wants and in the opening post and title the LOCK SCREEN background change was asked for.

But you also still haven't showed code how to change the LOCK SCREEN background for the user  >:D
Just repeating you need a service doesn't help vonskie if (s)he doesn't know what code to use.

vonskie

  • Full Member
  • ***
  • Posts: 127
Re: Does anyone know how to change the lock screen via pascal?
« Reply #11 on: September 13, 2017, 10:05:54 pm »
Here is what I came up with that works

You will need elevated privileges to run it.

procedure TForm1.setlockscreen(slockscreenPath: string);
var
  reg: TRegistry;

begin

  try
    if (slockscreenPath = 'clear') then
    begin

      try
        reg := TRegistry.Create(KEY_WRITE or KEY_WOW64_64KEY);
        reg.Lazywrite := False;
        reg.RootKey := hkey_local_machine;
        if reg.OpenKey(
          'SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background', True) then
        begin
          reg.Writeinteger('OEMBackground', StrToInt('$00000000'));
          SysUtils.ExecuteProcess(UTF8ToSys('rundll32'),
            (UTF8ToSys('"USER32.DLL,UpdatePerUserSystemParameters, 1, True"')), []);
          exit;
        end;
      finally
        reg.Free;
      end;
    end;

    try

      reg := TRegistry.Create(KEY_WRITE or KEY_WOW64_64KEY);
      reg.Lazywrite := False;
      reg.RootKey := hkey_local_machine;
      if reg.OpenKey(
        'SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background', True) then
      begin
        reg.Writeinteger('OEMBackground', StrToInt('$00000001'));

      end;


      if not (directoryexists(windowsfolder + '\SysNative\oobe\info\backgrounds')) then
        createdir(windowsfolder + '\SysNative\oobe\info\backgrounds');
      if (fileexists(windowsfolder +
        '\SysNative\oobe\info\backgrounds\backgroundDefault.jpg')) then
      begin
        deletefile(windowsfolder +
          '\SysNative\oobe\info\backgrounds\backgroundDefault.jpg');
      end;

      copyfile(slockscreenPath,
        windowsfolder + '\SysNative\oobe\info\backgrounds\backgroundDefault.jpg');

      SysUtils.ExecuteProcess(UTF8ToSys('rundll32'),
        (UTF8ToSys('"USER32.DLL,UpdatePerUserSystemParameters, 1, True"')), []);

    finally
      reg.Free;
    end;
  except
    on E: Exception do
      DumpExceptionCallStack(E);

  end;

end;

ASerge

  • Sr. Member
  • ****
  • Posts: 452
Re: Does anyone know how to change the lock screen via pascal?
« Reply #12 on: September 15, 2017, 07:27:59 pm »
Here is what I came up with that works
1. As indicated by @rvk, this is for the logon screen, not for the lock screen.
2. This only works in Windows 7. In Windows 8 and Windows 10, there are other ways. And, if I remember correctly, they also differ from Windows 8 and Windows 8.1.
3. Key SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication is shared (not redirected) - no need KEY_WOW64_64KEY.
4. Instead of calling UpdatePerUserSystemParameters by running an external program, use the direct call to SystemParametersInfo (or BroadcastSystemMessage with WM_SETTINGCHANGE). In my tests it is not necessary at all, the changes are immediately visible.
5. And again put the cart before the horse. Read carefully the documents about the try finally. I already posted several times about this on the forum.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus