Platform SDK: DirectX

Loading an Object from a Resource

[C++]

Loading an object from a resource, or from some other location in memory, is done like loading an object from a file. (See Loading an Object from a File.) In this case, however, the wszName, guidObject, and wszFileName members of the DMUS_OBJECTDESC structure are irrelevant. Instead, you must obtain a pointer to the block of memory occupied by the object, and its size, and put these in the pbMemData and llMemLength members respectively, of the DMUS_OBJECTDESC structure. You must also set the DMUS_OBJ_MEMORY flag in the dwFlags member.

The memory cannot be released once IDirectMusicLoader::GetObject has been called because the loader keeps the pointer to the memory internally to facilitate caching data. If you want to clear it out, call IDirectMusicLoader::SetObject with the same DMUS_OBJECTDESC descriptor, but with NULL in pbMemData. This is not an issue when loading from a resource because resource memory is not freed.

The following code example loads a MIDI file from a resource into a segment:

HRESULT LoadMidi(HMODULE hMod, WORD ResourceID)
{
    HRESULT              hr;
    DMUS_OBJECTDESC      ObjDesc;
    IDirectMusicSegment* pSegment = NULL; 
 
    HRSRC hFound = FindResource(hMod, 
            MAKEINTRESOURCE(ResourceID), RT_RCDATA);
    HGLOBAL hRes = LoadResource(hMod, hFound);
 
    ObjDesc.dwSize = sizeof(DMUS_OBJECTDESC);
    ObjDesc.guidClass = CLSID_DirectMusicSegment;
    ObjDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_MEMORY;
    ObjDesc.pbMemData = (BYTE *) LockResource(hRes);
    ObjDesc.llMemLength = SizeofResource(hMod, hFound);
 
    hr = m_pDXPerformance->m_pLoader->GetObject(
            &ObjDesc, IID_IDirectMusicSegment2, 
            (void**) &m_pSegment );
 
    return hr;
}

Objects referred to by other objects must be loaded first. For example, if you load a segment that contains a reference to a style, the style must already be loaded for the segment to play correctly. You can also call DirectMusicLoader::SetObject on the style so that the segment can find it.


[Visual Basic]

Loading an object from a resource, or from some other location in memory, is done like loading an object from a file. (See Loading an Object from a File.) The DirectMusicLoader.LoadBandFromResource, DirectMusicLoader.LoadChordMapFromResource, DirectMusicLoader.LoadCollectionFromResource, DirectMusicLoader.LoadSegmentFromResource, and DirectMusicLoader.LoadStyleFromResource methods each take a module name and resource identifier as parameters and return an instance of the appropriate class.

The following resource types are recognized by the loader:

"DMBAND" LoadBandFromResource
"DMCHORD" LoadChordmapFromResource
"DMCOLL" LoadCollectionFromResource
"DMSEG" LoadSegmentFromResource
"DMSTYLE" LoadStyleFromResource

Objects referred to by other objects must be loaded first. For example, if you load a segment that contains a reference to a style, the style must be loaded first for the segment to play correctly.

The following code example, in which loader is the DirectMusicLoader object and perf is the DirectMusicPerformance, loads and plays a MIDI file stored as a "DMSEG" resource in the executable:

Dim seg As DirectMusicSegment
Set seg = loader.LoadSegmentFromResource("listen.exe", "CANYON.MID")
Call seg.Download(perf)
Call perf.PlaySegment(SEG, 0, 0)