下载文件并将其设置为后台 VC++

问题描述

我正在尝试用 C++ 编写一个程序,该程序将从 Web 服务器下载图像并将其设置为用户的 Windows 桌面背景。这是我的代码

#include <iostream>
#include <Windows.h>
#include <string>
#pragma comment(lib,"urlmon.lib")

using namespace std;
int main()
{
    string dwnld_URL = "https://i.ibb.co/6wGttLF/Beautiful-Space-3-D-Live-Wallpaper.jpg";
    string savepath = "C:\\tmp\\star-bg.jpg";
    URLDownloadToFile(NULL,dwnld_URL.c_str(),savepath.c_str(),NULL);

    return 0;
}

我收到 3 条错误消息:

Error (active)  E0167   argument of type "const char *" is incompatible with parameter of type "LPCWSTR"    abcxyz  C:\Users\jprai\source\repos\abcxyz\abcxyz\abcxyz.cpp 11

Error (active)  E0167   argument of type "const char *" is incompatible with parameter of type "LPCWSTR"    abcxyz  C:\Users\jprai\source\repos\abcxyz\abcxyz\abcxyz.cpp 11

Error   C2664   'HRESULT URLDownloadToFileW(LPUNKNowN,LPCWSTR,DWORD,LPBINDSTATUSCALLBACK)': cannot convert argument 2 from 'const _Elem *' to 'LPCWSTR' abcxyz  C:\Users\jprai\source\repos\abcxyz\abcxyz\abcxyz.cpp    11  

我该怎么做才能解决这个问题?顺便说一下,我还没写代码把它设置为桌面壁纸。

解决方法

您需要将宽字符串传递给 <table> <thead> <tr> <th>One</th> <th>Two</th> </tr> </thead> <tbody> <tr> <td><div class="content"><div class="div1"></div></div></td> <td><div class="content"><div class="div2"></div></div></td> </tr> <tr> <td>lorem</td> <td>loremlorem</td> </tr> </tbody> </table>。因此更改此:

URLDownloadToFile

为此:

string dwnld_URL = "https://i.ibb.co/6wGttLF/Beautiful-Space-3-D-Live-Wallpaper.jpg";
string savepath = "C:\\tmp\\star-bg.jpg";

或者更好的是,直接传递(宽)字符串常量。