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.

Security noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

Building and Running the Sample

To build and run this sample

  1. Open the solution file async.sln.

  2. From the Build menu, click Build Solution.

  3. 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.

How the Sample Works

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.

Attributes

This sample uses the following attributes:

coclass, default, dual, helpstring, id, implements_category, in, module, object, out, pointer_default, progid, propget, propput, registration_script, retval, threading, uuid, version, vi_progid

Keywords

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

Note

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.

See Also

Other Resources

ATL Attributes Samples