Microsoft DirectX 9.0

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);
 
if (FAILED(hr))
{
  ErrorExit(hr);  // Add error-handling code.
}

You can then load an object from a 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 example 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]);
  if (FAILED(hr))
  {
    ErrorExit(hr);  // Add error-handling code.
  }
}

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

HRESULT LoadStyle(IDirectMusicStyle8 **ppStyle, IDirectMusicLoader8 *pLoader)
{
  if (pLoader)
  {
    DMUS_OBJECTDESC objDesc;
 
    // Start by initializing objDesc with the file name and 
    // class GUID for the style object.
 
    wcsncpy(objDesc.wszFileName, L"c:\\mymusic\\polka.sty",
        sizeof(objDesc.wszName) - 1);
    objDesc.wszFileName[sizeof(objDesc.wszFileName) - 1] = 0;
    objDesc.guidClass = CLSID_DirectMusicStyle; 
    objDesc.dwSize = sizeof (DMUS_OBJECTDESC);
    objDesc.dwValidData = DMUS_OBJ_CLASS | 
        DMUS_OBJ_FILENAME | 
        DMUS_OBJ_FULLPATH;
 
    return pLoader->GetObject(&objDesc, IID_IDirectMusicStyle8,
        (void **) ppStyle);
  }
  else return E_INVALIDARG;
}

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 wszName, guidObject, or wszFileName in 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