I'm trying to learn how to use LoadLibrary properly in a C function, but am having difficulty and there are not a lot of good tutorials to follow. I've created a simple C program that uses the libCurl library to successfully fetch the HTML of a website and print it to console. I am now trying to re-implement the same function using LoadLibrary and GetProcAddress and the libcurl.dll.
How do I pass data back from a function that is loaded into memory?
Posted below is the function using the .lib that works and subsequently the function trying to use the DLL that is failing to compile.
Here is my working program:
#include "stdafx.h"
#include "TestWebService.h"
#include "curl/curl.h"
int main(int argc, char **argv)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
struct string s;
init_string(&s);
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
/* always cleanup */
printf("%s\n", s.ptr);
free(s.ptr);
curl_easy_cleanup(curl);
}
return 0;
}
Here is my attempt to replicate the same functionality using LoadLibrary only (i.e. Not using the libCurl.lib). But I get the following error message and cannot determine why.
1) a value of type "CURL" cannot be assigned to an entity of type "CURL *"
2) '=': cannot convert from 'CURL' to 'CURL *'
#include "stdafx.h"
#include "TestWebService.h"
#include "curl/curl.h"
typedef CURL (*CurlInitFunc)();
int main(int argc, char **argv)
{
HINSTANCE hLib = NULL;
hLib = LoadLibrary("libcurl.dll");
if (hLib != NULL)
{
CURL *curl;
CurlInitFunc _CurlInitFunc;
_CurlInitFunc = (CurlInitFunc)GetProcAddress(hLib, "curl_easy_init");
if (_CurlInitFunc)
{
curl = _CurlInitFunc();
}
}
return 0;
}
This line:
typedef CURL (*CurlInitFunc)();
declares a pointer to a function that returns a CURL. But the prototype of curl_easy_init() is:
CURL *curl_easy_init();
that means it returns a pointer to CURL, that is CURL*
Therefore the correct declaration is:
typedef CURL *(*CurlInitFunc)();
Related
I am trying to test libcurl for my project, but when I want to download a test file I get error:
ERROR : Unknown error
but no reason why it happens,
my code:
#include <stdio.h>
#include <curl/curl.h>
int main(int argc, char **argv) {
CURL *curl;
FILE *fp;
char name[30] = {"Test"};
char link[100] = {"ipv4.download.thinkbroadband.com/5MB.zip"};
CURLcode error;
int result;
fp = fopen(name,"Wb");
curl = curl_easy_init();
curl_easy_setopt;(curl, CURLOPT_URL, argv[1] );
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_perform(curl);
result = curl_easy_perform(curl);
if (result = CURLE_OK)
printf("Sucessful download !");
else
printf("Could not download, ERROR : %s \n",curl_easy_strerror(error));
printf("%s",error);
fclose(fp);
curl_easy_cleanup(curl);
}
There's a bunch of errors in the code.
The argument to fopen should be a lower case "w".
You declare both error and result but should use only one.
There's a stray semicolon in the middle of this line: curl_easy_setopt;(curl, CURLOPT_URL, argv[1] );
Combined with what's already mentioned, this should work:
#include <stdio.h>
#include <curl/curl.h>
int main(int argc, char **argv) {
if(argc < 2) {
puts("URL not given");
return 1;
}
CURL *curl;
FILE *fp;
char name[30] = {"Test"};
char link[100] = {"ipv4.download.thinkbroadband.com/5MB.zip"};
CURLcode result;
fp = fopen(name,"w");
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, argv[1] );
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
result = curl_easy_perform(curl);
if (result == CURLE_OK)
printf("Sucessful download !");
else
printf("Could not download, ERROR : %s \n",curl_easy_strerror(result));
fclose(fp);
curl_easy_cleanup(curl);
return 0;
}
Don't forget to pass the URL as an argument, since you're not actually using link and don't check argc. And I advise you to learn how to use a debugger.
Complete guess - but could it have something to do with your code calling curl_easy_perform(curl); two times instead of once?
This looks suspicious:
curl_easy_perform(curl);
result = curl_easy_perform(curl);
Shouldn't it just be:
result = curl_easy_perform(curl);
Also, shouldn't link have the http prefix on it?
char link[100] = {"http://ipv4.download.thinkbroadband.com/5MB.zip"};
Regarding to your code indentation there are braces missing at least in your else path. That means that the last printf is being executed regardless of the result value...
Does it work as expected if you add the braces like this? Of course together with the other suggestions as stated by selbie and Emanuel P...
if (result == CURLE_OK) {
printf("Sucessful download !");
} else {
printf("Could not download, ERROR : %s \n",curl_easy_strerror(error));
printf("%s",error);
}
I am downloading file quite commonly with curl. However, the server does a tricky thing: it return non-200 code and still sends some data. The problem is that I have the HTTP code after the data are written, but I do not want to write anything if it is non-200. Does anyone know a way to do that without storing data on disk or memory?
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteHandler);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, ptr);
res = curl_easy_perform(curl);
if (res == CURLE_OK) {
return 0;
}
long response_code;
curl_easy_getinfo(curl_.get(), CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200) {
return 0;
}
size_t curlWriteHandler(char* chars, size_t size, size_t nmemb, void* userp) {
// write to file
return size * nmemb;
}
Setting CURLOPT_FAILONERROR should do it for 4xx and 5xx errors.
When this option is used and an error is detected, it will cause the connection to get closed and CURLE_HTTP_RETURNED_ERROR is returned.
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
Closing connection is not good for me, it is important to reuse one. Can you think about anything else?
Unfortunately I can't find a way to make CURLOPT_FAILONERROR not close the connection.
The other option is to make the write function aware of the response. Unfortunately the curl handle is not passed into the callback.
We could make the curl variable global. Or we can take advantage of the void *userdata option to the write callback and pass in a struct containing both the curl handle and the buffer.
Here's a rough sketch demonstrating how the write callback can get access to the response code and also save the response body.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
typedef struct {
CURL *curl;
char *buf;
} curl_write_data;
size_t curlWriteHandler(char* chars, size_t size, size_t nmemb, void* userp) {
curl_write_data *curl_data = (curl_write_data*)userp;
long response_code;
curl_easy_getinfo(curl_data->curl, CURLINFO_RESPONSE_CODE, &response_code);
printf("Response: %ld\n", response_code);
// Now we can save if we like.
if( response_code < 300 ) {
curl_data->buf = malloc(size*(nmemb+1));
strcpy(curl_data->buf, chars);
strcat(curl_data->buf, "\0");
return size * nmemb;
}
else {
return 0;
}
}
int main() {
CURL *curl = curl_easy_init();
if(!curl) {
perror("Cant' init curl");
}
curl_write_data curl_data = { .curl = curl, .buf = NULL };
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/alsdfjalj");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteHandler);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &curl_data);
curl_easy_perform(curl);
if( curl_data.buf ) {
puts(curl_data.buf);
}
}
I'm not sure if this is the best idea, its what I came up with.
I am using this code to retrieve the data from the web browser using curl request in c language. I want to store the output in another file or buffer.
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
The output of this code is an html file. I want to store that html in another file or buffer. How to do that.
Thank you in advance.
Here's a modification to your code that writes the HTML response to a file:
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* create an output file and prepare to write the response */
FILE *output_file = fopen("output_file.html", "w");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, output_file);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %sn",
curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
Here are some related questions:
Saving a file using libcurl in C
Download file using libcurl in C/C++
I have the following curl code, which make a request to website and retrieve data from it, it works well, but I want to store my data in a string and not in the output window. Any idea?
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://api.hostip.info/get_html.php?ip=xxx.xxx.xx.xxx");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return 0;
}
int http_get_response(void *buffer, size_t size, size_t rxed, char **msg_in)
{
char *c;
if (asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer) == -1) {
free(*msg_in);
msg_in = NULL;
return -1;
}
free(*msg_in);
*msg_in = c;
return size * rxed;
}
and add the following curl option in your main
char *msg_in = calloc(1,sizeof(char));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &msg_in);
Then you will get the message in the msg_in
EDIT
do not forget to free msg_in when it become uselless in your program
free(msg_in); msg_in = NULL;
A a general (non-curl specific) method, change your standard output (path 1) (or standard error: path 2) path(s) prior to calling curl. Read the man page on dup2 to see how to duplicate a path to a specific descriptor, and the fdopen function to get a FILE * out of it.
The idea is you first dup path 1 for stdout, and/or 2 for stderr, to save copies of them somewhere. You then close the original paths. You create a pipe (man pipe) and then dup2 the second channel of the pipe to path 1 (or 2). You can now read() from the first channel of the pipe to get the output that was placed there.
Try this:
curl_easy_setopt(curl, CURLOPT_RETURNTRANSFER, true);
I'm trying to figure it out hoy to send json data to a web service.
json.phph y just a php who prints all the $_REQUEST send to the script. but in hehe end i receive nothing,, i tried sending the form in urlenc format and just normal data D:
im running out of ideas how this can be done.
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Content-Type: application/json";
curl_global_init(CURL_GLOBAL_ALL);
/*
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "json",
CURLFORM_COPYCONTENTS, "reactantsJSON={\"O=O\":{\"N\":1}}&productsJSON= [\"O=O\",\"[O]\"]&temperature=2273.15&pressure=101.325",
CURLFORM_END);
*/
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
* wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1/json.php");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl,CURLOPT_POST,1);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS, "reactantsJSON%3d%7b%22O%3dO%22%3a%7b%22N%22%3a1%7d%7d%26productsJSON%3d%5b%22O%3dO%22%2c%22%5bO%5d%22%5d%26temperature%3d2273.15%26pressure%3d101.325" );
// curl_easy_setopt(curl,CURLOPT_POSTFIELDS, "reactantsJSON={\"O=O\": {\"N\":1}}&productsJSON=[\"O=O\",\"[O]\"]&temperature=2273.15&pressure=101.325" );
// if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
/* only disable 100-continue header if explicitly requested */
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all (headerlist);
}
return 0;
}
You want CURLOPT_HTTPPOST or CURLOPT_POSTFIELDS, not both. They will issue two different kinds of POST requests.