2011-07-31 199 views
0

我嘗試在Visual Studio C++中使用libcurl和C++ - cli編寫應用程序。它給了我這個錯誤libcurl編譯和鏈接器錯誤

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 

這意味着我要傳遞一些參數來寫入數據。我不知道如何傳遞這些數據,並且在libcurl的文檔中顯示了與我擁有的代碼完全相同的代碼,我只是複製並粘貼了這些代碼。我不知道我需要添加

此外,當我宣佈形式不在此寫入數據的功能我沒有得到這個錯誤,但我得到的鏈接錯誤,如:

Fehler 53 error LNK2031: p/invoke konnte nicht für ""extern "C" void __clrcall curl_easy_cleanup(void *)" ([email protected]@[email protected])" generiert werden. In den Metadaten fehlt die Aufrufkonvention. fr.obj 

意義,未接來電convetion在元數據

這是我的代碼:

#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

我覺得寫入數據的方法應該進行靜態...或外部Form類像你一樣......而關於鏈接錯誤:excer pt from msdn文章When attempting to import a native function into a pure image, remember that the implicit calling conventions differ between native and pure compilations.

+0

我通常用__cdecl和clr/pure調用約定時會遇到問題,而不僅僅是這個write_data函數,但正如我通過改變一些東西所看到的,我從libcurl庫中得到了與函數相同的錯誤,它是從c文件編譯的。你是否認爲我應該在Visual Studio中更改任何設置,以便編譯,因爲整個項目都是在dev C++中工作的,而我只需要添加一個GUI。 – TasostheGreat

+0

@Tasos Papanikolaou對不起,但從你的迴應我不明白,如果你已經添加/ clr:純在VisualStudio中編譯選項,那麼你在哪裏編譯你的項目,據我所知你也應該使用__cdecl然後你聲明本地函數例如extern 「C」int __cdecl func(); –