News:

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

WinInet

Started by KoDeR, June 02, 2005, 03:39:32 PM

Previous topic - Next topic

KoDeR

.if eax==IDC_BTN1
invoke InternetOpen,0,0,0,0,0
mov hInet,eax
invoke GetDlgItemText,hWnd,1001,addr Url,400
invoke InternetOpenUrl,hInet,eax,0,0,0,0
invoke GetDlgItemText,hWnd,1002,addr LocalFile,256
invoke CreateFile,addr LocalFile,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
mov hFile,eax
invoke InternetReadFile,Url,BUFFER,2000h,rwURL
invoke WriteFile,[hFile],BUFFER,[rwURL],rwFile,0
invoke CloseHandle,[hFile]
invoke InternetCloseHandle,[hInet]
.endif

(I tryed to create simple downloader.)

Why it doesn't work ? This program closes after CreateFile (the file has zero size  :( ); :dazzled:

Tedd

I see nothing obvious, but try replacing OPEN_ALWAYS, in the call to createfile, with CREATE_ALWAYS (or CREATE_NEW)
No snowflake in an avalanche feels responsible.

Faiseur

Hi,

test these adjustments..


.if eax==IDC_BTN1
invoke InternetOpen,0,0,0,0,0
mov hInet,eax
invoke GetDlgItemText,hWnd,1001,addr Url,400
invoke InternetOpenUrl,hInet,eax,0,0,0,0  ;<-- The return value of "GetDlgItemText" is the number of tchar copied. Replace eax by "addr Url"

; add this:
mov hUrl,eax ; hUrl == DWORD

invoke GetDlgItemText,hWnd,1002,addr LocalFile,256
invoke CreateFile,addr LocalFile,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
mov hFile,eax
invoke InternetReadFile,Url,BUFFER,2000h,rwURL ; replace Url by "hUrl", and replace rwURL by "addr rwURL"
invoke WriteFile,[hFile],BUFFER,[rwURL],rwFile,0
invoke CloseHandle,[hFile]

;add this:
invoke InternetCloseHandle,hUrl

invoke InternetCloseHandle,[hInet]
.endif


Note: Like said it Tedd, it is preferable to replace "OPEN_ALWAYS" by "CREATE_ALWAYS"...
French asm Forum: http://www.asmforum.net/   Website: http://www.faiseur.net/

KoDeR

THX, but it crashes again  :(


hInstance dd ?
CommandLine dd ?
hWnd dd ?
Url db ?
LocalFile db ?
hFile dd ?
hInet dd ?
hUrl dd ?
rwURL dd ?
rwFile dd ?

BUFFER dd 2000h


may be i have some wrong definitions ? ::)

sluggy

As Faiseur pointed out, this line:


     invoke InternetOpenUrl,hInet,eax,0,0,0,0


is wrong. Change it to:


     invoke InternetOpenUrl, hInet, ADDR Url, 0, 0, 0, 0


You were effectively passing garbage as your url. And as he also pointed out, don't forget to change the InternetReadFile line, you need to use pointers.


Faiseur

Hi,

this is my procedure, with checks...return 1 if success or 0 if errors.

Syntax of call:

invoke GetWinInetFile, addr Url, addr LocalFile

Correct your datas:
Url     db 256 dup(?)
LocalFile db 256 dup(?)




GetWinInetFile PROTO :DWORD,:DWORD


.code

; Success == 1, 0 == error
GetWinInetFile PROC fUrl,fSav:DWORD
LOCAL hUrl,fHand,bwrite,szBuffer,hSession: DWORD
LOCAL Buffer[1024]: BYTE

mov hSession, FUNC (InternetOpen,chr$('toto'),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL)
.if hSession != 0
    mov hUrl,FUNC (InternetOpenUrl,hSession,fUrl,NULL,NULL,NULL,NULL)
    .if hUrl != 0
        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
            inc szBuffer
            .WHILE szBuffer != 0
               invoke InternetReadFile,hUrl,addr Buffer,sizeof Buffer,addr szBuffer
               invoke WriteFile,fHand,addr Buffer,szBuffer,ADDR bwrite,NULL
            .ENDW
            invoke CloseHandle,fHand
            invoke InternetCloseHandle,hUrl
            invoke InternetCloseHandle,hSession
        .endif         
    .endif
.endif
ret
GetWinInetFile ENDP
French asm Forum: http://www.asmforum.net/   Website: http://www.faiseur.net/

KoDeR

I don't know why it doesn't work ::) ; please have a look... It's project for RadASM.

[attachment deleted by admin]

Faiseur

Ok, your project work if you take the values "Url" and "LocalFile" before using the procedure GetWinInetFile... And no put Null value in the last parameter of GetDlgItemText. This specifies the maximum length of the string to be copied to the buffer.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/dialogboxes/dialogboxreference/dialogboxfunctions/getdlgitemtext.asp


invoke GetDlgItemText,hWnd,1001,addr LocalFile,sizeof LocalFile
invoke GetDlgItemText,hWnd,1002,addr Url,sizeof Url
invoke GetWinInetFile,addr Url,addr LocalFile


You must recover the parameters necessary to the procedure BEFORE calling it. See GetWinInetFile as if it is an API function. With API you not recover the parameters in its function but, of course, before using it.  It is the same thing here. Always see a procedure like something of separate with your main program.  :wink

Note: i have added macros (include \masm32\macros\macros.asm) Switch, Case and Func. That makes the code readable for me. Sorry if that does not help you.




[attachment deleted by admin]
French asm Forum: http://www.asmforum.net/   Website: http://www.faiseur.net/

Mark Jones

Nice job Fraiseur, works perfectly in a WinXP console box.

One question, what is 'toto' for in InternetOpen,chr$('toto')? Thanks. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Faiseur

One question, what is 'toto' for in InternetOpen,chr$('toto')? Thanks. :)

Hé hé  :wink

In the msdn, this parameter is the "lpszAgent":

   " [in] Pointer to a null-terminated string that specifies the name of the application or entity calling the WinINet functions. This name is used as the user agent in the HTTP protocol."

That seems useless for a simple download without ftp. But to avoid a possible error I place a name by defect.

French asm Forum: http://www.asmforum.net/   Website: http://www.faiseur.net/

KoDeR

thanks Faiseur, but it doesn't download files and i don't know why ::)  :
For Example i tryed to use "http://www.freshdevices.com/files/frui.exe" as Url
and "D:\1.exe" as LocalFile executing : fn MessageBox,0, "Error"," ",0
How to make it download files :dazzled: 

Faiseur

Hi,

i have tested your example, function fine.



Downloaded 845ko.

Perhaps check that your FW accept your program.

French asm Forum: http://www.asmforum.net/   Website: http://www.faiseur.net/

KoDeR

THANK YOU It works!  :cheekygreen:

How to cut Url string to get file name  :eek?

Faiseur

French asm Forum: http://www.asmforum.net/   Website: http://www.faiseur.net/

KoDeR

I don't understand this  :( Is there any API Function for getting filename from url? ::)