简体   繁体   中英

'SystemParametersInfo' function returns incorrect value when get the desktop wallpaper

I'm using the function SystemParametersInfo to get the desktop wallpaper and it's assumed that when adding the action SPI_GETDESKWALLPAPER to the function returns a string.

LPWSTR bgPath;
if(!SystemParametersInfo(SPI_GETDESKWALLPAPER, 0, bgPath, SPIF_UPDATEINIFILE)){
     qDebug() << *bgPath;
     return;
}
qDebug()<< "an error occurred";

The problem is that the function returns a numeric value (ex: 50121) instead of a string.
Is there any problem in my code?

You not allocating any memory for bgPath for SystemParametersInfo() to fill in.

Per the SPI_GETDESKTOPWALLPAPER documentation:

The pvParam parameter must point to a buffer to receive the null-terminated path string. Set the uiParam parameter to the size, in characters, of the pvParam buffer. The returned string will not exceed MAX_PATH characters.

Even if you were allocating a buffer, you are checking the return value of SystemParametersInfo() for failure instead of success. And you are dereferencing your string pointer, so at best you would output only the first character, not the whole string.

Use this instead:

WCHAR bgPath[MAX_PATH];

if (SystemParametersInfo(SPI_GETDESKWALLPAPER, MAX_PATH, bgPath, 0))
{
    qDebug() << bgPath;
    return;
}
qDebug() << "an error occurred";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM