learnerX
learnerX

Reputation: 1082

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

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.

Upvotes: 3

Views: 6084

Answers (1)

darclander
darclander

Reputation: 1811

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 SystemParametersInfoW() should return true if it manages to change the background. I print it out as result for clarity when running the application.

Upvotes: 4

Related Questions