Author Topic: Download a file from the internet  (Read 2671 times)

Matthias Merkel

  • Regular Member
  • *
  • Posts: 6
Download a file from the internet
« on: December 17, 2017, 11:00:20 PM »
I've been trying to figure this out for a few hours now but wasn't able to find a way to download a file from the internet. Does anyone know how that would be possible to do?

jj2007

  • Member
  • *****
  • Posts: 8519
  • Assembler is fun ;-)
    • MasmBasic
Re: Download a file from the internet
« Reply #1 on: December 17, 2017, 11:37:38 PM »
Like this?

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  FileWrite "test.html", FileRead$("http://masm32.com/board/index.php?board=1.0")
  ShEx "test.html"   ; show it in your browser
EndOfCode



Matthias Merkel

  • Regular Member
  • *
  • Posts: 6
Re: Download a file from the internet
« Reply #2 on: December 17, 2017, 11:52:27 PM »
That is awesome! Thank you so much.

AW

  • Member
  • *****
  • Posts: 1357
  • Let's Make ASM Great Again!
Re: Download a file from the internet
« Reply #3 on: December 18, 2017, 01:12:10 AM »
 :biggrin:

You can also do it in pure VB.Net and have lots of fun too:

Code: [Select]
        Using client As System.Net.WebClient = New System.Net.WebClient()
            client.DownloadFile("http://masm32.com/board/index.php?board=1.0", "test.html")
        End Using

Matthias Merkel

  • Regular Member
  • *
  • Posts: 6
Re: Download a file from the internet
« Reply #4 on: December 18, 2017, 03:36:46 AM »
:biggrin:

You can also do it in pure VB.Net and have lots of fun too:

Code: [Select]
        Using client As System.Net.WebClient = New System.Net.WebClient()
            client.DownloadFile("http://masm32.com/board/index.php?board=1.0", "test.html")
        End Using

The question was about ASM though.

AW

  • Member
  • *****
  • Posts: 1357
  • Let's Make ASM Great Again!
Re: Download a file from the internet
« Reply #5 on: December 18, 2017, 03:56:55 AM »
The important thing is that you are happy with what you have learnt about ASM.

felipe

  • Member
  • ****
  • Posts: 867
  • Eagles are just great!
Re: Download a file from the internet
« Reply #6 on: December 18, 2017, 04:16:35 AM »
 :P

Welcome to the forum Matthias Merkel.   :icon14:
Felipe.

Matthias Merkel

  • Regular Member
  • *
  • Posts: 6
Re: Download a file from the internet
« Reply #7 on: December 18, 2017, 04:22:30 AM »
:P

Welcome to the forum Matthias Merkel.   :icon14:

Thanks

Vortex

  • Member
  • *****
  • Posts: 1810
Re: Download a file from the internet
« Reply #8 on: December 18, 2017, 06:55:05 AM »
Hi Matthias Merkel,

Here is a quick example :

Code: [Select]
.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\urlmon.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\urlmon.lib

.data

szURL       db 'http://vortex.masmcode.com/files/Wcrt0_7.zip',0
FileName    db 'TinyCRT.zip',0

.code

start:

    invoke  URLDownloadToFile,0,ADDR szURL,\
            ADDR FileName,0,0

    invoke  ExitProcess,0

END start

The trick is to use the API function URLDownloadToFile.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5491
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Download a file from the internet
« Reply #9 on: December 18, 2017, 12:16:20 PM »
I wonder if anyone knows how to construct the callback for the last argument to get download progress. The API works fine in 64 bit but it has a COM interface for the callback that I have no data on at all. Other than that, is there any viable alternatives for win64 (win7 up) that can both do the download AND have progress information ?

This in 64 bit works fine.

invoke URLDownloadToFile,0,url,targ,0,0       ; pCB
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

Matthias Merkel

  • Regular Member
  • *
  • Posts: 6
Re: Download a file from the internet
« Reply #10 on: December 18, 2017, 04:49:33 PM »
Hi Matthias Merkel,

Here is a quick example :

Code: [Select]
.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\urlmon.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\urlmon.lib

.data

szURL       db 'http://vortex.masmcode.com/files/Wcrt0_7.zip',0
FileName    db 'TinyCRT.zip',0

.code

start:

    invoke  URLDownloadToFile,0,ADDR szURL,\
            ADDR FileName,0,0

    invoke  ExitProcess,0

END start

The trick is to use the API function URLDownloadToFile.

The huge issue with that way is that it's detected by a lot of antivirus vendors as malware: https://www.virustotal.com/#/file/4785d45664ef436c1f2cbd11e9dd46a358aee36f323fd213101c289b5632b523/detection

AW

  • Member
  • *****
  • Posts: 1357
  • Let's Make ASM Great Again!
Re: Download a file from the internet
« Reply #11 on: December 18, 2017, 06:12:03 PM »
Other than that, is there any viable alternatives for win64 (win7 up) that can both do the download AND have progress information ?
I prefer InternetOpenUrl, HttpQueryInfo, InternetReadFile. HttpQueryInfo gives you the number of bytes to read.

AW

  • Member
  • *****
  • Posts: 1357
  • Let's Make ASM Great Again!
Re: Download a file from the internet
« Reply #12 on: December 18, 2017, 06:14:15 PM »
Quote
The huge issue with that way is that it's detected by a lot of antivirus vendors as malware
The antivirus are particularly dumb and many of them hate polink.

jj2007

  • Member
  • *****
  • Posts: 8519
  • Assembler is fun ;-)
    • MasmBasic
Re: Download a file from the internet
« Reply #13 on: December 18, 2017, 07:21:25 PM »
The huge issue with that way is that it's detected by a lot of antivirus vendors as malware

Yes indeed. Try the attached version, btw with progress indicator in the statusbar (Jotti:  2/18 scanners reported malware). Here is the source:
Code: [Select]
GuiParas equ "Downloading a file is easy"
include \masm32\MasmBasic\Res\MbGui.asm
GuiControl Edit, "edit"
GuiControl Status, "statusbar"
Download "http://masm32.com/board/index.php?topic=6771.msg72451#msg72451", cb:MyCallback, msg:hWnd_
Event Message
  .if uMsg==WM_DOWNLOADFINISHED
SetWin$ hEdit="Download finished"+CrLf$+NoTag$(wParam)
MsgBox 0, "Save contents to test.html?", "Hi", MB_YESNOCANCEL
If_ eax==IDYES Then <mcs FileWrite "test.html", wParam : ShEx "test.html">
  .endif
EndOfEvents
  MyCallback:
  SetWin$ hStatus=Str$("%i bytes\ndownloaded - hold Shift to stop it", edx) ; currrent state in edx
  invoke GetKeyState, VK_SHIFT
  test ah, ah ; stop download if Sign? flag set
  retn
GuiEnd

caballero

  • Member
  • *****
  • Posts: 1009
    • Abre Ojos Ensamblador
Re: Download a file from the internet
« Reply #14 on: December 18, 2017, 09:19:13 PM »
The huge issue with that way is that it's detected by a lot of antivirus vendors as malware

Yes indeed. Try the attached version, btw with progress indicator in the statusbar (Jotti:  2/18 scanners reported malware). Here is the source:
Code: [Select]
GuiParas equ "Downloading a file is easy"
include \masm32\MasmBasic\Res\MbGui.asm
GuiControl Edit, "edit"
GuiControl Status, "statusbar"
Download "http://masm32.com/board/index.php?topic=6771.msg72451#msg72451", cb:MyCallback, msg:hWnd_
Event Message
  .if uMsg==WM_DOWNLOADFINISHED
SetWin$ hEdit="Download finished"+CrLf$+NoTag$(wParam)
MsgBox 0, "Save contents to test.html?", "Hi", MB_YESNOCANCEL
If_ eax==IDYES Then <mcs FileWrite "test.html", wParam : ShEx "test.html">
  .endif
EndOfEvents
  MyCallback:
  SetWin$ hStatus=Str$("%i bytes\ndownloaded - hold Shift to stop it", edx) ; currrent state in edx
  invoke GetKeyState, VK_SHIFT
  test ah, ah ; stop download if Sign? flag set
  retn
GuiEnd
Wow, it is a quite good example, where are the masm sources?  :biggrin:
En un lugar de la Mancha de cuyo nombre no quiero acordarme