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

以下是源代码

#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)'
阅读 3k
1 个回答

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)'

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

推荐问题