Why does this keep failing?

S

Steve S

It always returns false...*pvParam never gets populated. Any ideas?



int temp;

char *pvParam = "";

try

{

if(SystemParametersInfo(SPI_GETDESKWALLPAPER,0,pvParam,0)==true)

{

cout << "True: vParam = " << pvParam;

}

else

{

cout << "False: vParam = " << pvParam;

}

}

catch(char * str)

{

cout << "EXCEPTION: " << str;

}

cout << "\n\nHit a Key to exit...";

cin >> temp;
 
A

Artie Gold

Steve said:
It always returns false...*pvParam never gets populated. Any ideas?

Well, let's see...

Oh wait, this is going to be harder than I thought, given the absurd
formatting of the code you've provided. Ugh!
int temp;

char *pvParam = "";

try

{

if(SystemParametersInfo(SPI_GETDESKWALLPAPER,0,pvParam,0)==true)

Hmmm. Passing a pointer to a string literal and expecting it to
*receive* information?

Make `pvParam' either an array of char or allocate space for it (in
either case long enough to contain whatever that long-named
non-standard function wants to write into it).
{

cout << "True: vParam = " << pvParam;

}

else

{

cout << "False: vParam = " << pvParam;

}

}

catch(char * str)

{

cout << "EXCEPTION: " << str;

}

cout << "\n\nHit a Key to exit...";

cin >> temp;

HTH,
--ag
 
M

Mike Wahler

Steve S said:
It always returns false...*pvParam never gets populated. Any ideas?

See below.
int temp;

char *pvParam = "";

'pvParam' points to one character (with value zero)
of storage which must not be modified. I.e. it
points to a (nonmodifiable) string literal, not
modifiable storage.


char pvParam[size required by 'SystemParametersInfo()'];

or

char *pvParam = new char[size required by 'SystemParametersInfo()'];

See the documentation for 'SystemParametersInfo()' for
what the size requirement is.

More below.
try

{

if(SystemParametersInfo(SPI_GETDESKWALLPAPER,0,pvParam,0)==true)

{

cout << "True: vParam = " << pvParam;

}

else

{

cout << "False: vParam = " << pvParam;

}

}

catch(char * str)

Are you sure that 'SystemParametersInfo()' throws this
type as an exception?
{

cout << "EXCEPTION: " << str;

}

cout << "\n\nHit a Key to exit...";

cin >> temp;

If you use the 'new[]' form above for reserving
your storage, don't forget to free it after you're
done with it:

delete[] pvParam;

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top