Loading Objects

To load an object, you must first obtain the IDirectMusicLoader interface, as in the following example:

IDirectMusicLoader* m_pLoader;
 
CoInitialize(NULL);
HRESULT hr = CoCreateInstance(
    CLSID_DMLoader,
    NULL,
    CLSCTX_INPROC, 
    IID_IDirectMusicLoader,
    (void**)&m_pLoader);
 

You then describe the object and call the IDirectMusicLoader::GetObject method to load it and obtain the desired interface.

The following example function loads a style from disk and returns a pointer to it in the variable addressed by the parameter.

void myLoadStyle(
    IDirectMusicStyle **ppIStyle)
{
    IDirectMusicLoader *pILoader;        // Loader interface.
 
/* Normally you would create the loader once and use it for the
   duration of the application. This reduces overhead and takes
   advantage of the loader's ability to cache objects. However, for
   purposes of this example, we create it dynamically and throw it
   away once the style is loaded. */
 
    CoCreateInstance(
            CLSID_DirectMusicLoader,NULL,
            CLSCTX_INPROC,
            IID_IDirectMusicLoader,
            (void **) &pILoader);
 
    if (pILoader)
    {
        DMUS_OBJECTDESC Desc;
 
        // Start by initializing Desc with the file name and 
        // class GUID for the style object.
 
        wcscpy(Desc.wszFileName,L"c:\\mymusic\\funky\\polka.sty");
        Desc.guidClass = CLSID_DirectMusicStyle; 
        Desc.dwSize = sizeof (DMUS_OBJECTDESC);
        Desc.dwValidData = DMUS_OBJ_CLASS | 
                           DMUS_OBJ_FILENAME | 
                           DMUS_OBJ_FULLPATH;
 
        pILoader->GetObject(&Desc, IID_IDirectMusicStyle,
                (void **) ppIStyle);
        pILoader->Release();
    }
}
 

This example function identifies the file by a full path name and indicates that it is doing so by setting the DMUS_OBJ_FULLPATH flag. If you have previously set the search directory, you can use the short name of the file without full path information. For an example, see Setting the Loader's Search Directory.

To identify the particular file object being sought you must fill in at least one of the wszName, guidObject, and wszFileName members of the DMUS_OBJECTDESC structure and set the corresponding flag or flags in the dwValidData member. If you identify the file by wszName or guidObject but not by wszFileName, you must first call the IDirectMusicLoader::ScanDirectory method in order to make the GUIDs and names in the current directory available. For more information, see Scanning a Directory for Objects.

See also Loading an Object from a Resource.