DiscMaster.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. {
  19. m_discMaster->Release();
  20. m_discMaster = NULL;
  21. }
  22. }
  23. ///////////////////////////////////////////////////////////////////////
  24. //
  25. // CDiscMaster::Initialize()
  26. //
  27. // Description:
  28. // Creates and initializes the IDiscMaster2 interface
  29. //
  30. bool CDiscMaster::Initialize()
  31. {
  32. ASSERT(m_discMaster == NULL);
  33. //
  34. // Initialize the IDiscMaster2 Interface
  35. //
  36. if (m_discMaster == NULL)
  37. {
  38. m_hResult = CoCreateInstance(__uuidof(MsftDiscMaster2), NULL, CLSCTX_INPROC_SERVER,
  39. __uuidof(IDiscMaster2), (void**)&m_discMaster);
  40. if (!SUCCEEDED(m_hResult))
  41. {
  42. m_errorMessage.Format(_T("Unable to Initialize IDiscMaster2 - Error:0x%08x"), m_hResult);
  43. return false;
  44. }
  45. }
  46. //
  47. // Verify that we have some device that uses this interface
  48. //
  49. VARIANT_BOOL isSupported = VARIANT_FALSE;
  50. m_hResult = m_discMaster->get_IsSupportedEnvironment(&isSupported); // get_IsSupportedEnvironment 获取是否有权限控制光驱
  51. if (!SUCCEEDED(m_hResult))
  52. {
  53. m_errorMessage.Format(_T("IDiscMaster2->get_IsSupportedEnvironment failed! - Error:0x%08x"), m_hResult);
  54. return false;
  55. }
  56. if (isSupported == VARIANT_FALSE)
  57. {
  58. m_errorMessage = _T("There were no writable devices detected!");
  59. return false;
  60. }
  61. return true;
  62. }
  63. ///////////////////////////////////////////////////////////////////////
  64. //
  65. // CDiscMaster::GetTotalDevices()
  66. //
  67. // Description:
  68. // Returns the total number of installed CD/DVD devices
  69. //
  70. long CDiscMaster::GetTotalDevices()
  71. {
  72. ASSERT(m_discMaster != NULL);
  73. if (m_discMaster == NULL)
  74. return 0;
  75. long totalDevices = 0;
  76. m_hResult = m_discMaster->get_Count(&totalDevices);
  77. if (FAILED(m_hResult))
  78. {
  79. m_errorMessage.Format(_T("IDiscMaster2->get_Count failed! - Error:0x%08x"), m_hResult);
  80. return 0;
  81. }
  82. return totalDevices;
  83. }
  84. ///////////////////////////////////////////////////////////////////////
  85. //
  86. // CDiscMaster::GetDeviceUniqueID()
  87. //
  88. // Description:
  89. // Returns the unique id of the device
  90. //
  91. CString CDiscMaster::GetDeviceUniqueID(long index)
  92. {
  93. ASSERT(m_discMaster != NULL);
  94. ASSERT(index < GetTotalDevices());
  95. BSTR uniqueID = NULL;
  96. m_hResult = m_discMaster->get_Item(index, &uniqueID);
  97. if (FAILED(m_hResult))
  98. {
  99. m_errorMessage.Format(_T("IDiscMaster2->get_Item(%d) failed! - Error:0x%08x"),
  100. index, m_hResult);
  101. return _T("");
  102. }
  103. return uniqueID;
  104. }