html - 我使用的数据类型有问题吗?(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;
}
}
解决方案
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);
推荐阅读
- php - 如何显示响应 json
- java - 为什么恢复下载会在 Java 应用程序中下载损坏的文件?
- apache - 如何在 Springboot 2.1.1 Final 中配置 CXF servlet?
- python-3.x - 尽管 pip3 show 找到了 python 包,但无法导入它
- dart - 如何为 SliverAppBar 添加抽屉
- r - change specific component of list in R
- google-visualization - Google Visualization API 可点击图表项
- python-3.x - 用户发送命令后,我应该如何私信我的用户?(不和谐蟒蛇)
- c# - 如何从“开关”返回变量“字符串”?
- javascript - Node.js mysql 查询未定义