Click here to Skip to main content
15,867,330 members
Articles / Desktop Programming / ATL

Burning CD/DVD Media with the Image Mastering API Version 2.0 (IMAPI2)

Rate me:
Please Sign up or sign in to vote.
4.85/5 (78 votes)
12 Dec 2009CPOL5 min read 537K   16.5K   222   146
Example of a CD/DVD burning application using IMAPI2.

Image 1

Introduction

If you are looking for a sample in C#/.NET, then check out my article: "Burning and Erasing CD/DVD Media with C# and IMAPI2".

Windows introduced the new IMAPIv2.0 with the release of the Vista Operating System, which was a big improvement over the original IMAPI. The original IMAPI is great for CDROMs, but it has some huge limitations like not being able to write to DVD media. I am sure this limitation is due to almost nobody having a DVD writer when Windows XP was released back in 2001. IMAPIv2 allows you to write to CD and DVD media, as well as read and write ISO files. IMAPIv2.0 had a problem since it was only available with Windows Vista. But in June of 2007, Microsoft released update packages for Windows XP and Windows 2003. You can download the updates here.

You will also need to download and install the Microsoft Windows Software Development Kit to get the header files necessary to compile the application. You can download the SDK here.

After you download and install the SDK, you will need to make sure the SDK's include directory is in Visual Studio's Include path. When using Visual Studio 2005, you must also make the SDK's LIB path as the first entry in the LIB directory list.

Anybody wanting to develop a full application should also read the "Joliet Recording Specification".

Using the Code

I have created several wrappers around the IMAPIv2 interfaces to assist in managing instances of the interfaces:

CDiscMaster wraps the IDiscMaster2 interface which allows you to determine if the computer has any optical devices installed, and if so, allows you to enumerate the CD and DVD drives installed on the computer.

CDiscRecorder wraps the IDiscRecorder2 interface which represents each physical drive. You use this interface to retrieve information about the drive including manufacturer information, logical drive, and supported media.

CDiscFormatData wraps the IDiscFormat2Data interface which is used to write data to the media.

CDiscFormatDataEvent wraps the DDiscFormat2DataEvents notifications which is used to receive the status of the IDiscFormat2Data write function.

For the file system, I created a base class CBaseObject which has three functions: GetPath(), GetName(), and GetSizeOnDisc(). GetPath returns the full path of the file or directory on the computer. GetName returns just the filename of the file or the directory which is used for display purposes, and is also used as the name of the file or directory in the root directory of the recorded image. GetSizeOnDisc is a pure virtual function that the two classes that are derived from CBaseObject, CFileObject, and CDirObject, implement. CFileObject returns the size the file will use on the media. CDirObject returns the size of all files and subdirectories of the directory.

I will cover a few main points here, but you should download the source code to see all the code.

You will need to include the imapi2 header files in your application:

C#
#include <imapi2.h>
#include <imapi2error.h>
#include <imapi2fs.h>
#include <imapi2fserror.h>

imapi2.h and imapi2error.h are needed for the imapi2 interfaces. imapi2fs.h and imapi2fserror.h are needed for the imapi2 file system interfaces.

Then, I create an instance of the CDiscMaster class, initialize it, and get the unique ID for each device. The unique ID is used to initialize a CDiscRecord object, and I use that to get display information for the combobox entry and store the pointer for the item data.

C#
CDiscMaster discMaster;
.
.
discMaster.Initialize();
.
.
long totalDevices = discMaster.GetTotalDevices();
for (long deviceIndex = 0; deviceIndex < totalDevices; deviceIndex++)
{
 CString recorderUniqueID = discMaster.GetDeviceUniqueID(deviceIndex);
 CDiscRecorder* pDiscRecorder = new CDiscRecorder();
 pDiscRecorder->Initialize(recorderUniqueID);
 //
 // Get the volume path(s). usually just 1
 //
 CString volumeList;
 ULONG totalVolumePaths = pDiscRecorder->GetTotalVolumePaths();
 for (ULONG volIndex = 0; volIndex < totalVolumePaths; volIndex++)
 {
  if (volIndex)
   volumeList += _T(",");
  volumeList += pDiscRecorder->GetVolumePath(volIndex);
 }
 //
 // Add Drive to combo and IDiscRecorder as data
 //
 CString productId = pDiscRecorder->GetProductID();
 CString strName;
 strName.Format(_T("%s [%s]"), (LPCTSTR)volumeList, (LPCTSTR)productId);
 int comboBoxIndex = m_deviceComboBox.AddString(strName);
 m_deviceComboBox.SetItemDataPtr(comboBoxIndex, pDiscRecorder);
}

When an item is selected in the device combobox, I get the CDiscRecorder object of the selected device, which I placed in the item's data. I then use the CDiscRecorder object to get the supported media types. The supported media types return an integer that is defined in the enum IMAPI_MEDIA_PHYSICAL_TYPE type. Since I get values like IMAPI_MEDIA_TYPE_DVDPLUSR and IMAPI_MEDIA_TYPE_DVDDASHR, I have three member variables (m_isCdromSupported, m_isDvdSupported, and m_isDualLayerDvdSupported) that I set to true if any media in the family is supported. I then add these media types to a media type combobox and let users select what type of media they are going to use. I then use a very "rough" estimate to determine how much of the media the user has filled up.

C#
void CBurnCDDlg::OnCbnSelchangeDeviceCombo()
{
  m_isCdromSupported = false;
  m_isDvdSupported = false;
  m_isDualLayerDvdSupported = false;
  m_mediaTypeCombo.ResetContent();
  int selectedIndex = m_deviceComboBox.GetCurSel();
  ASSERT(selectedIndex >= 0);
  if (selectedIndex < 0)
  {
   return;
  }
  CDiscRecorder* discRecorder =
   (CDiscRecorder*)m_deviceComboBox.GetItemDataPtr(selectedIndex);
  if (discRecorder != NULL)
  {
   CDiscFormatData discFormatData;
   if  (!discFormatData.Initialize(discRecorder, CLIENT_NAME))
   {
    return;
   }
   //
   // Display Supported Media Types
   //
   CString supportedMediaTypes;
   ULONG totalMediaTypes = discFormatData.GetTotalSupportedMediaTypes();
   for (ULONG volIndex = 0; volIndex < totalMediaTypes; volIndex++)
   {
    int mediaType = discFormatData.GetSupportedMediaType(volIndex);
    if (volIndex > 0)
     supportedMediaTypes += _T(", ");
    supportedMediaTypes += GetMediaTypeString(mediaType);
   }
   m_supportedMediaTypes.SetWindowText(supportedMediaTypes);
   //
   // Add Media Selection
   //
   if (m_isCdromSupported)
   {
    int stringIndex = m_mediaTypeCombo.AddString(_T("700MB CD Media"));
    m_mediaTypeCombo.SetItemData(stringIndex, CD_MEDIA);
   }
   if (m_isDvdSupported)
   {
     int stringIndex = m_mediaTypeCombo.AddString(_T("4.7GB DVD Media"));
     m_mediaTypeCombo.SetItemData(stringIndex, DVD_MEDIA);
   }
   if (m_isDualLayerDvdSupported)
   {
    int stringIndex = m_mediaTypeCombo.AddString(_T("8.5GB Dual-Layer DVD"));
    m_mediaTypeCombo.SetItemData(stringIndex, DL_DVD_MEDIA);
   }
   m_mediaTypeCombo.SetCurSel(0);
   OnCbnSelchangeMediaTypeCombo();
  }
}

When the user adds a file to the list, I create a CFileObject and I add it to the file listbox. I then call the UpdateCapacity function which calculates the total storage required by all the items in the file listbox and update the capacity progress bar.

C#
void CBurnCDDlg::OnBnClickedAddFilesButton()
{
  CFileDialog fileDialog(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, _T
        ("All Files (*.*)|*.*||"), NULL, 0);
  if (fileDialog.DoModal() == IDOK)
  {
    CFileObject* pFileObject = new CFileObject(fileDialog.GetPathName());
    int addIndex = m_fileListbox.AddString(pFileObject->GetName());
    m_fileListbox.SetItemDataPtr(addIndex, pFileObject);
    UpdateCapacity();
    EnableBurnButton();
  }
}

When the user adds a folder to the list, I create a CDirObject and I add it to the file listbox. Just like the file object, I call the UpdateCapacity function to calculate the total storage required by all the items in the file listbox and update the capacity progress bar.

C#
void CBurnCDDlg::OnBnClickedAddFolderButton()
{
  BROWSEINFO bi = {0};
  bi.hwndOwner = m_hWnd;
  bi.ulFlags = BIF_RETURNONLYFSDIRS|BIF_USENEWUI;
  LPITEMIDLIST lpidl = SHBrowseForFolder(&bi);
  if (!lpidl)
    return;
  TCHAR selectedPath[_MAX_PATH] = {0};
  if (SHGetPathFromIDList(lpidl, selectedPath))
  {
    CDirObject* pDirObject = new CDirObject(selectedPath);
    int addIndex = m_fileListbox.AddString(pDirObject->GetName());
    m_fileListbox.SetItemDataPtr(addIndex, pDirObject);
    UpdateCapacity();
    EnableBurnButton();
  }
}

When the user presses the Burn button, I disable the user interface and launch another thread, BurnThread, to perform the burn. This will keep the UI responsive during the burn process.

C#
void CBurnCDDlg::OnBnClickedBurnButton()
{
  if (m_isBurning)
  {
    SetCancelBurning(true);
  }
  else
  {
    SetCancelBurning(false);
    m_isBurning = true;
    UpdateData();
    EnableUI(false);
    AfxBeginThread(BurnThread, this, THREAD_PRIORITY_NORMAL);
  }
}
UINT CBurnCDDlg::BurnThread(LPVOID pParam)
{
  IStream* dataStream = NULL;
  CBurnCDDlg* pThis = (CBurnCDDlg*)pParam;
  if (!CreateMediaFileSystem(pThis, &dataStream))
  { // CreateMediaFileSystem reported error to UI
   return false;
  }
  //
  // Get the selected recording device from the combobox
  //
  int selectedIndex = pThis->m_deviceComboBox.GetCurSel();
  ASSERT(selectedIndex >= 0);
  if (selectedIndex < 0)
  {
    pThis->SendMessage(WM_BURN_FINISHED, 0, 
      (LPARAM)_T("Error: No Device Selected"));
    return 0;
  }
  CDiscRecorder* pOrigDiscRecorder =
    (CDiscRecorder*)pThis->m_deviceComboBox.GetItemDataPtr(selectedIndex);
  if (pOrigDiscRecorder == NULL)
  {
    //
    // This should never happen
    //
    pThis->SendMessage(WM_BURN_FINISHED, 0,
        (LPARAM)_T("Error: No Data for selected device"));
    return 0;
  }
  //
  // Did user cancel?
  //
  if (pThis->GetCancelBurning())
  {
    pThis->SendMessage(WM_BURN_FINISHED, 0, (LPARAM)_T("User Canceled!"));
    return 0;
  }
  pThis->SendMessage(WM_BURN_STATUS_MESSAGE, 0,
        (LPARAM)_T("Initializing Disc Recorder..."));
  //
  // Create another disc recorder because we're in a different thread
  //
  CDiscRecorder discRecorder;
  CString errorMessage;
  if (discRecorder.Initialize(pOrigDiscRecorder->GetUniqueId()))
  {
    //
    //
    //
    if (discRecorder.AcquireExclusiveAccess(true, CLIENT_NAME))
    {
     CDiscFormatData discFormatData;
     if (discFormatData.Initialize(&discRecorder, CLIENT_NAME))
     {
      discFormatData.SetCloseMedia(pThis->m_closeMedia ? true : false);
      //////////////////////////////
      //
      // Burn the media here
      //
      discFormatData.Burn(pThis->m_hWnd, dataStream);
      //
      // Release the IStream after burning
      //
      dataStream->Release();
      //
      // Eject Media if they chose
      //
      if (pThis->m_ejectWhenFinished)
      {
       discRecorder.EjectMedia();
      }
     }
     discRecorder.ReleaseExclusiveAccess();
     //
     // Finished Burning, GetHresult will determine if it was successful or not
     //
     pThis->SendMessage(WM_BURN_FINISHED, discFormatData.GetHresult(),
      (LPARAM)(LPCTSTR)discFormatData.GetErrorMessage());
    }
    else
    {
     errorMessage.Format(_T("Failed: %s is exclusive owner"),
      (LPCTSTR)discRecorder.ExclusiveAccessOwner());
     pThis->SendMessage(WM_BURN_FINISHED, discRecorder.GetHresult(),
      (LPARAM)(LPCTSTR)errorMessage);
    }
   }
   else
   {
    errorMessage.Format(_T("Failed to initialize recorder - Unique ID:%s"),
     (LPCTSTR)pOrigDiscRecorder->GetUniqueId());
    pThis->SendMessage(WM_BURN_FINISHED, discRecorder.GetHresult(),
     (LPARAM)(LPCTSTR)errorMessage);
   }
   return 0;
}

UI Notifications

The worker thread communicates with the UI via SendMessage commands. I send Status Messages (WM_BURN_STATUS_MESSAGE) and Burn Finished (WM_BURN_FINISHED) messages from the worker thread to the UI.

I send event notifications to the UI with a WM_IMAPI_UPDATE message from the CDiscFormatDataEvent::Update function. I create an instance of the CDiscFormatDataEvent class, which implements the DDiscFormat2DataEvents interface, in the CDiscFormatData::Burn function. When it receives an event, it gets the data and sends it to the UI so it can update the status.

History

  • v1 - Dec 29, 2007

    • Initial release.
  • v2 - Jan 8, 2008

    • Converted project from Visual Studio 2008 to Visual Studio 2005.
    • Removed ATL from the CDiscFormatDataEvent class.
    • Sample application statically linked with MFC DLLs.

    v3 - Jan 16, 2008

    • Bug fix - Not setting image to size of media.
    • Supports extra large files. Thanks Dale Stewart.
    • Does not assert when InitializeDiscRecorder fails because of Virtual CDROMs, etc.
  • v4 - Dec 12, 2009

    • Updated the link to the latest Windows SDK.
    • Fixed a leak where an IStream wasn't being released.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Thank you for voting on my articles!

MCSD.NET in C#


Comments and Discussions

 
Questionbuggy blu-ray support? Pin
Member 1102545817-Mar-17 12:14
Member 1102545817-Mar-17 12:14 
QuestionGUI Pin
Member 128805479-Dec-16 8:58
Member 128805479-Dec-16 8:58 
AnswerRe: GUI Pin
Member 1102545817-Mar-17 10:48
Member 1102545817-Mar-17 10:48 
QuestionFile system question Pin
Member 116480644-May-15 22:54
Member 116480644-May-15 22:54 
AnswerRe: File system question Pin
DaveCamp6-Oct-15 5:36
DaveCamp6-Oct-15 5:36 
GeneralRe: File system question Pin
Member 1102545817-Mar-17 10:49
Member 1102545817-Mar-17 10:49 
QuestionIs there any library for Java like Imapi Pin
Member 109670648-Jan-15 4:31
Member 109670648-Jan-15 4:31 
Questioncreate bootable cd/dvd Pin
saeed karimi8-Nov-14 21:59
saeed karimi8-Nov-14 21:59 
SuggestionOne read and the other IDiscFormat2Data.Write Pin
jiangyong62842211-Jun-14 18:00
jiangyong62842211-Jun-14 18:00 
Bugmany thanks!! Pin
Member 81519295-Jun-14 0:22
Member 81519295-Jun-14 0:22 
Questionwhy i am Not able to burn DVD-RW in next session? Pin
BK Malhotra15-Jul-13 4:00
BK Malhotra15-Jul-13 4:00 
Questionusing Source Code for GNU GPL Projects Pin
SepBen29-May-13 23:33
SepBen29-May-13 23:33 
AnswerRe: using Source Code for GNU GPL Projects Pin
Eric Haddan30-May-13 11:03
Eric Haddan30-May-13 11:03 
Questionerror-code-0x80004005 Pin
Daryappa22-May-13 21:55
Daryappa22-May-13 21:55 
While writing/burning data to disc, i have encountered error-code-0x80004005. After this error my DVD is no more usable. System goes in infinite loop when I click the DVD drive and can not access it. Please let me know what to do to avoid this error.

Thanks,
Daryappa
QuestionRe: error-code-0x80004005 Pin
ashag8716-Dec-15 23:00
ashag8716-Dec-15 23:00 
QuestionStash file Pin
Member 974141211-Apr-13 23:37
Member 974141211-Apr-13 23:37 
QuestionError Readable CD or dvd for burning data again Pin
gauri_21vaidya23-Aug-12 21:26
gauri_21vaidya23-Aug-12 21:26 
QuestionWindows 7 with IMAPI2 Pin
kayaker19611-Feb-12 8:50
kayaker19611-Feb-12 8:50 
QuestionWrite Speed Pin
Member 849723420-Dec-11 2:49
Member 849723420-Dec-11 2:49 
QuestionBorland 6.0 Imapi2 Pin
stankomix122-Nov-11 23:26
stankomix122-Nov-11 23:26 
QuestionError 0x80040154 Pin
Member 79192399-Nov-11 10:39
Member 79192399-Nov-11 10:39 
GeneralUnable to Initialize IDiscMaster2 - Error:0x800401f0 Pin
PraChauhan20-Apr-11 20:13
PraChauhan20-Apr-11 20:13 
GeneralRe: Unable to Initialize IDiscMaster2 - Error:0x800401f0 Pin
WebMaster8-May-11 12:06
WebMaster8-May-11 12:06 
GeneralRe: Unable to Initialize IDiscMaster2 - Error:0x800401f0 Pin
jiachangyu08-Feb-12 22:54
jiachangyu08-Feb-12 22:54 
GeneralRe: Unable to Initialize IDiscMaster2 - Error:0x800401f0 Pin
jiachangyu08-Feb-12 22:55
jiachangyu08-Feb-12 22:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.