我尝试在visual studio c ++中使用libcurl和c ++ - cli编写应用程序代码。它给了我这个错误
error C3867: "fr::Form1::write_data": Dem Funktionsaufruf fehlt die Argumentliste. Verwenden Sie "&fr::Form1::write_data", um einen Zeiger auf den Member zu erstellen. c:\users\ttg\documents\visual studio 2008\projects\fr\fr\Form1.h
这意味着我应该将一些参数传递给write_data。我不知道如何传递这些数据,并且在libcurl的文档中,它显示的是我所拥有的完全相同的代码,我只是复制和粘贴它。我不知道我需要添加什么
此外,当我在Form I之外声明此write_data函数时,我没有收到此错误,但是我收到链接器错误,如:
Fehler 53 error LNK2031: p/invoke konnte nicht für ""extern "C" void __clrcall curl_easy_cleanup(void *)" (?curl_easy_cleanup@@$$J0YMXPAX@Z)" generiert werden. In den Metadaten fehlt die Aufrufkonvention. fr.obj
意思是,缺少元数据中的呼叫对话
这是我的代码:
#pragma once
#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
#include <assert.h>
#include <string>
#include <curl/curl.h>
#include <sstream>
#include <curl/easy.h>
#include <iostream>
using namespace std;
namespace fr {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Zusammenfassung für Form1
///
/// Warnung: Wenn Sie den Namen dieser Klasse ändern, müssen Sie auch
/// die Ressourcendateiname-Eigenschaft für das Tool zur Kompilierung verwalteter Ressourcen ändern,
/// das allen RESX-Dateien zugewiesen ist, von denen diese Klasse abhängt.
/// Anderenfalls können die Designer nicht korrekt mit den lokalisierten Ressourcen
/// arbeiten, die diesem Formular zugewiesen sind.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Konstruktorcode hier hinzufügen.
//
}
protected:
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
protected:
private:
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
public:
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
// assert(bodyfile == (FILE*) stream); //this assertion fails, but when i comment it, code works. Why?
int written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
void proc(){
CURL *curl_handle;
const char *headerfilename = "head.out";
FILE *headerfile;
const char *bodyfilename = "body.html";
FILE *bodyfile;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://url");
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/*this causes error*/curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
headerfile = fopen(headerfilename,"w");
if (headerfile == NULL) {
curl_easy_cleanup(curl_handle);
return;
}
bodyfile = fopen(bodyfilename,"w");
if (bodyfile == NULL) {
curl_easy_cleanup(curl_handle);
return;
}
curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, headerfile);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
curl_easy_perform(curl_handle);
fclose(headerfile);
fclose(bodyfile);
curl_easy_cleanup(curl_handle);
}//()proc
};
}
答案 0 :(得分:0)
我认为write_data方法应该是静态的...或者像你一样在Form类之外......关于链接器错误:摘自msdn文章When attempting to import a native function into a pure image, remember that the implicit calling conventions differ between native and pure compilations.