How to change the windows 10 wallpaper with C++?

Viewed 3893

I am looking to change the Windows desktop background wallpaper in C++ using the Windows API.

I have read the following posts on this topic:

Problem:

When I execute the code, the desktop background changes to completely black like in the post above (yes, I did try the suggested fix in that post. No luck.)

Code:

#include <windows.h>

int main() {
    std::string s = "C:\\picture.jpg";
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID*)s.c_str(), SPIF_SENDCHANGE);
    return 0;
}

I have also tried just (void*) instead of (PVOID*) above and an L in front of the string. Nothing works so far.

SOLVED:

Changing SystemParametersInfo to SystemParametersInfoA (as suggested in the comment and answer) did the trick.

1 Answers

I believe you should use a wchar_t as input for SystemParametersInfo() instead of a string and also use SystemParametersInfoW().

The following code worked for me:

#include <windows.h>
#include <iostream>


int main() {
    const wchar_t *path = L"C:\\image.png";
    int result;
    result = SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (void *)path, SPIF_UPDATEINIFILE);
    std::cout << result;        
    return 0;
}

Where result should return true if it manages to change the background.

Related