3

I want to make some changes on active desktop wallpaper like adding watermark.

For that I need to get the path of the active wallpaper. Adding the watermark I can do.

This can be done using JNA library, but I can't access the file path.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
pankaj_ar
  • 757
  • 2
  • 10
  • 33
  • Can you access under its registry `HKEY_CURRENT_USER\Control Panel\Desktop\ `: `Wallpaper`, `OriginalWallpaper` or `ConvertedWallpaper` entry? [See link](http://hdsurvivor.blogspot.com/2006/05/set-default-wallpaper-for-users-in.html) – ee. Dec 05 '11 at 07:23

3 Answers3

3

The way to obtain the current desktop wallpaper could different based on the operating system, for windows 7 it can be obtained from following registry path,

HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper

to read the registry path you can use the method described in following question

read/write to Windows Registry using Java

Community
  • 1
  • 1
Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
  • That was already mentioned on the comment above (except you added the SO link on how to read/write to Windows Registry). – Buhake Sindi Dec 05 '11 at 08:53
2

As Low Flying Pelican and ee said,

HKEY_CURRENT_USER\Control Panel\Desktop

contains the key Wallpaper which has a pointer to the wallpaper. For the command prompt, you can use

reg query "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper

to get the location, or using this for native java support:

Runtime.getRuntime().exec('reg query "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper');
Community
  • 1
  • 1
LemonJuiz
  • 21
  • 3
0

Using JNA library:

implementation("net.java.dev.jna:jna-platform:5.12.1")

I was able to get the current Wallpaper path like this (tested on Windows 11):

Java:

import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.WinReg.HKEYByReference;

public class Main {

    public static final String REGISTRY_PATH = "Control Panel\\Desktop";
    public static final String REGISTRY_Key = "WallPaper";

    public static void main(String[] args) {
        var hKey = new HKEYByReference();
        var registryAccessResult = openRegistryKey(hKey);
        validate(registryAccessResult);
        var wallpaperPath = getWallpaperPath();
        System.out.println(wallpaperPath);
    }

    public static int openRegistryKey(HKEYByReference hKey) {
        return Advapi32.INSTANCE.RegOpenKeyEx(
                WinReg.HKEY_CURRENT_USER,
                REGISTRY_PATH,
                0,
                WinNT.KEY_READ,
                hKey
        );
    }

    public static void validate(int registryAccessResult) {
        if (registryAccessResult != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(registryAccessResult);
        }
    }

    public static String getWallpaperPath() {
        return Advapi32Util.registryGetStringValue(
                WinReg.HKEY_CURRENT_USER,
                REGISTRY_PATH,
                REGISTRY_Key
        );
    }
}

Kotlin:

const val REGISTRY_PATH = "Control Panel\\Desktop"
const val REGISTRY_Key = "WallPaper"

fun main() {
    val hKey = HKEYByReference()
    openRegistryKey(hKey).validate()
    val wallpaperPath = getWallpaperPath()
    println(wallpaperPath)
}

fun openRegistryKey(hKey: HKEYByReference) =
    Advapi32.INSTANCE.RegOpenKeyEx(
        WinReg.HKEY_CURRENT_USER,
        REGISTRY_PATH,
        0,
        WinNT.KEY_READ,
        hKey
    )

fun RegistryAccessResult.validate() =
    takeIf { it == W32Errors.ERROR_SUCCESS }
        ?: throw Win32Exception(this)

fun getWallpaperPath(): String =
    Advapi32Util.registryGetStringValue(
        WinReg.HKEY_CURRENT_USER,
        REGISTRY_PATH,
        REGISTRY_Key
    )

You can also listen for wallpaper changes and get its new path:

waitForWallpaperChange(hKey).validate() // Waits until wallpaper is changed
val newWallpaper = getWallpaperPath()

fun waitForWallpaperChange(hKey: HKEYByReference) =
    Advapi32.INSTANCE.RegNotifyChangeKeyValue(
        hKey.value,
        false,
        WinNT.REG_NOTIFY_CHANGE_LAST_SET,
        null,
        false
    )
Mahozad
  • 18,032
  • 13
  • 118
  • 133