DiscFormatData.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ///////////////////////////////////////////////////////////////////////
  2. // DiscFormatData.cpp
  3. //
  4. // Wrapper for IDiscFormat2Data Interface
  5. //
  6. // Written by Eric Haddan
  7. //
  8. #include "StdAfx.h"
  9. #include "DiscFormatData.h"
  10. #include "DiscRecorder.h"
  11. #include "DiscFormatDataEvent.h"
  12. CDiscFormatData::CDiscFormatData(void)
  13. : m_discFormatData(NULL)
  14. , m_mediaTypesArray(NULL)
  15. , m_supportedSpeeds(NULL)
  16. , m_hResult(0)
  17. , m_hNotificationWnd(NULL)
  18. , m_closeMedia(true)
  19. {
  20. }
  21. CDiscFormatData::~CDiscFormatData(void)
  22. {
  23. if (m_discFormatData != NULL)
  24. {
  25. m_discFormatData->Release();
  26. }
  27. }
  28. ///////////////////////////////////////////////////////////////////////
  29. //
  30. // CDiscFormatData::Initialize()
  31. //
  32. // Description:
  33. // Creates and initializes the IDiscFormat2Data interface
  34. //
  35. bool CDiscFormatData::Initialize(CDiscRecorder* pDiscRecorder, const CString& clientName)
  36. {
  37. ASSERT(m_discFormatData == NULL);
  38. ASSERT(pDiscRecorder != NULL);
  39. if (pDiscRecorder == NULL)
  40. {
  41. m_errorMessage = _T("Error - CDiscFormatData::Initialize - pDiscRecorder is NULL");
  42. return false;
  43. }
  44. //
  45. // Initialize the IDiscFormat2Data Interface
  46. //
  47. m_hResult = CoCreateInstance(__uuidof(MsftDiscFormat2Data), NULL, CLSCTX_INPROC_SERVER,
  48. __uuidof(IDiscFormat2Data), (void**)&m_discFormatData);
  49. if (!SUCCEEDED(m_hResult))
  50. {
  51. m_errorMessage.Format(_T("Unable to Initialize IDiscFormat2Data - Error:0x%08x"), m_hResult);
  52. return false;
  53. }
  54. //
  55. // Setup the Disc Format Information
  56. //
  57. VARIANT_BOOL isSupported = VARIANT_FALSE;
  58. m_hResult = m_discFormatData->IsRecorderSupported(pDiscRecorder->GetInterface(), &isSupported);
  59. if (isSupported == VARIANT_FALSE)
  60. {
  61. m_errorMessage = _T("Recorder not supported");
  62. return false;
  63. }
  64. m_hResult = m_discFormatData->put_Recorder(pDiscRecorder->GetInterface());
  65. if (!SUCCEEDED(m_hResult))
  66. {
  67. m_errorMessage.Format(_T("IDiscFormat2Data->put_Recorder Failed - Error:0x%08x"), m_hResult);
  68. return false;
  69. }
  70. m_hResult = m_discFormatData->put_ClientName(clientName.AllocSysString());
  71. if (!SUCCEEDED(m_hResult))
  72. {
  73. m_errorMessage.Format(_T("IDiscFormat2Data->put_ClientName Failed - Error:0x%08x"), m_hResult);
  74. return false;
  75. }
  76. // 检索刻录机支持的媒体类型;
  77. m_hResult = m_discFormatData->get_SupportedMediaTypes(&m_mediaTypesArray);
  78. if (!SUCCEEDED(m_hResult))
  79. {
  80. m_errorMessage.Format(_T("IDiscFormat2Data->get_SupportedMediaTypes Failed - Error:0x%08x"), m_hResult);
  81. return false;
  82. }
  83. // 检索当前刻录机或当前媒体支持的写入速度列表;
  84. m_hResult = m_discFormatData->get_SupportedWriteSpeeds(&m_supportedSpeeds);
  85. if (!SUCCEEDED(m_hResult))
  86. {
  87. m_errorMessage.Format(_T("IDiscFormat2Data->get_SupportedWriteSpeeds Failed - Error:0x%08x"), m_hResult);
  88. return false;
  89. }
  90. return true;
  91. }
  92. ULONG CDiscFormatData::GetTotalSupportedMediaTypes()
  93. {
  94. if (m_mediaTypesArray == NULL)
  95. return 0;
  96. return m_mediaTypesArray->rgsabound[0].cElements;
  97. }
  98. int CDiscFormatData::GetSupportedMediaType(ULONG index)
  99. {
  100. ASSERT(index < GetTotalSupportedMediaTypes());
  101. if (index < GetTotalSupportedMediaTypes())
  102. {
  103. if (m_mediaTypesArray)
  104. {
  105. return ((VARIANT*)(m_mediaTypesArray->pvData))[index].intVal;
  106. }
  107. }
  108. return 0;
  109. }
  110. ULONG CDiscFormatData::GetTotalSupportedWriteSpeeds()
  111. {
  112. if (m_supportedSpeeds == NULL)
  113. return 0;
  114. return m_supportedSpeeds->rgsabound[0].cElements;
  115. }
  116. int CDiscFormatData::GetSupportedWriteSpeed(ULONG index)
  117. {
  118. ASSERT(index < GetTotalSupportedWriteSpeeds());
  119. if ( index < GetTotalSupportedWriteSpeeds())
  120. {
  121. if (m_supportedSpeeds)
  122. return ((VARIANT*)(m_supportedSpeeds->pvData))[index].ulVal; // 每秒写入的扇区数;
  123. }
  124. return 0;
  125. }
  126. bool CDiscFormatData::SetWriteSpeed(ULONG index)
  127. {
  128. ASSERT(index < GetTotalSupportedWriteSpeeds());
  129. if ( index < GetTotalSupportedWriteSpeeds())
  130. {
  131. if (m_supportedSpeeds)
  132. m_discFormatData->SetWriteSpeed(((VARIANT*)(m_supportedSpeeds->pvData))[index].ulVal, VARIANT_FALSE);
  133. }
  134. return false;
  135. }
  136. /*
  137. bool CDiscFormatData::SetBurnVerification(IMAPI_BURN_VERIFICATION_LEVEL VerificationLevel)
  138. {
  139. HRESULT hr = S_OK;
  140. IBurnVerification *burnVerifier = NULL;
  141. hr = m_discFormatData->QueryInterface(IID_PPV_ARGS(&burnVerifier));
  142. if (SUCCEEDED(hr))
  143. {
  144. hr = burnVerifier->put_BurnVerificationLevel(VerificationLevel);
  145. }
  146. if (burnVerifier != NULL)
  147. {
  148. burnVerifier->Release();
  149. burnVerifier = NULL;
  150. }
  151. return hr;
  152. }*/
  153. bool CDiscFormatData::Burn(HWND hNotificationWnd, IStream* streamData)
  154. {
  155. if (m_discFormatData == NULL)
  156. return false;
  157. if (hNotificationWnd == NULL)
  158. return false;
  159. if (streamData == NULL)
  160. return false;
  161. m_streamData = streamData;
  162. m_hNotificationWnd = hNotificationWnd;
  163. // Create the event sink
  164. CDiscFormatDataEvent* eventSink = CDiscFormatDataEvent::CreateEventSink();
  165. if (eventSink == NULL)
  166. {
  167. m_errorMessage = _T("Unable to create event sink");
  168. return false;
  169. }
  170. if (!eventSink->ConnectDiscFormatData(this))
  171. {
  172. m_errorMessage = _T("Unable to connect event sink with interface");
  173. return false;
  174. }
  175. eventSink->SetHwnd(m_hNotificationWnd);
  176. m_discFormatData->put_ForceMediaToBeClosed(m_closeMedia ? VARIANT_TRUE : VARIANT_FALSE);
  177. m_hResult = m_discFormatData->Write(m_streamData);
  178. delete eventSink;
  179. if (SUCCEEDED(m_hResult))
  180. {
  181. return true;
  182. }
  183. m_errorMessage.Format(_T("IDiscFormat2Data->Write Failed! Error:0x%08x"),
  184. m_hResult);
  185. return true;
  186. }
  187. /************************************************************************/
  188. /* 函数:获取媒体总扇区大小(不是空闲扇区大小)[3/20/2018 Jeff];
  189. /* 描述:;
  190. /* 参数:;
  191. /* [IN] :;
  192. /* [OUT] :;
  193. /* [IN/OUT] :;
  194. /* 返回:void;
  195. /* 注意:;
  196. /* 示例:;
  197. /*
  198. /* 修改:;
  199. /* 日期:;
  200. /* 内容:;
  201. /************************************************************************/
  202. bool CDiscFormatData::GetFreeSectorsOnMedia(LONG& lFreeSectorOnMedia)
  203. {
  204. if (m_discFormatData == NULL)
  205. return false;
  206. HRESULT hr = m_discFormatData->get_FreeSectorsOnMedia(&lFreeSectorOnMedia);
  207. if (SUCCEEDED(hr))
  208. {
  209. lFreeSectorOnMedia *= 2;
  210. lFreeSectorOnMedia /= 1024;
  211. //lFreeSectorOnMedia /= 1024;
  212. return true;
  213. }
  214. return false;
  215. }