AutoHotkey Community

It is currently May 25th, 2012, 10:42 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: libcurl example
PostPosted: August 30th, 2007, 4:18 pm 
Offline

Joined: June 4th, 2005, 1:30 am
Posts: 113
Location: Stuttgart, Germany
Quote:
libcurl is a free and easy-to-use client-side URL transfer library, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE and LDAP.
libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication
(Basic, Digest, NTLM, Negotiate, Kerberos4), file transfer resume, http proxy tunneling and more!

libcurl is highly portable, it builds and works identically on numerous platforms, including Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, HPUX, IRIX
, AIX, Tru64, Linux, UnixWare, HURD, Windows, Amiga, OS/2, BeOs, Mac OS X, Ultrix, QNX, OpenVMS, RISC OS, Novell NetWare, DOS and more...

libcurl is free, thread-safe, IPv6 compatible, feature rich, well supported, fast, thoroughly documented and is already used by many
known, big and successful companies and numerous applications.

Here's a simple example for downloading a file, while showing download progress.
The script requires FileHelper.ahk.
You can directly download libcurl here.
Code:
#SingleInstance, force
#NoEnv
#include FileHelper.ahk ; http://www.autohotkey.com/forum/topic19608.html

CURLOPT_URL := 0x2712
CURLOPT_WRITEDATA := 0x2711
CURLOPT_WRITEFUNCTION := 0x4E2B
CURLOPT_PROGRESSFUNCTION := 0x4E58
CURLOPT_NOPROGRESS := 0x2B
CURLOPT_ERRORBUFFER := 0x271A
CURLOPT_TRANSFERTEXT := 0x35
CURL_ERROR_SIZE := 0x100


Url = http://dl.google.com/earth/client/branded/redirect/Google_Earth_BZXV.exe
hFile := CreateFile(A_ScriptDir . "\Google_Earth_BZXV.exe", 1)


hModule := DllCall("LoadLibrary", "str", "libcurl-4.dll")
If !hModule
{
  MsgBox, Error loading lib
  ExitApp
}

MsgBox % Curl_Version()

hCurlEasy := DllCall("libcurl-4\curl_easy_init")
pCurl_WriteFunction := RegisterCallback("Curl_WriteFunction", "C F")
pCurl_ProgressFunction := RegisterCallback("Curl_ProgressFunction", "C F")
VarSetCapacity(CURL_ERROR, CURL_ERROR_SIZE + 1)
DllCall("libcurl-4\curl_easy_setopt", "UInt", hCurlEasy, "UInt", CURLOPT_ERRORBUFFER, "UInt", &CURL_ERROR, "Cdecl")
DllCall("libcurl-4\curl_easy_setopt", "UInt", hCurlEasy, "UInt", CURLOPT_URL, "Str", URL, "Cdecl")
DllCall("libcurl-4\curl_easy_setopt", "UInt", hCurlEasy, "UInt", CURLOPT_NOPROGRESS, "Int", 0, "Cdecl")
DllCall("libcurl-4\curl_easy_setopt", "UInt", hCurlEasy, "UInt", CURLOPT_PROGRESSFUNCTION, "UInt", pCurl_ProgressFunction, "Cdecl")
DllCall("libcurl-4\curl_easy_setopt", "UInt", hCurlEasy, "UInt", CURLOPT_WRITEFUNCTION, "UInt", pCurl_WriteFunction, "Cdecl")
If DllCall("libcurl-4\curl_easy_perform", "UInt", hCurlEasy)
  MsgBox % CURL_ERROR


Progress, Off
CloseHandle(hFile)

VarSetCapacity(CURLINFO_SPEED_DOWNLOAD, 8)
DllCall("libcurl-4\curl_easy_getinfo", "UInt", hCurlEasy, "UInt", 0x300000 + 9, "UInt", &CURLINFO_SPEED_DOWNLOAD, "Cdecl")
VarSetCapacity(CURLINFO_TOTAL_TIME, 8)
DllCall("libcurl-4\curl_easy_getinfo", "UInt", hCurlEasy, "UInt", 0x300000 + 3, "UInt", &CURLINFO_TOTAL_TIME, "Cdecl")
MsgBox % "Average download speed: " . Round(NumGet(CURLINFO_SPEED_DOWNLOAD, 0, "Double"), 0) . " bytes\s`nTransfer time: " . Round(NumGet(CURLINFO_TOTAL_TIME, 0, "Double"), 0) . " seconds"

DllCall("libcurl-4\curl_easy_cleanup", "UInt", hCurlEasy)
DllCall("FreeLibrary", "UInt", hModule)




Curl_ProgressFunction(clientp, dltotal_l, dltotal_h, dlnow_l, dlnow_h, ultotal_l, ultotal_h, ulnow_l, ulnow_h)
{
  VarSetCapacity(dltotal, 8, 0)
  NumPut(dltotal_l, dltotal, 0), NumPut(dltotal_h, dltotal, 4)
 
  VarSetCapacity(dlnow, 8, 0)
  NumPut(dlnow_l, dlnow, 0), NumPut(dlnow_h, dlnow, 4)
 
  KBTotal := Round((NumGet(dltotal, 0, "Double") / 1024), 2)
  KBNow := Round((NumGet(dlnow, 0, "Double") / 1024), 2)
  Percent := Round((NumGet(dlnow, 0, "Double") / NumGet(dltotal, 0, "Double") * 100), 2)
  Progress, %Percent%, %KBNow% of %KBTotal% KB (%Percent% `%)
 
  Return 0
}


Curl_WriteFunction(pBuffer, size, nitems, pOutStream)
{
  global hFile
  WriteFile(hFile, pBuffer, size*nitems)
  return size*nitems
}


Curl_Version()
{
  Return DllCall("MulDiv", "Int", DllCall("libcurl-4\curl_version"), "Int",1, "Int",1, "str")
}

Edit1: Added example of curl_easy_getinfo


Last edited by olfen on August 30th, 2007, 7:11 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 30th, 2007, 6:38 pm 
Offline

Joined: March 16th, 2005, 10:33 pm
Posts: 968
Location: Frisia
Great script!

Will be a tough one to implement all cURL's functions, but it is amazing what it can do, so - well worth it.

curl.exe could now hopefully be replaced one-day with libcurl.dll

8)

_________________
Image mirror 1mirror 2mirror 3ahk4.me • PM or Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2008, 10:42 pm 
Offline

Joined: January 12th, 2007, 4:30 am
Posts: 531
Location: Norway
olfen, this script is great. Is there any chance you could elaborate on some of the other libcurl functions such as say... upload via FTP with progress bar??? :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2008, 10:32 am 
can anybody help me, where the RegisterCallback function points to? i cant find this function in the filehelper nor in the ahk-docs....
many thanks
jo


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2008, 12:02 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
@C:\Users\Admin\AppData\Local\Temp\dat20080127163022


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2008, 1:13 pm 
Offline

Joined: June 4th, 2005, 1:30 am
Posts: 113
Location: Stuttgart, Germany
Murp|e wrote:
olfen, this script is great. Is there any chance you could elaborate on some of the other libcurl functions such as say... upload via FTP with progress bar??? :wink:
I've been very busy the last few months and will be until March. I'll try to add more examples then.

derjoo wrote:
can anybody help me, where the RegisterCallback function points to? i cant find this function in the filehelper nor in the ahk-docs....
You probably use AHK version < 1.0.47


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2008, 2:00 pm 
Offline

Joined: January 19th, 2008, 1:55 pm
Posts: 3
Location: London
alright. thank you very much.
but i've another question.....
where have you got the hex-values from (on top of the program)?
i can't find them anywhere....
thank you!
joe


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2008, 2:54 pm 
Offline

Joined: June 4th, 2005, 1:30 am
Posts: 113
Location: Stuttgart, Germany
derjoo wrote:
where have you got the hex-values from (on top of the program)?

Example (code from curl.h):
Code:
#define CURLOPTTYPE_FUNCTIONPOINT 20000

CINIT(WRITEFUNCTION, FUNCTIONPOINT, 11)

CINIT(NOPROGRESS, LONG, 43)
becomes
Code:
CURLOPT_NOPROGRESS := 0x2B
CURLOPT_WRITEFUNCTION := 0x4E2B
or
Code:
CURLOPT_NOPROGRESS := 43
CURLOPT_WRITEFUNCTION := 20011


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2008, 11:27 pm 
Offline

Joined: January 19th, 2008, 1:55 pm
Posts: 3
Location: London
aaaa alright!
thank you ever so much... :oops: still not enough time to learn c :oops:
why is it better to store this codes as hex?
you could also pass an normal int by dllcall, couldn't you?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2008, 12:37 pm 
Offline

Joined: January 12th, 2007, 4:30 am
Posts: 531
Location: Norway
Wunderbar olfen!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2008, 9:23 pm 
Offline

Joined: January 19th, 2008, 1:55 pm
Posts: 3
Location: London
another question in connection with this topic:
what is the easiest way to get the value of the var which is given by the pointer *ptr (the buffer in olfens script) ?
i want to write the source code directly to a variable instead of the file...
thanks...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 8th, 2011, 5:27 pm 
Offline

Joined: November 18th, 2010, 7:26 pm
Posts: 124
I have everything downloaded but I'm not sure where to put the libcurl at...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 9th, 2011, 10:27 am 
crazyace: I think you're probably better off trying the libcurl wrapper by deathbynukes.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: ELengefeld and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group