Download library "cdhc.dll" provides threads & infos

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Download library "cdhc.dll" provides threads & infos

Post by Mijikai »

I hope my library will be helpful to someone :)

The library can download & catch (download to memory) files from the internet.
It provides state & progress!

Example:

Code: Select all

Import "cdhc.lib";or "cdhc64.lib"
  cdhcAgent(Custom.s)
  cdhcSource(Url.s);*STR
  cdhcSize(Url.s)
  cdhcDownload(Url.s,File.s,Thread.i,Free.i)
  cdhcCatch(Url.s,Thread.i)
  cdhcTaskState(*Task)
  cdhcTaskBytes(*Task)
  cdhcTaskPercent(*Task);
  cdhcTaskBytesRececived(*Task)
  cdhcTaskBytesRemaining(*Task)
  cdhcTaskBuffer(*Task)
  cdhcTask(*Task)
  cdhcTaskStop(*Task);will stop/kill & free a task!
  cdhcTaskFree(*Task)
EndImport

Debug "TEST SOURCE:"
*HttpSource.String = cdhcSource("http://www.purebasic.fr/english/search.php?search_id=active_topics")
If *HttpSource
  Debug *HttpSource\s
EndIf

Debug "TEST 1:"
Task.i = cdhcCatch("https://upload.wikimedia.org/wikipedia/commons/3/3d/LARGE_elevation.jpg",#True)
If Task
  Repeat
    Debug "Download: " + Str(cdhcTaskPercent(Task)) + " % Bytes: " + Str(cdhcTaskBytes(Task)) + " - " + Str(cdhcTaskBytesRececived(Task)) + " downloaded" 
  Until cdhcTaskState(Task)
  If cdhcTask(Task)
    Debug "Download complete!"
    Debug "Buffer: " + Hex(cdhcTaskBuffer(Task))
    Debug "BufferSize: " + Str(cdhcTaskBytes(Task)) + " Bytes"
    cdhcTaskFree(cdhcTaskBuffer(Task))
  Else
    Debug "Download error!"
  EndIf
  cdhcTaskFree(Task)
EndIf

Debug "TEST 2:"
Buffer.i = cdhcCatch("http://www.purebasic.fr/english/styles/subsilverPlus/imageset/purebasic_logo.png",#False)
If Buffer
  Debug "File downloaded to memory!"
  Debug "Buffer: " + Hex(Buffer)
  Debug "BufferSize: " + cdhcTaskBytes(#Null);or MemorySize(Buffer)
  cdhcTaskFree(Buffer);or FreeMemory(Buffer)
EndIf

Debug "TEST 3:"
If cdhcDownload("http://www.purebasic.fr/english/styles/subsilverPlus/imageset/purebasic_logo.png","testlogo.png",#False,#Null)
  Debug "Download success!"
Else
  Debug "Download error!"
EndIf

Debug "TEST 4:"
If cdhcDownload("http://www.purebasic.fr/english/styles/subsilverPlus/imageset/purebasic_logo.png","testlogo2.png",#True,#True)
  Debug "Download thread started - no more infos!"
EndIf

Debug "TEST 5:"
Task.i = cdhcDownload("https://upload.wikimedia.org/wikipedia/commons/3/3d/LARGE_elevation.jpg","testimage.jpg",#True,#False)
If Task
  Repeat
    Debug "Download: " + Str(cdhcTaskPercent(Task)) + " % Bytes: " + Str(cdhcTaskBytes(Task)) + " - " + Str(cdhcTaskBytesRececived(Task)) + " downloaded" 
  Until cdhcTaskState(Task)
  If cdhcTask(Task)
    Debug "Download complete!"
    Debug "FileSize: " + Str(cdhcTaskBytes(Task)) + " Bytes"
  Else
    Debug "Download error!"
  EndIf
  cdhcTaskFree(Task)
EndIf

;Tested on Windows 10
Download cdhc.dll & cdhc64.dll v.alpha (only Windows!):
https://drive.google.com/file/d/0B3QupQ ... sp=sharing