我正在尝试使用 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/