| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- #include "StdAfx.h"
- #include "DiscFormatData.h"
- #include "DiscRecorder.h"
- #include "DiscFormatDataEvent.h"
- CDiscFormatData::CDiscFormatData(void)
- : m_discFormatData(NULL)
- , m_mediaTypesArray(NULL)
- , m_supportedSpeeds(NULL)
- , m_hResult(0)
- , m_hNotificationWnd(NULL)
- , m_closeMedia(true)
- {
- }
- CDiscFormatData::~CDiscFormatData(void)
- {
- if (m_discFormatData != NULL)
- {
- m_discFormatData->Release();
- m_discFormatData = NULL;
- }
- }
- ///////////////////////////////////////////////////////////////////////
- //
- // CDiscFormatData::Initialize()
- //
- // Description:
- // Creates and initializes the IDiscFormat2Data interface
- //
- bool CDiscFormatData::Initialize(CDiscRecorder* pDiscRecorder, const CString& clientName)
- {
- ASSERT(m_discFormatData == NULL);
- ASSERT(pDiscRecorder != NULL);
- if (pDiscRecorder == NULL)
- {
- m_errorMessage = _T("Error - CDiscFormatData::Initialize - pDiscRecorder is NULL");
- return false;
- }
- //
- // Initialize the IDiscFormat2Data Interface
- //
- m_hResult = CoCreateInstance(__uuidof(MsftDiscFormat2Data), NULL, CLSCTX_INPROC_SERVER,
- __uuidof(IDiscFormat2Data), (void**)&m_discFormatData);
- if (!SUCCEEDED(m_hResult))
- {
- m_errorMessage.Format(_T("Unable to Initialize IDiscFormat2Data - Error:0x%08x"), m_hResult);
- return false;
- }
- //
- // Setup the Disc Format Information
- //
- m_hResult = m_discFormatData->get_SupportedMediaTypes(&m_mediaTypesArray); // 获取本系统内所有光驱支持的媒体格式 两个光驱则是罗列两个光驱的所有媒体格式
- if (!SUCCEEDED(m_hResult))
- {
- m_errorMessage.Format(_T("IDiscFormat2Data->get_SupportedMediaTypes Failed - Error:0x%08x"), m_hResult);
- return false;
- }
- //SAFEARRAY* pSafeArray;
- m_hResult = pDiscRecorder->GetInterface()->get_SupportedProfiles(&m_mediaTypesArray); // 此时是获取一个光驱的支持媒体格式
- if(!SUCCEEDED(m_hResult))
- {
- m_errorMessage.Format(_T("IDiscRecorder2->get_SupportedProfiles Failed - Error:0x%08x"), m_hResult);
- return false;
- }
- VARIANT_BOOL isSupported = VARIANT_FALSE;
- m_hResult = m_discFormatData->IsRecorderSupported(pDiscRecorder->GetInterface(), &isSupported);
- if (isSupported == VARIANT_FALSE)
- {
- m_errorMessage = _T("Recorder not supported");
- return false;
- }
- m_hResult = m_discFormatData->put_Recorder(pDiscRecorder->GetInterface());
- if (!SUCCEEDED(m_hResult))
- {
- m_errorMessage.Format(_T("IDiscFormat2Data->put_Recorder Failed - Error:0x%08x"), m_hResult);
- return false;
- }
- m_hResult = m_discFormatData->put_ClientName(clientName.AllocSysString());
- if (!SUCCEEDED(m_hResult))
- {
- m_errorMessage.Format(_T("IDiscFormat2Data->put_ClientName Failed - Error:0x%08x"), m_hResult);
- return false;
- }
- // 检索当前刻录机或当前媒体支持的写入速度列表;
- m_hResult = m_discFormatData->get_SupportedWriteSpeeds(&m_supportedSpeeds);
- if (!SUCCEEDED(m_hResult))
- {
- m_errorMessage.Format(_T("IDiscFormat2Data->get_SupportedWriteSpeeds Failed - Error:0x%08x"), m_hResult);
- return false;
- }
- return true;
- }
- ULONG CDiscFormatData::GetTotalSupportedMediaTypes()
- {
- if (m_mediaTypesArray == NULL)
- return 0;
- return m_mediaTypesArray->rgsabound[0].cElements;
- }
- int CDiscFormatData::GetSupportedMediaType(ULONG index)
- {
- ASSERT(index < GetTotalSupportedMediaTypes());
- if (index < GetTotalSupportedMediaTypes())
- {
- if (m_mediaTypesArray)
- {
- return ((VARIANT*)(m_mediaTypesArray->pvData))[index].intVal;
- }
- }
- return 0;
- }
- ULONG CDiscFormatData::GetTotalSupportedWriteSpeeds()
- {
- if (m_supportedSpeeds == NULL)
- return 0;
- return m_supportedSpeeds->rgsabound[0].cElements;
- }
- int CDiscFormatData::GetSupportedWriteSpeed(ULONG index)
- {
- ASSERT(index < GetTotalSupportedWriteSpeeds());
- if ( index < GetTotalSupportedWriteSpeeds())
- {
- if (m_supportedSpeeds)
- return ((VARIANT*)(m_supportedSpeeds->pvData))[index].ulVal; // 每秒写入的扇区数;
- }
- return 0;
- }
- bool CDiscFormatData::SetWriteSpeed(ULONG index)
- {
- ASSERT(index < GetTotalSupportedWriteSpeeds());
- if ( index < GetTotalSupportedWriteSpeeds())
- {
- if (m_supportedSpeeds)
- m_discFormatData->SetWriteSpeed(((VARIANT*)(m_supportedSpeeds->pvData))[index].ulVal, VARIANT_FALSE);
- }
- return false;
- }
- /*
- bool CDiscFormatData::SetBurnVerification(IMAPI_BURN_VERIFICATION_LEVEL VerificationLevel)
- {
- HRESULT hr = S_OK;
- IBurnVerification *burnVerifier = NULL;
- hr = m_discFormatData->QueryInterface(IID_PPV_ARGS(&burnVerifier));
- if (SUCCEEDED(hr))
- {
- hr = burnVerifier->put_BurnVerificationLevel(VerificationLevel);
- }
- if (burnVerifier != NULL)
- {
- burnVerifier->Release();
- burnVerifier = NULL;
- }
- return hr;
- }*/
- bool CDiscFormatData::Burn(HWND hNotificationWnd, IStream* streamData)
- {
- if (m_discFormatData == NULL)
- return false;
- if (streamData == NULL)
- return false;
- m_streamData = streamData;
- m_hNotificationWnd = hNotificationWnd;
- // Create the event sink
- CDiscFormatDataEvent* eventSink = CDiscFormatDataEvent::CreateEventSink();
- if (eventSink == NULL)
- {
- m_errorMessage = _T("Unable to create event sink");
- return false;
- }
- if (!eventSink->ConnectDiscFormatData(this))
- {
- m_errorMessage = _T("Unable to connect event sink with interface");
- return false;
- }
- eventSink->SetHwnd(m_hNotificationWnd);
- m_discFormatData->put_ForceMediaToBeClosed(m_closeMedia ? VARIANT_TRUE : VARIANT_FALSE);
- m_hResult = m_discFormatData->Write(m_streamData);
- delete eventSink;
- if (SUCCEEDED(m_hResult))
- {
- return true;
- }
- m_errorMessage.Format(_T("IDiscFormat2Data->Write Failed! Error:0x%08x"),
- m_hResult);
- return false;
- }
- CString CDiscFormatData::GetWriteErrorMsg(HRESULT hr)
- {
- CString cstrWriteError;
- switch(hr)
- {
- case 0xC0AA020D:
- cstrWriteError = _T("超时");
- break;
- case 0xC0AA02FF:
- cstrWriteError = _T("异常或者数据无效");
- break;
- case 0xC0AA0204:
- cstrWriteError = _T("光盘放颠倒了");
- break;
- case 0xC0AA0205:
- cstrWriteError = _T("准备中,稍后可能需要继续调用Write函数");
- break;
- case 0xC0AA0202:
- cstrWriteError = _T("光驱中无光盘");
- break;
- case 0xC0AA0206:
- cstrWriteError = _T("media正在格式化,请稍等");
- break;
- case 0xC0AA0207:
- cstrWriteError = _T("光驱可能已经长时间工作,现在需要停止工作一会");
- break;
- case 0xC0AA0203:
- cstrWriteError = _T("光盘不兼容或者未知的物理格式");
- break;
- case 0xC0AA0201:
- cstrWriteError = _T("光驱要求的页模式,应用程序未提供");
- break;
- case 0xC0AA0208:
- cstrWriteError = _T("模式页不支持");
- break;
- case 0xC0AA0209:
- cstrWriteError = _T("光盘写保护");
- break;
- case 0xC0AA020F:
- cstrWriteError = _T("写入的速度不匹配");
- break;
- case 6:
- cstrWriteError = _T("句柄无效");
- break;
- case 55:
- cstrWriteError = _T("网络异常或者光驱被卸载");
- break;
- case 0xC0AA0210:
- cstrWriteError = _T("光驱正在被其他的写入独占"); //
- break;
- case 0xC0AA0301:
- cstrWriteError = _T("光驱报告异常");
- break;
- case 0xC0AA0003:
- cstrWriteError = _T("写入动作没有指定光驱");
- break;
- case 0x00AA0005:
- cstrWriteError = _T("要求的旋转时刻录,光驱不支持");
- break;
- case 0x00AA0004:
- cstrWriteError = _T("要求的刻录速度光驱不支持,光驱已自行调整刻录速度");
- break;
- case 0x00AA0006:
- cstrWriteError = _T("要求的旋转刻录和刻录速度不支持,光驱已经自行匹配");
- break;
- case 0xC0AA0407:
- cstrWriteError = _T("光驱不支持光盘的格式");
- break;
- case 0xC0AA0002:
- cstrWriteError = _T("操作取消");
- break;
- case 0xC0AA0400:
- cstrWriteError = _T("另一个写入操作与此操作冲突"); //
- break;
- case 0xC0AA0403:
- cstrWriteError = _T("提供的IStream大小无效.The size must be a multiple of the sector size, 2048.");
- break;
- case 0x80070057:
- cstrWriteError = _T("一个或多个操作无效");
- break;
- case 0x80004003:
- cstrWriteError = _T("指针无效");
- break;
- case 0x80004005:
- cstrWriteError = _T("未知的失败");
- break;
- case 0x8007000E:
- cstrWriteError = _T("内存不足");
- break;
- case 0x80004001:
- cstrWriteError = _T("未知");
- break;
- case 0xC0AA0300:
- cstrWriteError = _T("The write failed because the drive did not receive data quickly enough to continue writing");
- break;
- case 0xC0AA020E:
- cstrWriteError = _T("DVD设备未找到");
- break;
- }
- CString strTemp;
- strTemp .Format(_T(" 错误号:0x%x,"),hr);
- cstrWriteError += strTemp;
- return cstrWriteError;
- }
- /************************************************************************/
- /* 函数:获取媒体总扇区大小(不是空闲扇区大小)[3/20/2018 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- bool CDiscFormatData::GetFreeSectorsOnMedia(LONG& lFreeSectorOnMedia)
- {
- if (m_discFormatData == NULL)
- return false;
- HRESULT hr = m_discFormatData->get_FreeSectorsOnMedia(&lFreeSectorOnMedia);
- if (SUCCEEDED(hr))
- {
- lFreeSectorOnMedia *= 2;
- lFreeSectorOnMedia /= 1024;
- //lFreeSectorOnMedia /= 1024;
- return true;
- }
- return false;
- }
|