cURL / Mailing Lists / curl-library / Single Mail

curl-library

Unable to get content when using libcurl as staticlibrary in dll

From: Saravana kumar <saravanakumar.a.o_at_gmail.com>
Date: Fri, 17 Aug 2012 23:42:50 +0530

Dear Sirs,
I am using following code in my dll and builded with curl static
library. But the exported function returns CURLCode 23, which was
write function error! Am i missing anything?

#include <windows.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string.h>
#include <stdlib.h>

# define DLLIMPORT __declspec(dllexport)

/* to keep the response */
char *response = NULL;

//prototypes
DLLIMPORT int processRequest(char *URL, int isPost, char *postFields,
char *cookie, int needHeader, int needBody, int followLocation);
DLLIMPORT char *getResponse(void);
DLLIMPORT const char *getError(int errCode);

size_t static write_callback_func(void *buffer, size_t size, size_t
nmemb, void *userp);
char *strndup (char const *s, size_t n);
static size_t strnlen(const char *s, size_t max);

DLLIMPORT int processRequest(char *URL, int isPost, char *postFields,
char *cookie, int needHeader, int needBody, int followLocation)
{
    //keeps the handle to the curl object
    CURL *curl_handle = NULL;
    int result;

    //make response null first
    response = NULL;

    //initializing curl and setting the url
    curl_handle = curl_easy_init();

    curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.google.co.in");

    curl_easy_setopt(curl_handle, CURLOPT_URL, URL);

    if(isPost == 1) {
        curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, postFields);
    }

    //follow locations specified by the response header
    curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, followLocation);

    //include header in response
    curl_easy_setopt(curl_handle, CURLOPT_HEADER, needHeader);

    //include body in response
    if(needBody == 0)
        curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);

    //place cookie also
    if(strlen(cookie) > 3)
        curl_easy_setopt(curl_handle, CURLOPT_COOKIE, cookie);

    //setting a callback function to return the data
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback_func);

    //passing the pointer to the response as the callback parameter
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &response);

    //perform the request
    result = curl_easy_perform(curl_handle);

    //cleaning all curl stuff
    curl_easy_cleanup(curl_handle);

    return result;
}

DLLIMPORT char *getResponse(void)
{
    return response;
}

DLLIMPORT const char *getError(int errCode)
{
    return curl_easy_strerror(errCode);
}

/* the function to invoke as the data recieved */
size_t static write_callback_func(void *buffer, size_t size, size_t
nmemb, void *userp)
{
    char **response_ptr = (char**)userp;

    /* assuming the response is a string */
    *response_ptr = strndup(buffer, (size_t)(size *nmemb));
}

char *strndup (char const *s, size_t n)
{
  size_t len = strnlen (s, n);
  char *new = malloc (len + 1);

  if (new == NULL)
    return NULL;

  new[len] = '\0';
  return memcpy (new, s, len);
}

static size_t strnlen(const char *s, size_t max) {
    register const char *p;
    for(p = s; *p && max--; ++p);
    return(p - s);
}

//Default dll functions
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
                       DWORD reason /* Reason this function is
being called. */ ,
                       LPVOID reserved /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}

PS: I used Dev C++(MingW) compiler environment to build this DLL.

Regards,
Saravana Kumar.A
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-08-17