繁体   English   中英

从“ std :: string”到“ PVOID”的转换功能不存在

[英]no suitable conversion function from “std::string” to “PVOID” exists

我正在尝试使用SetParametersInfo函数更改墙纸。我想将墙纸的文件路径作为变量传递,但是每当尝试这样做时,都会收到错误消息

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

这是我到目前为止的代码

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#pragma comment(lib, "user32.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");
}

你们对我的工作有什么建议吗? 我到处寻找解决方案,却找不到解决方案。 也许我不是在寻找正确的术语。

额外信息:我正在运行Windows 7并使用Visual Studio 2010 Ultimate。

编辑:我终于得到它的工作。 我必须将“字符集”设置更改为“未设置”,然后它才能正常工作。 这是更新的代码:

#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()将返回一个const char *,它将隐式转换为PVOID。

将input.c_str()传递给SystemParametersInfo,它应该可以工作。

*请确保未使用UNICODE = 1编译此文件,因为这样会将SystemParametersInfo重定向到SystemParametersInfoW,它将期望使用std :: wstring,而不是std :: string。

如果您确实要在编译UNICODE时强制使用ascii字符串,请显式调用SystemParametersInfoA。

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

string :: c_str()将返回const char *指针,然后您可能需要进行指针类型转换才能使SystemParametersInfo正常工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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