DiscRecorder.cpp 7.0 KB

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