Microsoft DirectX 8.1 (C++)

Step 2: Load a File

The DirectMusic performance and synthesizer are now ready to process sound data. To get the data, the loader needs to know where to find it. Although a full path can be provided each time a file is loaded, it is more convenient to establish a default directory. Do this by using the IDirectMusicLoader8::SetSearchDirectory method.

In the sample code, the path to the default Windows media directory is given. You can change the value of wstrSearchPath to get files from a different folder.

The following code is from the WinMain function in the tutorial sample:

  // Find the Windows media directory.
 
  CHAR strPath[MAX_PATH];
  GetWindowsDirectory( strPath, MAX_PATH );
  strcat( strPath, "\\media" );
 
 // Convert to Unicode.
 
  WCHAR wstrSearchPath[MAX_PATH];
  MultiByteToWideChar( CP_ACP, 0, strPath, -1, 
         wstrSearchPath, MAX_PATH );
 
  // Set the search directory.
 
  g_pLoader->SetSearchDirectory( 
    GUID_DirectMusicAllTypes, // Types of files sought.
    wstrSearchPath,     // Where to look.
    FALSE         // Don't clear object data.
  );

In the call to SetSearchDirectory, the fClear parameter is set to FALSE because there is no danger of accidentally reloading objects from the wrong directory. This is likely to happen only if the application is loading identically named objects from different folders.

Now that the loader knows where to look for the file, it can load it as a segment:

  WCHAR wstrFileName[MAX_PATH] = L"The Microsoft Sound.wav";
 
  if (FAILED(g_pLoader->LoadObjectFromFile(
    CLSID_DirectMusicSegment, // Class identifier.
    IID_IDirectMusicSegment8, // ID of desired interface.
    wstrFileName,     // Filename.
    (LPVOID*) &g_pSegment   // Pointer that receives interface.
  )))
  {
    MessageBox( NULL, "Media not found, sample will now quit.", 
          "DMusic Tutorial", MB_OK );
    return 0;
  }

Next: Step 3: Play the File