我已经使用 urlmon.dll 用C ++编写了一个下载程序。
我已经使用Visual Studio 2015 RTM作为IDE。
这是我的代码:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "clocale"
#include "fstream"
#include "iostream"
#include "string"
using namespace std;
typedef int*(*tdDosyaIndir)(void*, char*, char*, DWORD, void*);
int main()
{
setlocale(LC_ALL, "turkish");
string strAdres;
cout << "İndirilecek adresi girin:\n";
cin >> strAdres;
HINSTANCE dll = LoadLibrary(L"urlmon.dll");
tdDosyaIndir DosyaIndir = (tdDosyaIndir)GetProcAddress(dll, "URLDownloadToFileA");
DosyaIndir(0, &strAdres[0u], "dosya.html", 0, 0);
FreeLibrary(dll);
return 0;
}
但是问题是当我尝试下载某些程序时显示此错误:
该如何解决此问题?
答案 0 :(得分:2)
您需要在函数指针typedef中指定调用约定。
Windows API函数通常使用__stdcall
调用约定。但是,C和C ++函数通常使用__cdecl
调用约定,这是编译器的默认设置。当调用约定不匹配时,编译器将生成错误的代码,并且您会收到此错误消息。
为确保编译器生成正确的代码来调用该函数,您的typedef应该如下所示:
typedef HRESULT (__stdcall *tdDosyaIndir)(void*, char*, char*, DWORD, void*);