Command-line app to set the desktop wallpaper

Working on Windows, I find myself installing Windows a lot.

I find that I like to change a lot of the settings that Windows offers to non-default values.  (That is, I’m picky.)

I have a script which automates some of these things, which I add to now and again.  Some of the bits of the script are straightforward, but once in a while the tweak itself is of interest.

One of the things I love about my work setup is the many large monitors.  So, one of the things I like to change is the desktop wallpaper image.

Changing the desktop wallpaper required some code, which makes it “of interest.”  Here’s the code.

// main.cpp

#include <windows.h>
#include <winuser.h>
#include <stdio.h>

int _cdecl wmain(int argc, LPCWSTR argv[]) {
    if (1 != argc - 1) {
        wprintf(L"expected a single argument, not %d\n", argc);
        return -__LINE__;
    }
    
    if (!SystemParametersInfo(
        SPI_SETDESKWALLPAPER,
        0,
        const_cast<LPWSTR>(argv[1]),
        SPIF_SENDCHANGE
    )) {
        DWORD dwErr = GetLastError();
        wprintf(L"SystemParametersInfo(...) failed with error %d\n", dwErr);
        return -__LINE__;
    }
    
    wprintf(L"Setting the desktop wallpaper to %s succeeded.\n", argv[1]);    
    return 0;
}

Warning: if you pass a relative path to this tool, it won’t qualify it for you, and the SystemParametersInfo call won’t either – so the wallpaper you want won’t be set, though all the calls will succeed.  Make sure to specify a fully-qualified path.

EDIT September 22 2015: removed source and binaries as this is obsoleted by https://matthewvaneerde.wordpress.com/2012/10/10/changing-the-desktop-wallpaper-using-idesktopwallpaper/

2 thoughts on “Command-line app to set the desktop wallpaper

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s