DiscMaster.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ///////////////////////////////////////////////////////////////////////
  2. // DiscMaster.cpp
  3. //
  4. // Wrapper for IDiscMaster2 Interface
  5. //
  6. // Written by Eric Haddan
  7. //
  8. #include "StdAfx.h"
  9. #include "DiscMaster.h"
  10. CDiscMaster::CDiscMaster(void)
  11. : m_discMaster(NULL)
  12. , m_hResult(0)
  13. {
  14. }
  15. CDiscMaster::~CDiscMaster(void)
  16. {
  17. if (m_discMaster)
  18. m_discMaster->Release();
  19. }
  20. ///////////////////////////////////////////////////////////////////////
  21. //
  22. // CDiscMaster::Initialize()
  23. //
  24. // Description:
  25. // Creates and initializes the IDiscMaster2 interface
  26. //
  27. bool CDiscMaster::Initialize()
  28. {
  29. ASSERT(m_discMaster == NULL);
  30. //
  31. // Initialize the IDiscMaster2 Interface
  32. //
  33. if (m_discMaster == NULL)
  34. {
  35. m_hResult = CoCreateInstance(__uuidof(MsftDiscMaster2), NULL, CLSCTX_INPROC_SERVER,__uuidof(IDiscMaster2), (void**)&m_discMaster);
  36. if (!SUCCEEDED(m_hResult))
  37. {
  38. m_errorMessage.Format(_T("Unable to Initialize IDiscMaster2 - Error:0x%08x"), m_hResult);
  39. return false;
  40. }
  41. }
  42. //
  43. // Verify that we have some device that uses this interface
  44. //
  45. VARIANT_BOOL isSupported = VARIANT_FALSE;
  46. m_hResult = m_discMaster->get_IsSupportedEnvironment(&isSupported);
  47. if (!SUCCEEDED(m_hResult))
  48. {
  49. m_errorMessage.Format(_T("IDiscMaster2->get_IsSupportedEnvironment failed! - Error:0x%08x"), m_hResult);
  50. return false;
  51. }
  52. if (isSupported == VARIANT_FALSE)
  53. {
  54. m_errorMessage = _T("There were no writable devices detected!");
  55. return false;
  56. }
  57. return true;
  58. }
  59. ///////////////////////////////////////////////////////////////////////
  60. //
  61. // CDiscMaster::GetTotalDevices()
  62. //
  63. // Description:
  64. // Returns the total number of installed CD/DVD devices
  65. //
  66. long CDiscMaster::GetTotalDevices()
  67. {
  68. ASSERT(m_discMaster != NULL);
  69. if (m_discMaster == NULL)
  70. return 0;
  71. long totalDevices = 0;
  72. m_hResult = m_discMaster->get_Count(&totalDevices);
  73. if (FAILED(m_hResult))
  74. {
  75. m_errorMessage.Format(_T("IDiscMaster2->get_Count failed! - Error:0x%08x"), m_hResult);
  76. return 0;
  77. }
  78. return totalDevices;
  79. }
  80. ///////////////////////////////////////////////////////////////////////
  81. //
  82. // CDiscMaster::GetDeviceUniqueID()
  83. //
  84. // Description:
  85. // Returns the unique id of the device
  86. //
  87. CString CDiscMaster::GetDeviceUniqueID(long index)
  88. {
  89. ASSERT(m_discMaster != NULL);
  90. ASSERT(index < GetTotalDevices());
  91. BSTR uniqueID = NULL;
  92. m_hResult = m_discMaster->get_Item(index, &uniqueID);
  93. if (FAILED(m_hResult))
  94. {
  95. m_errorMessage.Format(_T("IDiscMaster2->get_Item(%d) failed! - Error:0x%08x"),
  96. index, m_hResult);
  97. return _T("");
  98. }
  99. return uniqueID;
  100. }