Partager via


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:

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.

© 2004 Microsoft Corporation. All rights reserved.