Solved

How to call InternetOpen Asynchronous

Posted on 2003-12-03
5
1,901 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

16 Experts available now in Live!

Get 1:1 Help Now