DiscRecorder.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. ///////////////////////////////////////////////////////////////////////
  2. // DiscRecorder.cpp
  3. //
  4. // Wrapper for IDiscRecorder2 Interface
  5. //
  6. // Written by Eric Haddan
  7. //
  8. #include "StdAfx.h"
  9. #include "DiscRecorder.h"
  10. #ifndef CDROM_EXCLUSIVE_CALLER_LENGTH
  11. #define CDROM_EXCLUSIVE_CALLER_LENGTH 64
  12. #endif
  13. CDiscRecorder::CDiscRecorder(void)
  14. : m_discRecorder(NULL)
  15. , m_volumePathNames(NULL)
  16. {
  17. }
  18. CDiscRecorder::~CDiscRecorder(void)
  19. {
  20. if (m_discRecorder != NULL)
  21. {
  22. m_discRecorder->Release();
  23. m_discRecorder = NULL;
  24. }
  25. }
  26. ///////////////////////////////////////////////////////////////////////
  27. //
  28. // CDiscRecorder::Initialize()
  29. //
  30. // Description:
  31. // Creates and initializes the IDiscRecorder2 interface
  32. //
  33. // Parameters:
  34. // recorderUniqueId The recorder's unique id retrieved from
  35. // the IDiscMaster2 interface.
  36. //
  37. bool CDiscRecorder::Initialize(const CString& recorderUniqueId)
  38. {
  39. m_recorderUniqueId = recorderUniqueId;
  40. //
  41. // Create an IDiscRecorder2
  42. //
  43. m_hResult = CoCreateInstance(__uuidof(MsftDiscRecorder2), NULL, CLSCTX_INPROC_SERVER,
  44. __uuidof(IDiscRecorder2), (void**)&m_discRecorder);
  45. ASSERT(SUCCEEDED(m_hResult));
  46. if (FAILED(m_hResult))
  47. {
  48. return false;
  49. }
  50. m_hResult = m_discRecorder->InitializeDiscRecorder(recorderUniqueId.AllocSysString());
  51. if (FAILED(m_hResult))
  52. {
  53. return false;
  54. }
  55. //
  56. // Get the volume name paths
  57. //
  58. /**********以下将不显示不支持刻录的光驱
  59. //SAFEARRAY *pSafeArray;
  60. //m_discRecorder->get_SupportedProfiles(&pSafeArray);
  61. //if(pSafeArray == NULL)
  62. //{
  63. // return false;
  64. //}
  65. //ULONG i = 0;
  66. //for( i = 0; i < pSafeArray->rgsabound[0].cElements; i++)
  67. //{
  68. // int nData = ((VARIANT*)pSafeArray->pvData)[i].intVal;
  69. // if(nData == IMAPI_MEDIA_TYPE_CDRW || nData == IMAPI_MEDIA_TYPE_DVDPLUSRW || nData == IMAPI_MEDIA_TYPE_DVDDASHRW
  70. // || nData == IMAPI_MEDIA_TYPE_DISK || nData == IMAPI_MEDIA_TYPE_DVDPLUSRW_DUALLAYER || nData == IMAPI_MEDIA_TYPE_BDRE)
  71. // {
  72. // break;
  73. // }
  74. //}
  75. //if(i >= pSafeArray->rgsabound[0].cElements)
  76. //{
  77. // return false;
  78. //}
  79. ************/
  80. m_hResult = m_discRecorder->get_VolumePathNames(&m_volumePathNames);
  81. //BSTR bstr;
  82. //m_discRecorder->get_VolumeName(&bstr); // 此函数获得光驱的ID,而非磁盘盘符名
  83. if(!SUCCEEDED(m_hResult))
  84. {
  85. return false;
  86. }
  87. return true;
  88. }
  89. ///////////////////////////////////////////////////////////////////////
  90. //
  91. // CDiscRecorder::AcquireExclusiveAccess()
  92. //
  93. // Description:
  94. // Acquires exclusive access to this device
  95. //
  96. // Parameters:
  97. // force forces exclusive access whether or not the device
  98. // can be dismounted
  99. // clientName The name of the client application
  100. //
  101. bool CDiscRecorder::AcquireExclusiveAccess(bool force, const CString& clientName)
  102. {
  103. int length = clientName.GetLength();
  104. ASSERT(!clientName.IsEmpty());
  105. ASSERT(length < CDROM_EXCLUSIVE_CALLER_LENGTH);
  106. if (length == 0 || length >= CDROM_EXCLUSIVE_CALLER_LENGTH)
  107. {
  108. return false;
  109. }
  110. for (int index = 0; index < length; index++)
  111. {
  112. TCHAR ch = clientName[index];
  113. if (_istalnum(ch) || ch == _T(' ') || ch == _T('.') || ch == _T(',') ||
  114. ch == _T(':') || ch == _T(';') || ch == _T('-') || ch == _T('_'))
  115. {
  116. continue;
  117. }
  118. //
  119. // Client name does not meet specification
  120. //
  121. ASSERT(FALSE);
  122. return false;
  123. }
  124. if (m_discRecorder != NULL)
  125. {
  126. m_hResult = m_discRecorder->AcquireExclusiveAccess(
  127. force ? VARIANT_TRUE : VARIANT_FALSE,
  128. clientName.AllocSysString());
  129. if (SUCCEEDED(m_hResult))
  130. {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. ///////////////////////////////////////////////////////////////////////
  137. //
  138. // CDiscRecorder::ReleaseExclusiveAccess()
  139. //
  140. // Description:
  141. // Releases exclusive access to this device
  142. //
  143. bool CDiscRecorder::ReleaseExclusiveAccess()
  144. {
  145. if (m_discRecorder != NULL)
  146. {
  147. m_hResult = m_discRecorder->ReleaseExclusiveAccess();
  148. if (SUCCEEDED(m_hResult))
  149. {
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. ///////////////////////////////////////////////////////////////////////
  156. //
  157. // CDiscRecorder::ExclusiveAccessOwner()
  158. //
  159. // Description:
  160. // Returns the name of the client who has exclusive access to this
  161. // device.
  162. //
  163. CString CDiscRecorder::ExclusiveAccessOwner()
  164. {
  165. if (m_discRecorder != NULL)
  166. {
  167. BSTR owner = NULL;
  168. m_hResult = m_discRecorder->get_ExclusiveAccessOwner(&owner);
  169. if (SUCCEEDED(m_hResult))
  170. {
  171. return owner;
  172. }
  173. }
  174. return _T("");
  175. }
  176. ///////////////////////////////////////////////////////////////////////
  177. //
  178. // CDiscRecorder::EjectMedia()
  179. //
  180. // Description:
  181. // Elects the media on this device
  182. //
  183. bool CDiscRecorder::EjectMedia()
  184. {
  185. if (m_discRecorder != NULL)
  186. {
  187. m_hResult = m_discRecorder->EjectMedia();
  188. if (SUCCEEDED(m_hResult))
  189. {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. ///////////////////////////////////////////////////////////////////////
  196. //
  197. // CDiscRecorder::CloseTray()
  198. //
  199. // Description:
  200. // Closes the tray on this device
  201. //
  202. bool CDiscRecorder::CloseTray()
  203. {
  204. if (m_discRecorder != NULL)
  205. {
  206. m_hResult = m_discRecorder->CloseTray();
  207. if (SUCCEEDED(m_hResult))
  208. {
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. ///////////////////////////////////////////////////////////////////////
  215. //
  216. // CDiscRecorder::EnableMcn()
  217. //
  218. // Description:
  219. // Enables the Media Change Notification on this device
  220. //
  221. bool CDiscRecorder::EnableMcn()
  222. {
  223. if (m_discRecorder != NULL)
  224. {
  225. m_hResult = m_discRecorder->EnableMcn();
  226. if (SUCCEEDED(m_hResult))
  227. {
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. ///////////////////////////////////////////////////////////////////////
  234. //
  235. // CDiscRecorder::DisableMcn()
  236. //
  237. // Description:
  238. // Disables the Media Change Notification on this device
  239. //
  240. bool CDiscRecorder::DisableMcn()
  241. {
  242. if (m_discRecorder != NULL)
  243. {
  244. m_hResult = m_discRecorder->DisableMcn();
  245. if (SUCCEEDED(m_hResult))
  246. {
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. ///////////////////////////////////////////////////////////////////////
  253. //
  254. // CDiscRecorder::LegacyDeviceNumber()
  255. //
  256. // Description:
  257. // Returns the legacy device number
  258. //
  259. LONG CDiscRecorder::GetLegacyDeviceNumber()
  260. {
  261. LONG deviceNumber = 0;
  262. if (m_discRecorder != NULL)
  263. {
  264. m_discRecorder->get_LegacyDeviceNumber(&deviceNumber);
  265. }
  266. return deviceNumber;
  267. }
  268. ///////////////////////////////////////////////////////////////////////
  269. //
  270. // CDiscRecorder::ProductRevision()
  271. //
  272. // Description:
  273. // Returns the product id for this device
  274. //
  275. CString CDiscRecorder::GetProductID()
  276. {
  277. BSTR productId = NULL;
  278. if (m_discRecorder != NULL)
  279. {
  280. m_discRecorder->get_ProductId(&productId);
  281. }
  282. return productId;
  283. }
  284. ///////////////////////////////////////////////////////////////////////
  285. //
  286. // CDiscRecorder::ProductRevision()
  287. //
  288. // Description:
  289. // Returns the product revision for this device
  290. //
  291. CString CDiscRecorder::GetProductRevision()
  292. {
  293. BSTR productRevision = NULL;
  294. if (m_discRecorder != NULL)
  295. {
  296. m_discRecorder->get_ProductRevision(&productRevision);
  297. }
  298. return productRevision;
  299. }
  300. ///////////////////////////////////////////////////////////////////////
  301. //
  302. // CDiscRecorder::VendorId()
  303. //
  304. // Description:
  305. // Returns the vendor id for this device
  306. //
  307. CString CDiscRecorder::GetVendorId()
  308. {
  309. BSTR vendorId = NULL;
  310. if (m_discRecorder != NULL)
  311. {
  312. m_discRecorder->get_VendorId(&vendorId);
  313. }
  314. return vendorId;
  315. }
  316. ///////////////////////////////////////////////////////////////////////
  317. //
  318. // CDiscRecorder::VolumeName()
  319. //
  320. // Description:
  321. // Returns the unique volume name associated with this device
  322. //
  323. CString CDiscRecorder::GetVolumeName()
  324. {
  325. BSTR volumeName = NULL;
  326. if (m_discRecorder != NULL)
  327. {
  328. m_discRecorder->get_VolumeName(&volumeName);
  329. }
  330. return volumeName;
  331. }
  332. ULONG CDiscRecorder::GetTotalVolumePaths()
  333. {
  334. if (m_volumePathNames != NULL)
  335. {
  336. return m_volumePathNames->rgsabound[0].cElements;
  337. }
  338. return 0;
  339. }
  340. CString CDiscRecorder::GetVolumePath(ULONG volumePathIndex)
  341. {
  342. ASSERT(volumePathIndex < m_volumePathNames->rgsabound[0].cElements);
  343. if (volumePathIndex >= m_volumePathNames->rgsabound[0].cElements)
  344. {
  345. return _T("");
  346. }
  347. return ((VARIANT*)(m_volumePathNames->pvData))[volumePathIndex].bstrVal;
  348. }