Microsoft DirectX 8.1 (C++)

Loading an Object from a File

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

IDirectMusicLoader8* g_pLoader;
 
CoInitializeEx(NULL, 0);
HRESULT hr = CoCreateInstance(
  CLSID_DirectMusicLoader,
  NULL,
  CLSCTX_INPROC, 
  IID_IDirectMusicLoader8,
  (void**)&g_pLoader);

You can then load an object from file by using either IDirectMusicLoader8::LoadObjectFromFile or IDirectMusicLoader8::GetObject. The first of these methods is more convenient because it does not require you to describe the object by filling out a DMUS_OBJECTDESC structure.

The following sample code loads four segments from a directory previously set by IDirectMusicLoader8::SetSearchDirectory:

IDirectMusicSegment8 * g_pSegments[4];
 
static WCHAR  wszNames[4][MAX_PATH] = {
  L"AudioPath1.sgt",
  L"AudioPath2.sgt",
  L"AudioPath3.wav",
  L"AudioPath4.sgt"
};
 
for (DWORD dwIndex = 0; dwIndex < 4; dwIndex++)
{
  hr = g_pLoader->LoadObjectFromFile(
    CLSID_DirectMusicSegment,
    IID_IDirectMusicSegment8,
    wszNames[dwIndex],
    (void**) &g_pSegments[dwIndex]);
}

The following example function uses IDirectMusicLoader8::GetObject to load a style object from a file. The first parameter receives a pointer to the style.

void myLoadStyle(
  IDirectMusicStyle8 **ppStyle, IDirectMusicLoader8 *pLoader)
{
  if (pLoader)
  {
    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;
 
    pLoader->GetObject(&Desc, IID_IDirectMusicStyle8,
      (void **) ppStyle);
  }
}

The example identifies the file by a full path name and indicates this by setting the DMUS_OBJ_FULLPATH flag.

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 IDirectMusicLoader8::ScanDirectory method 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.