Solved

How to call InternetOpen Asynchronous

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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