c++ - 不存在从 "std::string"到 "PVOID"的合适转换函数

标签 c++ visual-studio-2010

我正在尝试使用 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() 将返回一个将隐式转换为 PVOID 的 const char *。

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

*确保这不是用 UNICODE=1 编译的,因为这样 SystemParametersInfo 被重定向到 SystemParametersInfoW,它需要一个 std::wstring,而不是 std::string。

如果您真的想在编译 UNICODE 时强制使用 ascii 字符串,则显式调用 SystemParametersInfoA。

关于c++ - 不存在从 "std::string"到 "PVOID"的合适转换函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4869754/

相关文章:

c# - 为什么我的 WPF 应用程序在显示 WinForms 对话框后不能正常关闭?

c++ - 使用批处理文件使用 Cmake 和 Msbuild (vs2010) 单击一次构建我的项目

visual-studio-2010 - Visual Studio 2010 无法识别正确的源代码管理插件

c++ - 引入std::enable_if后出现"No match"错误

c++ - INT_MIN 的用途

c++ - c++中char和signed char的区别?

c# - 扩展用户控件后,Visual Studio 在设计模式下被阻止

c++ - parallel_for ppl.h 不比顺序 C++ 快

c++ - 创建指针时没有调用构造函数吗?

visual-studio-2010 - 从 TFS 命令行创建新工作区 - 总是添加根工作目录?