Platform SDK: DirectX

Loading an Object from a File

[C++]

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

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

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

The following code example 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,
   this example creates it dynamically and throws 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 code example identifies the file by a full path name and indicates this 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, 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 to make the GUIDs and names in the current directory available. For more information, see Scanning a Directory for Objects.

[Visual Basic]

To load an object, first create a DirectMusicLoader object. Then, call one of the following methods:

Pass in either a simple file name or a full path for the file that contains the desired object. Each of these methods returns an instance of the appropriate class.

The following code example, in which objDX is a DirectX7 object, loads a segment from a file in the current search directory:

Dim objDMLoader As DirectMusicLoader
Dim objSeg As DirectMusicSegment
 
Set objDMLoader = objDX.DirectMusicLoaderCreate
Set objSeg = objDMLoader.LoadSegment("Myseg.sgt")

See also Loading an Object from a Resource.