1

As the title states, I'm trying to use SystemParametersInfo to set my wallpaper, but it's just setting my wallpaper to a black screen. Here is my code:

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

void setWall()
{
    LPWSTR path = L"D:\\Wallpaper.png";

    int result;
    result = SystemParametersInfo(
        SPI_SETDESKWALLPAPER,
        0,
        path,
        SPIF_UPDATEINFILE);

    std::cout<<result<<std::endl;

    if (result)
    {
        std::cout << "Wallpaper set";
    }
    else
    {
        std::cout << "Wallpaper not set";
        std::cout << "SPI returned" << result;
    }
}

int main()
{
    setWall();
    return 0;
}

When compiling the program it gives the following:

warning: ISO C++ forbids converting a string constant to 'LPWSTR {aka wchar_t*}' [-Wwrite-strings]

Thanks

Ken White
  • 123,280
  • 14
  • 225
  • 444
Logan Davenport
  • 191
  • 1
  • 2
  • 7
  • The compiler warning can be fixed by changing `LPWSTR` (non-const `wchar_t*`) to `LPCWSTR` (`const wchar_t*`). Assigning string literals to non-const pointers is deprecated in C++11 onwards. But that is not the reason why your wallpaper is turning black. Are you compiling your project for ANSI or Unicode? It makes a difference in what kind of strings `SystemParametersInfo()` expects. If you are compiling for ANSI, use `LPCSTR` instead and drop the `L` modifier. – Remy Lebeau Oct 28 '17 at 01:29
  • The call looks good. Are you sure you have a file called "D:\Wallpaper.png" – mnistic Oct 28 '17 at 01:32
  • 1
    Related: [SystemParametersInfo returns 0](https://stackoverflow.com/questions/14634908/) and [SystemParametersInfo sets wallpaper completly black (using SPI_SETDESKWALLPAPER)](https://stackoverflow.com/questions/34710677/) – Remy Lebeau Oct 28 '17 at 01:37
  • 3
    Have you defined `UNICODE`? Use `SystemParametersInfoW` to make sure it matches `wchar_t*` parameter. – Barmak Shemirani Oct 28 '17 at 01:42
  • @RemyLebeau I used your fix in the second link you sent and it worked, thank you very much for all the responses. : ) – Logan Davenport Oct 28 '17 at 01:51
  • 1
    @LoganDavenport: and which fix would that be exactly? Feel free to post an answer to your own question – Remy Lebeau Oct 28 '17 at 01:58

0 Answers0