© 2004 Microsoft Corporation. All rights reserved.

Figure 1 Volume Device Types

Type
Description
Fixed disk drives
All hard disks. GetDriveType returns DRIVE_FIXED
CD drives
All optical drives (CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, and so on). GetDriveType returns DRIVE_CDROM
Removable disk drives
All removable media drives except the drives qualifying as CD drives. Most mass storage digital camera and portable music players fall into this category since they contain removable media. GetDriveType returns DRIVE_ REMOVABLE. Even though floppy drives also return DRIVE_REMOVABLE, they are not supported by Autoplay since they do not generate media insertion/removal events
Figure 6 Volume's Handler Values

Value
Description
Action
Text string that represents what action the application will take with regard to the content type that triggered an Autoplay response. Examples are "Play music" and "Copy music". Although plain text strings are acceptable, Multi-Language User Interface (MUI) string references are recommended for internationally-aware applications. MUI references specify a resource string that will be dynamically loaded at runtime to match the user locale—for example, "@%SystemRoot%\system32\MyHandler.dll,-100"
Provider
Text string that represents the application. An example is "Windows Media Player". Both the Action value and the Provider value are used to build a description string to be displayed in the user interface—for example, "Play music using Windows Media Player." In this example, Autoplay obtains "Play music" from the Action value and "Windows Media Player" from the Provider value. The word "using" is automatically inserted by Autoplay. The format of the value specified follows the same guidelines as listed for the Action value.
DefaultIcon
Icon that represents the application in the Autoplay UI. The value follows the same guideline as the Icon value in the Autorun.INF section
InvokeProgID
This is the ProgID of an application or COM component providing a Shell sub key that will be invoked the usual way whenever an Autoplay event triggers the launch of this handler. With Windows XP, the application can now provide a DropTarget entry in addition to the command entry under a verb. On Windows XP, this will be the preferred method of calling the handler, and an IDataObject interface will be passed that contains information about the Autoplay event. This method is illustrated in Sample 3, available in the download for this article
InvokeVerb
This is the verb under the ProgID specified in the InvokeProgID value
Figure 7 Non-Volume's Handler Values

Value
Description
Action
Same as the volume handler's action
Provider
Same as the volume handler's provider
DefaultIcon
Same as the volume handler's DefaultIcon
ProgID
The ProgID of a COM component that implements the IHWEventHandler interface
InitCmdLine
The string value that will be passed as is to the Initialize method of the newly created component referred to in the ProgID value
Figure 9 Enumerating Sniffed Files
FORMATETC fmte = { g_cfAutoPlayHIDA, NULL, DVASPECT_CONTENT, -1, 
                   TYMED_HGLOBAL };
STGMEDIUM medium = {0};
        
HRESULT hr = pdataobject->GetData(&fmte, &medium);
        
if (SUCCEEDED(hr))
{
    CIDA* pida = (CIDA*)GlobalLock(medium.hGlobal);
        
    if (pida)
    {
        CDlgSamples::PrintMsg(TEXT("    List of files:"));
        
        for (UINT iItem = 0; iItem < pida->cidl; ++iItem)
        {
            LPITEMIDLIST pidlFull = ILCombine(
                HIDA_GetPIDLFolder(pida),
                HIDA_GetPIDLItem(pida, iItem));
        
            if (pidlFull)
            {
                IShellFolder *psf;
                LPCITEMIDLIST pidlItem;
        
                hr = SHBindToParent(pidlFull, IID_IShellFolder,
                    (void**)&psf, &pidlItem);
        
                if (SUCCEEDED(hr))
                {
                    STRRET strDisplayName;
        
                    // Add this flag if you only want the filename: 
                    // SHGDN_INFOLDER
                    hr = psf->GetDisplayNameOf(pidlItem,
                        SHGDN_FORPARSING, &strDisplayName);
        
                    if (SUCCEEDED(hr))
                    {
                        // Do work here
                    }
        
                    psf->Release();
                }
        
                ILFree(pidlFull);
            }
        }
        
        GlobalUnlock(pida);
    }
        
    ReleaseStgMedium(&medium);
}
Figure 10 Registering a Local Server COM Factory
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE /*hinstPrev*/,
    LPSTR /*szCmdLine*/, int iCmdShow)
{
    if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
    {
        DWORD dwRegisterClass;

        // Tell COM that we'll create these Components
        if (SUCCEEDED(CoRegisterClassObject(g_clsidHWEventHandlerSample2,
            &factory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE,
            &dwRegisterClass))
        {            
        // Regular WinMain code goes here
            
            CoRevokeClassObject(dwRegisterClass);
        }
        CoUninitialize();
    }
    •••
Figure 11 Volume Predefined EventHandlers

HandleCDBurningOnArrival
Blank CD (CD-R/RW media) arrival
ShowPicturesOnArrival
For BMPs, JPGs, and so on
PlayMusicFilesOnArrival
For WMAs, MP3s, and so on
PlayVideoFilesOnArrival
For MPEGs and other video files
PlayCDAudioOnArrival
Audio CD, REDBOOK format CD with Audio tracks
PlayDVDMovieOnArrival
For movie DVDs
Figure 12 Registering a COM Component in the ROT
CQueryCancelAutoplay* pQCA = new CQueryCancelAutoplay();

if (pQCA)
{
    // Initialize to know what events to cancel
    hr = pQCA->Init(pszDrive, dwContentType);

    if (SUCCEEDED(hr))
    {
        IUnknown* punk;
            
        hr = pQCA->QueryInterface(IID_IUnknown, (void**)&punk);

        if (SUCCEEDED(hr))
        {
            hr = prot->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE,
                                punk, pmoniker, pdwRegisterROT);

            •••
Figure 13 Content Types
ARCONTENT_AUTORUNINF  	0x00000002
ARCONTENT_AUDIOCD		0x00000004
ARCONTENT_DVDMOVIE     	0x00000008
ARCONTENT_BLANKCD      	0x00000010
ARCONTENT_BLANKDVD     	0x00000020
ARCONTENT_UNKNOWNCONTENT	0x00000040
ARCONTENT_AUTOPLAYPIX    	0x00000080
ARCONTENT_AUTOPLAYMUSIC  	0x00000100
ARCONTENT_AUTOPLAYVIDEO  	0x00000200
Figure 14 IHWEventHandler Interface
[
    object,
    uuid(C1FB73D0-EC3A-4ba2-B512-8CDB9187B6D1),
    pointer_default(unique)
]
interface IHWEventHandler : IUnknown
{
    HRESULT Initialize([in, string] LPCWSTR pszParams);
    HRESULT HandleEvent([in, string] LPCWSTR pszDeviceID,
        [in, string] LPCWSTR pszAltDeviceID,
        [in, string] LPCWSTR pszEventType);
    HRESULT HandleEventWithContent([in, string] LPCWSTR pszDeviceID,
        [in, string] LPCWSTR pszAltDeviceID,
        [in, string] LPCWSTR pszEventType,
        [in, string] LPCWSTR pszContentTypeHandler,
        [in] IDataObject* pdataobject);
};