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:
#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.
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);
CString volumeList;
ULONG totalVolumePaths = pDiscRecorder->GetTotalVolumePaths();
for (ULONG volIndex = 0; volIndex < totalVolumePaths; volIndex++)
{
if (volIndex)
volumeList += _T(",");
volumeList += pDiscRecorder->GetVolumePath(volIndex);
}
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.
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;
}
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);
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.
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.
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.
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))
{
return false;
}
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)
{
pThis->SendMessage(WM_BURN_FINISHED, 0,
(LPARAM)_T("Error: No Data for selected device"));
return 0;
}
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..."));
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);
discFormatData.Burn(pThis->m_hWnd, dataStream);
dataStream->Release();
if (pThis->m_ejectWhenFinished)
{
discRecorder.EjectMedia();
}
}
discRecorder.ReleaseExclusiveAccess();
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
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.