Setting Objects
Sometimes it is desirable to tell the loader where to get an object, without actually loading that object, so that the loader can retrieve it if the object is later referenced by other objects as they are being loaded. You might also want to give an object a new attribute so that the loader can find it by that attribute.
The IDirectMusicLoader8::SetObject method takes as a parameter a DMUS_OBJECTDESC structure that contains two key pieces of information:
- A pointer to the data. This can be either a file path or a pointer to a block of memory. See Loading an Object from a File and Loading an Object from a Resource or Memory Address.
- An identifier for the object when it is referenced later. This could be a GUID or a name. Later, the call to IDirectMusicLoader8::GetObject will find the stored object by using the same name or GUID. Note that you cannot change a GUID or name that already exists in the object.
On return, the DMUS_OBJECTDESC structure may contain additional information about the object gathered by the loader.
The following function assigns a name to an unnamed object (such as a MIDI file) in a resource:
HRESULT SetObjectFromResource(const GUID* guid, int ID,
char* type, WCHAR* name, IDirectMusicLoader8* pLoader,
HINSTANCE hInstance)
{
HRSRC hResource = NULL;
HGLOBAL hData = NULL;
hResource = FindResource(hInstance, MAKEINTRESOURCE(ID), type);
if (hResource != NULL)
{
hData = LoadResource(hInstance, hResource);
if (hData != NULL)
{
DMUS_OBJECTDESC objDesc;
if(pLoader && (hResource != NULL) && (hData != NULL))
{
ZeroMemory(&objDesc,sizeof(objDesc));
objDesc.pbMemData = (BYTE*) LockResource(hData);
objDesc.llMemLength = SizeofResource(hInstance, hResource);
objDesc.guidClass = (*guid);
objDesc.dwSize = sizeof(objDesc);
objDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_MEMORY;
if (name)
{
wcsncpy(objDesc.wszName, name, sizeof(objDesc.wszName) - 1);
objDesc.wszName[sizeof(objDesc.wszName) - 1] = 0;
objDesc.dwValidData |= DMUS_OBJ_NAME;
}
return pLoader->SetObject(&objDesc);
}
}
}
return E_FAIL;
}
The example function could be used to assign a name to a MIDI file stored as a resource of type "MIDI", as in the following function call:
SetObjectFromResource(CLSID_DirectMusicSegment, 101,
"MIDI", "canyon", g_pLoader, g_hInstance);
The object can now be loaded at any time by name.