AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

libcurl example

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
olfen



Joined: 04 Jun 2005
Posts: 99
Location: Stuttgart, Germany

PostPosted: Thu Aug 30, 2007 4:18 pm    Post subject: libcurl example Reply with quote

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 Thu Aug 30, 2007 7:11 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
daonlyfreez



Joined: 16 Mar 2005
Posts: 745
Location: Berlin

PostPosted: Thu Aug 30, 2007 6:38 pm    Post subject: Reply with quote

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

Cool
_________________
(sorry, homesite offline atm)
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Murp|e



Joined: 12 Jan 2007
Posts: 240
Location: Norway

PostPosted: Mon Jan 14, 2008 10:42 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
derjoo
Guest





PostPosted: Sat Jan 19, 2008 10:32 am    Post subject: Reply with quote

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
Back to top
majkinetor



Joined: 24 May 2006
Posts: 3624
Location: Belgrade

PostPosted: Sat Jan 19, 2008 12:02 pm    Post subject: Reply with quote

@C:\Users\Admin\AppData\Local\Temp\dat20080127163022
Back to top
View user's profile Send private message MSN Messenger
olfen



Joined: 04 Jun 2005
Posts: 99
Location: Stuttgart, Germany

PostPosted: Sat Jan 19, 2008 1:13 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
derjoo



Joined: 19 Jan 2008
Posts: 3
Location: London

PostPosted: Sat Jan 19, 2008 2:00 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
olfen



Joined: 04 Jun 2005
Posts: 99
Location: Stuttgart, Germany

PostPosted: Sat Jan 19, 2008 2:54 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
derjoo



Joined: 19 Jan 2008
Posts: 3
Location: London

PostPosted: Sun Jan 20, 2008 11:27 pm    Post subject: Reply with quote

aaaa alright!
thank you ever so much... Embarassed still not enough time to learn c Embarassed
why is it better to store this codes as hex?
you could also pass an normal int by dllcall, couldn't you?
Back to top
View user's profile Send private message
Murp|e



Joined: 12 Jan 2007
Posts: 240
Location: Norway

PostPosted: Mon Jan 21, 2008 12:37 pm    Post subject: Reply with quote

Wunderbar olfen!
Back to top
View user's profile Send private message Visit poster's website
derjoo



Joined: 19 Jan 2008
Posts: 3
Location: London

PostPosted: Tue Jan 22, 2008 9:23 pm    Post subject: Reply with quote

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...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group