开发者

no suitable conversion function from "std::string" to "PVOID" exists

I am trying to use the SetParametersInfo function to change wallpapers.I would like to pass the filepath for the wallpaper as a variable, but whenever I try to do this, I get the error

Error: no suitable conversion function from "std::string" to "PVOID" exists

Here is the code I have thus far

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#pragma comment(lib, "us开发者_开发百科er32.lib")
using namespace std;

#define _TEXT(x) L##x

void main(){
    string input ="";
    cout << "Enter the filepath\n";
    getline(cin, input);

    BOOL success = SystemParametersInfo(
    SPI_SETDESKWALLPAPER,   //iuAction
    0,                      //uiParam
    input,                  //pvParam
    SPIF_UPDATEINIFILE      //fWinIni
    );
    if (success){
        printf("Success!\n");
    }else
        printf("Failure =(\n");
}

Do you guys have any suggestions for what I can do? I have looked all over to find a solution and haven't been able to find one. Perhaps I am not searching for the right terms.

Extra info: I am running Windows 7 and using Visual Studio 2010 Ultimate.

Edit: I finally got it to work. I had to change the "Character Set" setting to "Not Set" then it worked fine. Here is the updated code:

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#pragma comment(lib, "user32.lib")
using namespace std;

void main(){
    string input ="";
    cout << "Enter the filepath\n";
    getline(cin, input);

    BOOL success = SystemParametersInfo(
    SPI_SETDESKWALLPAPER,   //iuAction
    0,                      //uiParam
    (PVOID) input.c_str(),  //pvParam
    SPIF_UPDATEINIFILE      //fWinIni
    );
    if (success){
        printf("Success!\n");
    }else{
        printf("Failure =(\n ");
        cout << input << "\n";
        cout << (PVOID) input.c_str()<< "\n";
    }
}


input.c_str() will return a const char * that will implicitly convert to PVOID.

Pass input.c_str() to SystemParametersInfo and it should work.

*Make sure this is not compiled with UNICODE=1 because then SystemParametersInfo is redirected to SystemParametersInfoW which would expect a std::wstring, not a std::string.

If you really want to force an ascii string even when compiling UNICODE then call SystemParametersInfoA explicitly.


BOOL success = SystemParametersInfo(
SPI_SETDESKWALLPAPER,   //iuAction
0,                      //uiParam
(PVOID)input.c_str(),   //pvParam
SPIF_UPDATEINIFILE      //fWinIni
);


string::c_str() will return a const char* pointer, then you may need to do a pointer type cast to make the SystemParametersInfo works.

BOOL success = SystemParametersInfo(
SPI_SETDESKWALLPAPER,   //iuAction
0,                      //uiParam
(PVOID)input.c_str(),                  //pvParam
SPIF_UPDATEINIFILE      //fWinIni
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜