尝试使用urlmon.dll下载文件时,如何解决“运行时检查失败#0”错误?

时间:2019-02-10 10:14:52

标签: c++ visual-studio-2015 runtime-error urlmon

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

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

screenshot of dialog box

该如何解决此问题?

1 个答案:

答案 0 :(得分:2)

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

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

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

typedef HRESULT (__stdcall *tdDosyaIndir)(void*, char*, char*, DWORD, void*);