Solved

How to call InternetOpen Asynchronous

Posted on 2003-12-03
5
1,980 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 23

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

615 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