ASYNC Attributes Sample: Downloads Data Asynchronously
The ASYNC attributes sample creates a control that downloads data asynchronously from a URL. The control implements the IBindStatusCallback interface. Typically, you asynchronously download large binary objects or properties. This allows the control's user interface to remain unblocked during potentially lengthy network operations. The use of asynchronous downloading also gives the user a chance to abort the download. ATL uses WinInet functions internally to implement asynchronous downloading.
The ASYNC sample is the nonattributed version of this sample.
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
To get samples and instructions for installing them:
On the Visual Studio Help menu, click Samples.
For more information, see Visual Studio Samples.
The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.
You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.
To build and run this sample
Open the solution file async.sln.
From the Build menu, click Build Solution.
After the sample builds, open ATLAsync.htm in your Web browser and follow the instructions. This sets the ASYNC control's URL property and starts the download. As data is downloaded, you will see it displayed in the ASYNC control.
You can test the control in the ActiveX Control Test Container. For details on accessing Test Container and using it to test a control, see Testing Properties and Events with Test Container.
ASYNC creates a subclassed edit control with one property called URL. The URL property is a BSTR that represents a URL that points to data. The ASYNC sample uses the ATL CBindStatusCallback class to implement asynchronous downloading. When the control user sets the URL property, ASYNC creates a CBindStatusCallback object. The CBindStatusCallback::StartAsyncDownload method is then called and passed both the URL and a pointer to a callback function. This function, CAtlAsync::OnData, is called by the CBindStatusCallback object and is passed the binary data from the URL as it is received. CAtlAsync::OnData simply sends the received data to the subclassed edit control, where it is displayed.
For an example of how to superclass Windows controls using ATL, see the ATL SubEdit sample.
This sample uses the following keywords:
ALT_MSG_MAP; ATLTRACE ; BEGIN_COM_MAP; BEGIN_MSG_MAP; BEGIN_OBJECT_MAP; BEGIN_PROPERTY_MAP; CBindStatusCallback::Download; CComBSTR::Append; CComCoClass; CComControl; CComModule::GetClassObject; CComModule::GetLockCount; CComModule::Init; CComModule::RegisterServer; CComModule::Term; CComModule::UnregisterServer; CComObjectRoot; COM_INTERFACE_ENTRY; COM_INTERFACE_ENTRY_IMPL; DECLARE_REGISTRY_RESOURCEID; DisableThreadLibraryCalls; DLL_PROCESS_ATTACH; DLL_PROCESS_DETACH; DllMain; END_COM_MAP; END_MSG_MAP; END_OBJECT_MAP; END_PROPERTY_MAP; IDataObjectImpl; IDispatchImpl; IObjectSafetyImpl; IOleControlImpl; IOleInPlaceActiveObjectImpl; IOleInPlaceObjectWindowlessImpl; IOleInPlaceObjectWindowlessImpl::SetObjectRects; IOleObjectImpl; IPerPropertyBrowsingImpl; IPersistPropertyBagImpl; IPersistStorageImpl; IPersistStreamInitImpl; IProvideClassInfo2Impl; IQuickActivateImpl; IsWindow; IViewObjectExImpl; MESSAGE_HANDLER; OBJECT_ENTRY; PROP_ENTRY; SendMessage; USES_CONVERSION
Some of the samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.