News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Abot wininet.dll and user interaction

Started by QvasiModo, July 18, 2005, 04:56:29 PM

Previous topic - Next topic

QvasiModo

Hi folks, just a quick question here: if I use the wininet.dll API to download files using the http/shttp protocols, what kind of user interaction can I expect? I mean, will the dialer pop up? Or if IE hasn't been run for the first time, will the Internet Connection Wizard pop up instead?

Thanks for your time. :U

Mark Jones

#1
Hi Qvasi. :) I don't know if the ICW will pop up (it should...) but here's a program you can use to test the idea. :)


; "Download a file from the 'net" example by MCJ 2005

include masm32rt.inc    ; new MASM32 v8.2 SP2a compile-time libs
include wininet.inc     ; internet libs
includelib wininet.lib

GetWinInetFile proto :DWORD,:DWORD  ; define procedure prototypes

.data           ; initialized data
UsrAgent    db  "Internet Exploder",0   ; User_Agent
Url         db  "http://unc.dl.sourceforge.net/sourceforge/jal/jal-0.4.60.i686.win32.zip",0 ; URL to d/l

.data?          ; runtime (uninitialized) data 
ibuffer     db  4096 dup(?)         ; file receive buffer
filename    dd  ?                   ; filename pointer

.code           ; execution starts here
start:
    print chr$("Console 'Net Download Test MCJ/2005",13,10)
    print chr$("Target: ",60h)
    print addr Url
    print chr$(27h," ",13,10)

    mov edx, offset Url     ; load offset of Url
@@:
    mov al, byte ptr [edx]  ; get a byte into al
    test al, al             ; byte=00h?(end of url?)
    je @F                   ; jump forward if so
    inc edx                 ; else increment pointer
    jmp @B                  ; and jump back
@@:                         ; now edx points at end of Url
    dec edx                 ; backup one
    mov al, byte ptr [edx]  ; load it
    cmp al, "/"             ; is the last char a "/"?
    jnz @F                  ; if not, leave it alone
    mov byte ptr [edx], 00  ; otherwise overwrite it!
@@:                         ; we are now at the end of the filename
    dec edx                 ; search backwards again
    mov al, byte ptr [edx]  ; load byte into al
    cmp al, "/"             ; is it the / before the filename?
    jnz @B                  ; keep looping until the / is found
    inc edx                 ; then move forward one
    mov [filename], edx     ; store pointer value as filename

    print chr$("Filename: ")
    print dword ptr filename
   
    mov eax, input(13,10,13,10,"Press ENTER to start download.")

    mov eax, FUNC (GetWinInetFile,addr Url,dword ptr filename)
    .if eax == 0
        print chr$(13,10,"Download failure!",13,10)
    .else
        print chr$(13,10,"Download successful!",13,10)
    .endif

    mov eax, input(13,10,"Press ENTER to exit...")
    invoke ExitProcess,0    ; terminate gracefully


GetWinInetFile proc fUrl,fSav:DWORD     ; routine originally by Fraiseur
local hUrl,fHand,bwrite,szBuffer,hSession,good:DWORD

    mov good,0                          ; preset flag to "failure"
    mov hSession, FUNC (InternetOpen,addr UsrAgent,INTERNET_OPEN_TYPE_PRECONFIG,0,0,0)
    .if hSession != 0                   ; if InternetOpen ok
        mov hUrl,FUNC (InternetOpenUrl,hSession,fUrl,0,0,INTERNET_FLAG_NO_AUTH or INTERNET_FLAG_NO_AUTO_REDIRECT or INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES,0)
        .if hUrl != 0                   ; if InternetOpenUrl ok
            mov fHand, FUNC (CreateFile,fSav,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0)
            .if fHand != INVALID_HANDLE_VALUE   ; if file create ok
                .while szBuffer != 0    ; loop until entire file is received
                    print chr$(".")     ; 1 progress indicator per buffer fill
                    invoke InternetReadFile,hUrl,addr ibuffer,sizeof ibuffer,addr szBuffer
                    invoke WriteFile,fHand,addr ibuffer,szBuffer,addr bwrite,0
                .endw
                invoke CloseHandle,fHand
                invoke InternetCloseHandle,hUrl
                invoke InternetCloseHandle,hSession
                mov good,1              ; we wrote the file successfully
            .endif
        .endif
    .endif
    mov eax, good                       ; return good's value
    ret
GetWinInetFile endp
end start


Edit: small typo. As usual. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

QvasiModo

I was hoping I didn't have to actually test it :toothy, it might depend on Os version and/or settings. I'll give it a try though, thanks :thumbu I'll post my findings here.