Solved

How to call InternetOpen Asynchronous

Posted on 2003-12-03
5
1,913 Views
Last Modified: 2007-12-19
In my programme,I want ot donwload file from Internet.I use InternetOpen with parameter INTERNET_FLAG_ASYNC,in order to make an asynchronous call.

hSession := InternetOpen('http-get-demo',
                            INTERNET_OPEN_TYPE_PRECONFIG,
                            nil,nil,INTERNET_FLAG_ASYNC);

But I don't know what to do next...
Could you give me an example for this...Thanks
Excuse me for my English is no so good .
0
Comment
Question by:yyfly
5 Comments
 
LVL 2

Accepted Solution

by:
TheLeader earned 63 total points
ID: 9872685
function GetInetFile
(const fileURL, FileName: String): boolean;
const BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  f: File;
  sAppName: string;
begin
Result:=False;
sAppName := ExtractFileName(Application.ExeName);
hSession := InternetOpen(PChar(sAppName),
                INTERNET_OPEN_TYPE_PRECONFIG,
               nil, nil, 0);
try
  hURL := InternetOpenURL(hSession,
            PChar(fileURL),
            nil,0,0,0);
  try
   AssignFile(f, FileName);
   Rewrite(f,1);
   repeat
    InternetReadFile(hURL, @Buffer,
                     SizeOf(Buffer), BufferLen);
    BlockWrite(f, Buffer, BufferLen)
   until BufferLen = 0;
   CloseFile(f);
   Result:=True;
  finally
   InternetCloseHandle(hURL)
  end
finally
  InternetCloseHandle(hSession)
end
end;


procedure TForm1.button1Click(Sender: TObject);
var  InternetFile,LocalFile: string;
begin
InternetFile:=' http://www.delphitips.com/images/delphispirit.gif';
LocalFile:='c:/delphispirit.gif';

if GetInetFile(IntrnetFile,LocalFile)=True then
   ShowMessage('download')
else
  ShowMessage('Can not download the updated files');
end;

and heres another examples:
http://www.scalabium.com/faq/dct0080.htm
http://delphi.about.com/library/weekly/aa013001a.htm

hope it helped...
0
 

Author Comment

by:yyfly
ID: 9872732
thank you .but in you example,you use InternetOpen like this

hSession := InternetOpen(PChar(sAppName),
                INTERNET_OPEN_TYPE_PRECONFIG,
               nil, nil, 0);

but i want the last parameter is INTERNET_FLAG_ASYNC..
and who can tell me the different between asynchronous and synchronous..
0
 
LVL 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 62 total points
ID: 9873039
InternetOpen
Initialises the WinInet API.

function InternetOpen(
    lpszAgent: PChar;
    dwAccessType: DWORD;
    lpszProxy: PChar;
    lpszProxyBypass: PChar;
    dwFlags: DWORD): HINTERNET;
Parameters:

lpszAgent
Name of the calling application. This is used as the user agent for HTTP protocol.
dwAccessType
Access type required, use one of:
INTERNET_OPEN_TYPE_DIRECT
Resolve all host names locally.
INTERNET_OPEN_TYPE_PRECONFIG
Retrieve proxy or direct configuration from the registry.
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY
Retrieves proxy or direct configuration from the registry and prevents the use of any start-up script.
INTERNET_OPEN_TYPE_PROXY
Requests passed on to the proxy. If a proxy bypass list is supplied and the name is on that list then INTERNET_OPEN_TYPE_DIRECT is used instead.
lpszProxy
If dwAccessType is set to INTERNET_OPEN_TYPE_PROXY then this should be the name of the proxy server(s) to use. WinInet API functions recognise only CERN type proxies (HTTP only) and TIS FTP gateway (FTP only). If Internet Explorer is installed then SOCKS proxy are also supported. For all other values of dwAccessType this should be set to nil.
lpszProxyBypass
String containing an optional list of host names or IP addresses, which should not be routed through the proxy. If dwAccessType is not INTERNET_OPEN_TYPE_PROXY then this parameter is ignored and should be set to nil.
dwFlags
Various configuration options. Use one or more of:
INTERNET_FLAG_ASYNC
Makes only asynchronous requests.
INTERNET_FLAG_FROM_CACHE
All entries are returned from the local cache. Requests will fail if the item is not in the local cache.
INTERNET_FLAG_OFFLINE
Same as INTERNET_FLAG_FROM_CACHE
Returns: Handle for use with the other WinInet API functions or nil if the function fails in which case details of the specific error can be found by calling GetLastError().

Note:

InternetOpen should be the first WinInet API function called by the application.
When the application finishes using the WinInet API it should call InternetCloseHandle to free the handle and any associated resources.
Applications may make more than one call to InternetOpen (although only one is required).

BTW i'd use UrlMon function....

uses URLMon;


if URLDownloadToFile(nil, 'http://www.afsoftware.it', 'c:\index.html', 0, nil) <> 0
then
MessageBox(Handle, 'An error ocurred while downloading the file.', PChar
(Application.Title), MB_ICONERROR or MB_OK);
0
 
LVL 2

Expert Comment

by:-Karamja-
ID: 12382467
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Split: TheLeader & Ferruccio68

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Karamja (Alan)
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now