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