-2

我使用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;
}

但问题是当我尝试下载某些程序时显示此错误:

对话框截图

我应该怎么做才能解决这个问题?

4

1 回答 1

2

您需要在函数指针 typedef 中指定调用约定。

Windows API 函数通常使用__stdcall调用约定。但是,C 和 C++ 函数通常使用__cdecl调用约定,这是编译器的默认设置。当调用约定不匹配时,编译器会生成错误代码,并且您会收到此错误消息。

为确保编译器生成正确的代码来调用函数,您的 typedef 应如下所示:

typedef HRESULT (__stdcall *tdDosyaIndir)(void*, char*, char*, DWORD, void*);
于 2019-02-10T10:21:51.690 回答