c++lpwstr

'Initializing': Cannot convert from 'const wchar_t[35]' to 'LPWSTR'


i am currently learning C++ and want to change my desktop wallpaper. However i am getting this error above.

#include <string>
#include <iostream>
#include <Windows.h>

using namespace std; 

int main() {

LPWSTR test = L"C:\\Users\\user\\Pictures\\minion.png";

int result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, 
test, SPIF_UPDATEINIFILE);


}

A value of type "Const wchar_t*" cannot be used to initialize an entity of type LPWSTR

Any ideas?

Thanks


Solution

  • LPWSTR is an alias for wchar_t*, ie a pointer to a non-const character.

    A string literal is a const array of characters, in your case a const wchar_t[35]. It decays into a pointer to a const character, pointing at the 1st character in the literal.

    You can't assign a pointer-to-const to a pointer-to-non-const. That would allow writing access to read-only memory.

    Use LPCWSTR instead, which is an alias for const wchar_t*.

    LPCWSTR test = L"C:\\Users\\user\\Pictures\\minion.png";