-2

I've read other questions regarding this same issue on StackOverflow, but none of them worked for me. I'm using jna 5.8.0, but every time I try to make this work the wallpaper is set to Black. I get no error messages whatsoever

public class Main {
    public static void main(String []args) {
        windowsChange("INSERT IMAGE PATH HERE");
    }

    interface User32 extends Library {
        User32 INSTANCE = (User32) Native.load("user32",User32.class,W32APIOptions.DEFAULT_OPTIONS);
        boolean SystemParametersInfo (int one, int two, String s ,int three);
    }
    static void windowsChange(String path) {
        User32.INSTANCE.SystemParametersInfo(0x0014, 0, path , 1);
    }
}

I have no experience at all with jna, nor interfacing with the OS. Is there a solution to this?

Mamiglia
  • 108
  • 7
  • 2
    Does this answer your question? [Can I change my Windows desktop wallpaper programmatically in Java/Groovy?](https://stackoverflow.com/questions/4750372/can-i-change-my-windows-desktop-wallpaper-programmatically-in-java-groovy) – DuncG Apr 07 '21 at 08:49

1 Answers1

2

You've copied an answer from this question. Windows sets background as black screen if the path is rubbish, so you should test the path you supply exists using Files.isRegularFile(Path.of(xyz)) or new File(xyz).isFile() before calling SystemParametersInfoA.

private static final int SPI_SETDESKWALLPAPER  = 0x0014;
private static final int SPIF_UPDATEINIFILE    = 0x01;
private static final int SPIF_SENDCHANGE       = 0x02;
User32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
DuncG
  • 12,137
  • 2
  • 21
  • 33
  • Yes, that's where I got the code. I've run a few tests and it appears to be working in both cases, even if the SystemParametersInfoA is set to 1. As you suggested my error was caused by the path not being recognized by Windows ('/' instead of '\\'). Thanks! – Mamiglia Apr 07 '21 at 10:56
  • 1
    It looks like the 3 is needed for other SystemParametersInfoA calls, will modify the answer – DuncG Apr 07 '21 at 14:05