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
|
// EnumFormatEtc.cpp
#include "StdAfx.h"
#include "EnumFormatEtc.h"
#include "MyCom2.h"
class CEnumFormatEtc :
public IEnumFORMATETC,
public CMyUnknownImp
{
public:
MY_UNKNOWN_IMP1_MT(IEnumFORMATETC)
STDMETHOD(Next)(ULONG celt, FORMATETC *rgelt, ULONG *pceltFetched);
STDMETHOD(Skip)(ULONG celt);
STDMETHOD(Reset)(void);
STDMETHOD(Clone)(IEnumFORMATETC **ppEnumFormatEtc);
CEnumFormatEtc(const FORMATETC *pFormatEtc, ULONG numFormats);
~CEnumFormatEtc();
private:
LONG m_RefCount;
ULONG m_NumFormats;
FORMATETC *m_Formats;
ULONG m_Index;
};
static void DeepCopyFormatEtc(FORMATETC *dest, const FORMATETC *src)
{
*dest = *src;
if(src->ptd)
{
dest->ptd = (DVTARGETDEVICE*)CoTaskMemAlloc(sizeof(DVTARGETDEVICE));
*(dest->ptd) = *(src->ptd);
}
}
CEnumFormatEtc::CEnumFormatEtc(const FORMATETC *pFormatEtc, ULONG numFormats)
{
m_RefCount = 1;
m_Index = 0;
m_NumFormats = 0;
m_Formats = new FORMATETC[numFormats];
if(m_Formats)
{
m_NumFormats = numFormats;
for(ULONG i = 0; i < numFormats; i++)
DeepCopyFormatEtc(&m_Formats[i], &pFormatEtc[i]);
}
}
CEnumFormatEtc::~CEnumFormatEtc()
{
if(m_Formats)
{
for(ULONG i = 0; i < m_NumFormats; i++)
if(m_Formats[i].ptd)
CoTaskMemFree(m_Formats[i].ptd);
delete[]m_Formats;
}
}
STDMETHODIMP CEnumFormatEtc::Next(ULONG celt, FORMATETC *pFormatEtc, ULONG *pceltFetched)
{
ULONG copied = 0;
if(celt == 0 || pFormatEtc == 0)
return E_INVALIDARG;
while(m_Index < m_NumFormats && copied < celt)
{
DeepCopyFormatEtc(&pFormatEtc[copied], &m_Formats[m_Index]);
copied++;
m_Index++;
}
if(pceltFetched != 0)
*pceltFetched = copied;
return (copied == celt) ? S_OK : S_FALSE;
}
STDMETHODIMP CEnumFormatEtc::Skip(ULONG celt)
{
m_Index += celt;
return (m_Index <= m_NumFormats) ? S_OK : S_FALSE;
}
STDMETHODIMP CEnumFormatEtc::Reset(void)
{
m_Index = 0;
return S_OK;
}
STDMETHODIMP CEnumFormatEtc::Clone(IEnumFORMATETC ** ppEnumFormatEtc)
{
HRESULT hResult = CreateEnumFormatEtc(m_NumFormats, m_Formats, ppEnumFormatEtc);
if(hResult == S_OK)
((CEnumFormatEtc *)*ppEnumFormatEtc)->m_Index = m_Index;
return hResult;
}
// replacement for SHCreateStdEnumFmtEtc
HRESULT CreateEnumFormatEtc(UINT numFormats, const FORMATETC *formats, IEnumFORMATETC **enumFormat)
{
if(numFormats == 0 || formats == 0 || enumFormat == 0)
return E_INVALIDARG;
*enumFormat = new CEnumFormatEtc(formats, numFormats);
return (*enumFormat) ? S_OK : E_OUTOFMEMORY;
}
|