-2

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?

7
  • 2
    It don't understand why you would write this code. It's as if you didn't read the documentation. It says the following: Retrieves the full path of the bitmap file for the desktop wallpaper. 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. If there is no desktop wallpaper, the returned string is empty. Commented Oct 7, 2017 at 15:56
  • 1
    So you got both parameters wrong, and also the file Al parameter is wrong. On top of everything else, you handle the return value incorrectly. Commented Oct 7, 2017 at 15:57
  • 1
    Yes, you are supposed to use a WCHAR[] so it can write the name of the file into the buffer. And pass the size of the array so it can't write too much. And fix the if() statement, you already noticed that it returned FALSE to indicate failure. And use GetLastError() to find out why it failed, always important to discover why winapi function calls don't work. Commented Oct 7, 2017 at 16:05
  • @DavidHeffernan: Thank you. I've read the documentation about that point as you exactly wrote, but I still don't understand because I don't use winapi too much, so, is it possible to give me an example of using SystemParametersInfo correctly? Commented Oct 7, 2017 at 16:07
  • Why don't you ask a colleague for help. Asking us to do your work for you ain't cool. Commented Oct 7, 2017 at 16:15

1 Answer 1

2

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";
Sign up to request clarification or add additional context in comments.

9 Comments

I thank you for your answer. unfortunately, the function returns an address (ex: 0x28c1c0).
@LionKing does qDebug() accept Unicode strings to begin with? Or only ANSI strings? If the latter, then convert bgPath to ANSI using WideCharToMultiByte(), or use SystemParametersInfoA() with a char[] buffer
@LionKing: Since there is no operator<< overload for QDebug taking a const wchar_t*, lookup will match the generic pointer overload instead, taking a const void*. This will output the pointer value. If you want to dump a wide character string, you're going to have to construct a QString temporary first: qDebug() << QString( bgPath );.
@IInspectable: Thanks. I've used a static member of QString called QString::fromWCharArray() to convert the string.
what size buffer is recommended? and why? i've seen someone use 200. i'm not sure if that's right. i know that path lengths can be 256 (or higher perhaps) (i found that out when a folders properties weren't showing the correct size of all the contents because it excluded files with long paths) and there's a way you can tell windows to use longer paths in the register.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.