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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
//
// ENUMFORMAT.CPP
//
// By J Brown 2004
//
// www.catch22.net
//
// Implementation of the IEnumFORMATETC interface
//
// For Win2K and above look at the SHCreateStdEnumFmtEtc API call!!
//
// Apparently the order of formats in an IEnumFORMATETC object must be
// the same as those that were stored in the clipboard
//
//
//#define STRICT
#include "headers.h"
class CEnumFormatEtc : public IEnumFORMATETC
{
public:
//
// IUnknown members
//
HRESULT __stdcall QueryInterface(REFIID iid, void ** ppvObject);
ULONG __stdcall AddRef(void);
ULONG __stdcall Release(void);
//
// IEnumFormatEtc members
//
HRESULT __stdcall Next(ULONG celt, FORMATETC * rgelt, ULONG * pceltFetched);
HRESULT __stdcall Skip(ULONG celt);
HRESULT __stdcall Reset(void);
HRESULT __stdcall Clone(IEnumFORMATETC ** ppEnumFormatEtc);
//
// Construction / Destruction
//
CEnumFormatEtc(FORMATETC *pFormatEtc, int nNumFormats);
~CEnumFormatEtc();
private:
LONG m_lRefCount; // Reference count for this COM interface
ULONG m_nIndex; // current enumerator index
ULONG m_nNumFormats; // number of FORMATETC members
FORMATETC * m_pFormatEtc; // array of FORMATETC objects
};
//
// "Drop-in" replacement for SHCreateStdEnumFmtEtc. Called by CDataObject::EnumFormatEtc
//
HRESULT CreateEnumFormatEtc(UINT nNumFormats, FORMATETC *pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc)
{
if (nNumFormats == 0 || pFormatEtc == 0 || ppEnumFormatEtc == 0)
return E_INVALIDARG;
*ppEnumFormatEtc = new CEnumFormatEtc(pFormatEtc, nNumFormats);
return (*ppEnumFormatEtc) ? S_OK : E_OUTOFMEMORY;
}
//
// Helper function to perform a "deep" copy of a FORMATETC
//
static void DeepCopyFormatEtc(FORMATETC *dest, FORMATETC *source)
{
// copy the source FORMATETC into dest
*dest = *source;
if (source->ptd) {
// allocate memory for the DVTARGETDEVICE if necessary
dest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(sizeof(DVTARGETDEVICE));
// copy the contents of the source DVTARGETDEVICE into dest->ptd
*(dest->ptd) = *(source->ptd);
}
}
//
// Constructor
//
CEnumFormatEtc::CEnumFormatEtc(FORMATETC *pFormatEtc, int nNumFormats)
{
m_lRefCount = 1;
m_nIndex = 0;
m_nNumFormats = nNumFormats;
m_pFormatEtc = new FORMATETC[nNumFormats];
// copy the FORMATETC structures
for (int i = 0; i < nNumFormats; i++) {
DeepCopyFormatEtc(&m_pFormatEtc[i], &pFormatEtc[i]);
}
}
//
// Destructor
//
CEnumFormatEtc::~CEnumFormatEtc()
{
if (m_pFormatEtc) {
for (ULONG i = 0; i < m_nNumFormats; i++) {
if (m_pFormatEtc[i].ptd)
CoTaskMemFree(m_pFormatEtc[i].ptd);
}
delete[] m_pFormatEtc;
}
}
//
// IUnknown::AddRef
//
ULONG __stdcall CEnumFormatEtc::AddRef(void)
{
// increment object reference count
return InterlockedIncrement(&m_lRefCount);
}
//
// IUnknown::Release
//
ULONG __stdcall CEnumFormatEtc::Release(void)
{
// decrement object reference count
LONG count = InterlockedDecrement(&m_lRefCount);
if (count == 0) {
delete this;
return 0;
}
else {
return count;
}
}
//
// IUnknown::QueryInterface
//
HRESULT __stdcall CEnumFormatEtc::QueryInterface(REFIID iid, void **ppvObject)
{
// check to see what interface has been requested
if (iid == IID_IEnumFORMATETC || iid == IID_IUnknown) {
AddRef();
*ppvObject = this;
return S_OK;
}
else {
*ppvObject = 0;
return E_NOINTERFACE;
}
}
//
// IEnumFORMATETC::Next
//
// If the returned FORMATETC structure contains a non-null "ptd" member, then
// the caller must free this using CoTaskMemFree (stated in the COM documentation)
//
HRESULT __stdcall CEnumFormatEtc::Next(ULONG celt, FORMATETC *pFormatEtc, ULONG * pceltFetched)
{
ULONG copied = 0;
// validate arguments
if (celt == 0 || pFormatEtc == 0)
return E_INVALIDARG;
// copy FORMATETC structures into caller's buffer
while (m_nIndex < m_nNumFormats && copied < celt) {
DeepCopyFormatEtc(&pFormatEtc[copied], &m_pFormatEtc[m_nIndex]);
copied++;
m_nIndex++;
}
// store result
if (pceltFetched != 0)
*pceltFetched = copied;
// did we copy all that was requested?
return (copied == celt) ? S_OK : S_FALSE;
}
//
// IEnumFORMATETC::Skip
//
HRESULT __stdcall CEnumFormatEtc::Skip(ULONG celt)
{
m_nIndex += celt;
return (m_nIndex <= m_nNumFormats) ? S_OK : S_FALSE;
}
//
// IEnumFORMATETC::Reset
//
HRESULT __stdcall CEnumFormatEtc::Reset(void)
{
m_nIndex = 0;
return S_OK;
}
//
// IEnumFORMATETC::Clone
//
HRESULT __stdcall CEnumFormatEtc::Clone(IEnumFORMATETC ** ppEnumFormatEtc)
{
HRESULT hResult;
// make a duplicate enumerator
hResult = CreateEnumFormatEtc(m_nNumFormats, m_pFormatEtc, ppEnumFormatEtc);
if (hResult == S_OK) {
// manually set the index state
((CEnumFormatEtc *)*ppEnumFormatEtc)->m_nIndex = m_nIndex;
}
return hResult;
}
|