cURL / Mailing Lists / curl-library / Single Mail

curl-library

Using libcurl in win32 DLL in MVSC 2010

From: Bej Glee <bejglee_at_gmail.com>
Date: Mon, 23 Jul 2012 11:00:48 +0200

Hello everyone,

I want to create a win32 DLL what use libcurl. My goal is: read a HTML page
and get back as string.
An external program (not C++) call this DLL, and get the HTML page in
string.
I want to create ONE dll which contains the necessary resources (all dll).
The project build successful -> dll has been created, but when I call
getHTML() function I get error: "cannot load library 'example.dll' (error
126)".
Someone can tell you what to set in the project?

Here is my example.cpp:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <curl.h>
#include <string>
#include <windows.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define MY_EXPFUNC extern "C" __declspec(dllexport)
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows
headers

CWinApp theApp;

BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID
lpReserved){return TRUE;}

using namespace std;

static int writer(char *data, size_t size, size_t nmemb, string *buffer)
{
    int result=0;
    if (buffer!=NULL)
    { buffer->append(data,size*nmemb);
        result=size*nmemb;
    }
    return result;
}

static char* convert(const std::string &s)
{
    try
    { char *pc=new char[s.size()+1];
        std::strcpy(pc,s.c_str());
        return(pc);
    }
    catch(...){return(NULL);}
    return(NULL);
}

MY_EXPFUNC char* __stdcall getHTML(void)
{
    CURL *curl;
    string buffer;

    curl_global_init(CURL_GLOBAL_WIN32);
    curl = curl_easy_init();
    if (curl)
    { curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
        curl_easy_setopt(curl, CURLOPT_HEADER, 0);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer );

        curl_easy_perform(curl);

        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

    return(convert(buffer));
}

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-07-23