如果需要在windows平台上开发有关光盘刻录的应用,调用微软Imapi2的接口即可实现。
例子代码下载链接:http://download.csdn.net/detail/itmes/4454080
Imapi2是以com接口的方式提供的,而不是类似windows api 的c语言函数式接口,因此和 api编程稍有不同,但com接口的好处是可以同时支持多种编程语言,c++,c#,vb,vbscript等等都可以直接调用。
如下是实现光盘擦除的C++代码,代码中还隐藏着一些错误,仅支持单台刻录设备,这段代码仅仅是为了说明Imapi2最基本的使用。
// imapitest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//必须引用的头文件
#include <imapi2.h>
#include <imapi2error.h>
#include <imapi2fs.h>
#include <imapi2fserror.h>
#include <shlwapi.h> // for SHCreateStreamOnFileEx
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitializeEx(NULL, COINIT_MULTITHREADED); //Com环境初始化,COINIT_MULTITHREADED允许在多个线程中使用COM对象
CString m_errorMessage;
HRESULT m_hResult;
IDiscMaster2* m_discMaster; //声明IDiscMaster2 接口
m_hResult = CoCreateInstance(__uuidof(MsftDiscMaster2), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IDiscMaster2), (void**)&m_discMaster); //初始化一个IDiscMaster2的实例
if (!SUCCEEDED(m_hResult))
{
m_errorMessage.Format(_T("Unable to Initialize IDiscMaster2 - Error:0x%08x"), m_hResult);
return false;
}
VARIANT_BOOL isSupported = VARIANT_FALSE;
m_hResult = m_discMaster->get_IsSupportedEnvironment(&isSupported); //获得当前环境(硬件、用户权限)是否允许刻录操作
if (!SUCCEEDED(m_hResult))
{
m_errorMessage.Format(_T("IDiscMaster2->get_IsSupportedEnvironment failed! - Error:0x%08x"), m_hResult);
return false;
}
if (isSupported == VARIANT_FALSE)
{
m_errorMessage = _T("There were no writable devices detected!");
return false;
}
long totalDevices = 0;
m_hResult = m_discMaster->get_Count(&totalDevices); //获得当前环境下有多少光驱设备
if (FAILED(m_hResult))
{
m_errorMessage.Format(_T("IDiscMaster2->get_Count failed! - Error:0x%08x"), m_hResult);
return 0;
}
IDiscRecorder2* m_discRecorder;//声明一个IDiscRecorder2 接口
for (long deviceIndex = 0; deviceIndex < totalDevices; deviceIndex++)
{
CString recorderUniqueID ;
BSTR uniqueID = NULL;
m_hResult = m_discMaster->get_Item(deviceIndex, &uniqueID); //获得某个光驱设备的唯一描述字符串
if (FAILED(m_hResult))
{
m_errorMessage.Format(_T("IDiscMaster2->get_Item(%d) failed! - Error:0x%08x"),
deviceIndex, m_hResult);
}
recorderUniqueID= uniqueID;
if (recorderUniqueID.IsEmpty())
{
continue;
}
m_hResult = CoCreateInstance(__uuidof(MsftDiscRecorder2), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IDiscRecorder2), (void**)&m_discRecorder); //初始化一个IDiscRecorder2 实例
ASSERT(SUCCEEDED(m_hResult));
if (FAILED(m_hResult))
{
return false;
}
m_hResult = m_discRecorder->InitializeDiscRecorder(recorderUniqueID.AllocSysString()); //用前面获得的光驱设备的唯一描述字串对IDiscRecorder2 接口进行初始化,可以理解为将IDiscRecorder2 接口与特定的光驱设备进行绑定
if (FAILED(m_hResult))
{
return false;
}
}
IDiscFormat2Erase *iErase;
m_hResult = CoCreateInstance(__uuidof(MsftDiscFormat2Erase), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IDiscFormat2Erase) , (void**)&iErase);
if (!SUCCEEDED(m_hResult))
{
m_errorMessage.Format(_T("Unable to Initialize IDiscFormat2Erase - Error:0x%08x"), m_hResult);
return false;
}
m_hResult =iErase->put_Recorder(m_discRecorder);//与IDiscRecorder2 接口绑定
if (!SUCCEEDED(m_hResult))
{
return false;
}
m_hResult =iErase->put_FullErase(VARIANT_FALSE);//是否进行完全擦除(格式化)或者快速擦除
if (!SUCCEEDED(m_hResult))
{
return false;
}
CString clientname = _T("BurnMedia");
m_hResult =iErase->put_ClientName(clientname.AllocSysString());
if (!SUCCEEDED(m_hResult))
{
return false;
}
m_hResult =iErase->EraseMedia();//擦除光盘
if (!SUCCEEDED(m_hResult))
{
return false;
}
wprintf(L"erase disk success~");
m_discRecorder->EjectMedia();//弹出光驱
::CoUninitialize();
return 0;
}