DiscFormatData.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "StdAfx.h"
  2. #include "DiscFormatData.h"
  3. #include "DiscRecorder.h"
  4. #include "DiscFormatDataEvent.h"
  5. CDiscFormatData::CDiscFormatData(void)
  6. : m_discFormatData(NULL)
  7. , m_mediaTypesArray(NULL)
  8. , m_supportedSpeeds(NULL)
  9. , m_hResult(0)
  10. , m_hNotificationWnd(NULL)
  11. , m_closeMedia(true)
  12. {
  13. }
  14. CDiscFormatData::~CDiscFormatData(void)
  15. {
  16. if (m_discFormatData != NULL)
  17. {
  18. m_discFormatData->Release();
  19. m_discFormatData = NULL;
  20. }
  21. }
  22. ///////////////////////////////////////////////////////////////////////
  23. //
  24. // CDiscFormatData::Initialize()
  25. //
  26. // Description:
  27. // Creates and initializes the IDiscFormat2Data interface
  28. //
  29. bool CDiscFormatData::Initialize(CDiscRecorder* pDiscRecorder, const CString& clientName)
  30. {
  31. ASSERT(m_discFormatData == NULL);
  32. ASSERT(pDiscRecorder != NULL);
  33. if (pDiscRecorder == NULL)
  34. {
  35. m_errorMessage = _T("Error - CDiscFormatData::Initialize - pDiscRecorder is NULL");
  36. return false;
  37. }
  38. //
  39. // Initialize the IDiscFormat2Data Interface
  40. //
  41. m_hResult = CoCreateInstance(__uuidof(MsftDiscFormat2Data), NULL, CLSCTX_INPROC_SERVER,
  42. __uuidof(IDiscFormat2Data), (void**)&m_discFormatData);
  43. if (!SUCCEEDED(m_hResult))
  44. {
  45. m_errorMessage.Format(_T("Unable to Initialize IDiscFormat2Data - Error:0x%08x"), m_hResult);
  46. return false;
  47. }
  48. //
  49. // Setup the Disc Format Information
  50. //
  51. m_hResult = m_discFormatData->get_SupportedMediaTypes(&m_mediaTypesArray); // 获取本系统内所有光驱支持的媒体格式 两个光驱则是罗列两个光驱的所有媒体格式
  52. if (!SUCCEEDED(m_hResult))
  53. {
  54. m_errorMessage.Format(_T("IDiscFormat2Data->get_SupportedMediaTypes Failed - Error:0x%08x"), m_hResult);
  55. return false;
  56. }
  57. //SAFEARRAY* pSafeArray;
  58. m_hResult = pDiscRecorder->GetInterface()->get_SupportedProfiles(&m_mediaTypesArray); // 此时是获取一个光驱的支持媒体格式
  59. if(!SUCCEEDED(m_hResult))
  60. {
  61. m_errorMessage.Format(_T("IDiscRecorder2->get_SupportedProfiles Failed - Error:0x%08x"), m_hResult);
  62. return false;
  63. }
  64. VARIANT_BOOL isSupported = VARIANT_FALSE;
  65. m_hResult = m_discFormatData->IsRecorderSupported(pDiscRecorder->GetInterface(), &isSupported);
  66. if (isSupported == VARIANT_FALSE)
  67. {
  68. m_errorMessage = _T("Recorder not supported");
  69. return false;
  70. }
  71. m_hResult = m_discFormatData->put_Recorder(pDiscRecorder->GetInterface());
  72. if (!SUCCEEDED(m_hResult))
  73. {
  74. m_errorMessage.Format(_T("IDiscFormat2Data->put_Recorder Failed - Error:0x%08x"), m_hResult);
  75. return false;
  76. }
  77. m_hResult = m_discFormatData->put_ClientName(clientName.AllocSysString());
  78. if (!SUCCEEDED(m_hResult))
  79. {
  80. m_errorMessage.Format(_T("IDiscFormat2Data->put_ClientName 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 (streamData == NULL)
  158. return false;
  159. m_streamData = streamData;
  160. m_hNotificationWnd = hNotificationWnd;
  161. // Create the event sink
  162. CDiscFormatDataEvent* eventSink = CDiscFormatDataEvent::CreateEventSink();
  163. if (eventSink == NULL)
  164. {
  165. m_errorMessage = _T("Unable to create event sink");
  166. return false;
  167. }
  168. if (!eventSink->ConnectDiscFormatData(this))
  169. {
  170. m_errorMessage = _T("Unable to connect event sink with interface");
  171. return false;
  172. }
  173. eventSink->SetHwnd(m_hNotificationWnd);
  174. m_discFormatData->put_ForceMediaToBeClosed(m_closeMedia ? VARIANT_TRUE : VARIANT_FALSE);
  175. m_hResult = m_discFormatData->Write(m_streamData);
  176. delete eventSink;
  177. if (SUCCEEDED(m_hResult))
  178. {
  179. return true;
  180. }
  181. m_errorMessage.Format(_T("IDiscFormat2Data->Write Failed! Error:0x%08x"),
  182. m_hResult);
  183. return false;
  184. }
  185. CString CDiscFormatData::GetWriteErrorMsg(HRESULT hr)
  186. {
  187. CString cstrWriteError;
  188. switch(hr)
  189. {
  190. case 0xC0AA020D:
  191. cstrWriteError = _T("超时");
  192. break;
  193. case 0xC0AA02FF:
  194. cstrWriteError = _T("异常或者数据无效");
  195. break;
  196. case 0xC0AA0204:
  197. cstrWriteError = _T("光盘放颠倒了");
  198. break;
  199. case 0xC0AA0205:
  200. cstrWriteError = _T("准备中,稍后可能需要继续调用Write函数");
  201. break;
  202. case 0xC0AA0202:
  203. cstrWriteError = _T("光驱中无光盘");
  204. break;
  205. case 0xC0AA0206:
  206. cstrWriteError = _T("media正在格式化,请稍等");
  207. break;
  208. case 0xC0AA0207:
  209. cstrWriteError = _T("光驱可能已经长时间工作,现在需要停止工作一会");
  210. break;
  211. case 0xC0AA0203:
  212. cstrWriteError = _T("光盘不兼容或者未知的物理格式");
  213. break;
  214. case 0xC0AA0201:
  215. cstrWriteError = _T("光驱要求的页模式,应用程序未提供");
  216. break;
  217. case 0xC0AA0208:
  218. cstrWriteError = _T("模式页不支持");
  219. break;
  220. case 0xC0AA0209:
  221. cstrWriteError = _T("光盘写保护");
  222. break;
  223. case 0xC0AA020F:
  224. cstrWriteError = _T("写入的速度不匹配");
  225. break;
  226. case 6:
  227. cstrWriteError = _T("句柄无效");
  228. break;
  229. case 55:
  230. cstrWriteError = _T("网络异常或者光驱被卸载");
  231. break;
  232. case 0xC0AA0210:
  233. cstrWriteError = _T("光驱正在被其他的写入独占"); //
  234. break;
  235. case 0xC0AA0301:
  236. cstrWriteError = _T("光驱报告异常");
  237. break;
  238. case 0xC0AA0003:
  239. cstrWriteError = _T("写入动作没有指定光驱");
  240. break;
  241. case 0x00AA0005:
  242. cstrWriteError = _T("要求的旋转时刻录,光驱不支持");
  243. break;
  244. case 0x00AA0004:
  245. cstrWriteError = _T("要求的刻录速度光驱不支持,光驱已自行调整刻录速度");
  246. break;
  247. case 0x00AA0006:
  248. cstrWriteError = _T("要求的旋转刻录和刻录速度不支持,光驱已经自行匹配");
  249. break;
  250. case 0xC0AA0407:
  251. cstrWriteError = _T("光驱不支持光盘的格式");
  252. break;
  253. case 0xC0AA0002:
  254. cstrWriteError = _T("操作取消");
  255. break;
  256. case 0xC0AA0400:
  257. cstrWriteError = _T("另一个写入操作与此操作冲突"); //
  258. break;
  259. case 0xC0AA0403:
  260. cstrWriteError = _T("提供的IStream大小无效.The size must be a multiple of the sector size, 2048.");
  261. break;
  262. case 0x80070057:
  263. cstrWriteError = _T("一个或多个操作无效");
  264. break;
  265. case 0x80004003:
  266. cstrWriteError = _T("指针无效");
  267. break;
  268. case 0x80004005:
  269. cstrWriteError = _T("未知的失败");
  270. break;
  271. case 0x8007000E:
  272. cstrWriteError = _T("内存不足");
  273. break;
  274. case 0x80004001:
  275. cstrWriteError = _T("未知");
  276. break;
  277. case 0xC0AA0300:
  278. cstrWriteError = _T("The write failed because the drive did not receive data quickly enough to continue writing");
  279. break;
  280. case 0xC0AA020E:
  281. cstrWriteError = _T("DVD设备未找到");
  282. break;
  283. }
  284. CString strTemp;
  285. strTemp .Format(_T(" 错误号:0x%x,"),hr);
  286. cstrWriteError += strTemp;
  287. return cstrWriteError;
  288. }
  289. /************************************************************************/
  290. /* 函数:获取媒体总扇区大小(不是空闲扇区大小)[3/20/2018 Jeff];
  291. /* 描述:;
  292. /* 参数:;
  293. /* [IN] :;
  294. /* [OUT] :;
  295. /* [IN/OUT] :;
  296. /* 返回:void;
  297. /* 注意:;
  298. /* 示例:;
  299. /*
  300. /* 修改:;
  301. /* 日期:;
  302. /* 内容:;
  303. /************************************************************************/
  304. bool CDiscFormatData::GetFreeSectorsOnMedia(LONG& lFreeSectorOnMedia)
  305. {
  306. if (m_discFormatData == NULL)
  307. return false;
  308. HRESULT hr = m_discFormatData->get_FreeSectorsOnMedia(&lFreeSectorOnMedia);
  309. if (SUCCEEDED(hr))
  310. {
  311. lFreeSectorOnMedia *= 2;
  312. lFreeSectorOnMedia /= 1024;
  313. //lFreeSectorOnMedia /= 1024;
  314. return true;
  315. }
  316. return false;
  317. }