C++编译时出现字符串转换错误,求解决

发布于 2022-09-12 22:20:44 字数 950 浏览 33 评论 0

以下是源代码

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <urlmon.h>
#include <windows.h>
#include <bits/stdc++.h>

#pragma comment(lib, "urlmon.lib")

using namespace std;


bool DownloadFiles(const wchar_t  url[], const wchar_t downloadPath[])
{
    if (URLDownloadToFile(NULL, url, downloadPath, 0, NULL) == S_OK) return true;  //在第二个NULL处出错
    else return false;
}

int main(void)
{
    wchar_t url[] = L"https://music.163.com/song/media/outer/url?id=512377039.mp3";
    wchar_t to[] = L"D:\\";
    cout<<"..."<<endl;
    if (DownloadFiles( url , to)) printf("OK!\n");
    else printf("Error!\n");
    return 0;
}

出错信息:

cannot convert 'const wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'HRESULT URLDownloadToFileA(LPUNKNOWN, LPCSTR, LPCSTR, DWORD, LPBINDSTATUSCALLBACK)'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2022-09-19 22:20:44

URLDownloadToFile -> URLDownloadToFileW

cannot convert 'const wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'HRESULT
URLDownloadToFileA 这里由于宏的原因使用了 A 结尾的函数,你用了 wchar_t 所以要用 W 结尾的
(LPUNKNOWN, LPCSTR, LPCSTR, DWORD, LPBINDSTATUSCALLBACK)'

我猜的,没测试过,大概行吧。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文