首页 > 解决方案 > 我使用的数据类型有问题吗?(const char* 的参数与 LPCTSTR 类型的参数不兼容)

问题描述

我正在尝试使用类项目的 urlmon 库从给定网页中提取链接。我附上了一张图片来显示错误。错误说:

“const char*”类型的参数与“LPCTSTR”类型的参数不兼容

关于我做错了什么的任何线索?

使用 Visual Studio 2019

我是一个相对较新的程序员,所以请以简单/可理解的方式彻底解释。

#include <windows.h>
#include <fstream>
#include <iostream>
#include <set>
#include <regex>
using namespace std;

typedef HRESULT(WINAPI* UDTF)(LPVOID, LPCTSTR, LPCTSTR, DWORD, LPVOID);

bool getURLToFile(string url, string file)
{
    int r = 1;
    HMODULE hDll;
    UDTF URLDownloadToFile;

    if ((hDll = LoadLibrary(LPCWSTR("urlmon")))) // Loads the module (DLL) urlmon into this process
    {
        if ((URLDownloadToFile = (UDTF)GetProcAddress(hDll, "URLDownloadToFileA"))) // Retrieves the function URLDownloadToFileA from the urlmon DLL
        {
            if (URLDownloadToFile(NULL, url.c_str(), file.c_str(), 0, 0) == 0) // Actual download happens here
            {
                r = 0; // Success!
            }
        }
        FreeLibrary(hDll); // Unload the module
    }
    return !r; // return True if r = 0
}

string getStringFromFile(string file_name)
{
    ifstream file(file_name); // Creates the file stream
    return { istreambuf_iterator<char>(file), istreambuf_iterator<char>{} };
}

set<string> extractLinks(string file_name)
{
    static const regex href_regex("<a href=\"(.*?)\"", regex_constants::icase); // Creates the regex that parses <a> tags

    const string text = getStringFromFile(file_name); // Gets stored string

    return { sregex_token_iterator(text.begin(), text.end(), href_regex, 1), sregex_token_iterator{} }; // Returns the set of matched instances
}

int main(void)
{
    string url;
    string file = "sample.txt"; // File for temporary storage of web page
    cout << "Please enter a url address: ";
    cin >> url;

    if (getURLToFile(url, file)) // Try to get the url
    {
        cout << "The following links were found:" << endl;
        for (string ref : extractLinks(file)) // Print all the links in the set
        {
            cout << ref << endl;
        }
        cout << "Done!" << endl;
    }
    else
    {
        cout << "Could not fetch the url" << endl;
    }

}

图片

标签: htmlc++

解决方案


LPCTSTR是一个Long Pointer to an TCHAR。我敢打赌这已经TCHAR解决了,wchar_t所以你不能通过它,std::string::c_str()因为这是const char*.

更新:

如评论中所述,您必须将其更改UDTF为此,因为您正在寻找该URLDownloadToFileA功能:

typedef HRESULT(WINAPI* UDTF)(LPVOID, LPCSTR, LPCSTR, DWORD, LPVOID);

推荐阅读