-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy pathcomtest.cpp
More file actions
317 lines (274 loc) · 7.61 KB
/
comtest.cpp
File metadata and controls
317 lines (274 loc) · 7.61 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Christoph M. Becker <cmb@php.net> |
| Based on: <https://thrysoee.dk/InsideCOM+/> |
+----------------------------------------------------------------------+
*/
#include "comtest.h" // Generated by MIDL
long g_cComponents = 0;
long g_cLocks = 0;
class CDocument : public IDocument, IPersistStream
{
public:
// IUnknown
STDMETHODIMP_(ULONG) AddRef();
STDMETHODIMP_(ULONG) Release();
STDMETHODIMP QueryInterface(REFIID riid, void** ppv);
// IDispatch
STDMETHODIMP GetTypeInfoCount(UINT* pCountTypeInfo);
STDMETHODIMP GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo);
STDMETHODIMP GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId);
STDMETHODIMP Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
// IDocument
STDMETHODIMP get_Content(BSTR* retvalue);
STDMETHODIMP put_Content(BSTR newvalue);
// IPersist
STDMETHODIMP GetClassID(CLSID *pClassID);
// IPersistStream
STDMETHODIMP IsDirty( void);
STDMETHODIMP Load(IStream *pStm);
STDMETHODIMP Save(IStream *pStm, BOOL fClearDirty);
STDMETHODIMP GetSizeMax(ULARGE_INTEGER *pcbSize);
CDocument() : m_content(SysAllocString(L"")), m_cRef(1) { g_cComponents++; }
~CDocument() { SysFreeString(m_content); g_cComponents--; }
HRESULT Init(void);
private:
BSTR m_content;
ULONG m_cRef;
ITypeInfo* m_pTypeInfo;
BOOL m_dirty;
};
ULONG CDocument::AddRef()
{
return ++m_cRef;
}
ULONG CDocument::Release()
{
if (--m_cRef != 0) {
return m_cRef;
}
m_pTypeInfo->Release();
delete this;
return 0;
}
HRESULT CDocument::QueryInterface(REFIID riid, void** ppv)
{
if (riid == IID_IUnknown) {
*ppv = static_cast<IDocument*>(this);
} else if (riid == IID_IDocument) {
*ppv = static_cast<IDocument*>(this);
} else if (riid == IID_IDispatch) {
*ppv = static_cast<IDispatch*>(this);
} else if (riid == IID_IPersistStream) {
*ppv = static_cast<IPersistStream*>(this);
} else {
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
HRESULT CDocument::GetTypeInfoCount(UINT* pCountTypeInfo)
{
*pCountTypeInfo = 1;
return S_OK;
}
HRESULT CDocument::GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo)
{
(void) lcid;
*ppITypeInfo = NULL;
if (iTypeInfo != 0) {
return DISP_E_BADINDEX;
}
m_pTypeInfo->AddRef();
*ppITypeInfo = m_pTypeInfo;
return S_OK;
}
HRESULT CDocument::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId)
{
(void) lcid;
if (riid != IID_NULL) {
return DISP_E_UNKNOWNINTERFACE;
}
return DispGetIDsOfNames(m_pTypeInfo, rgszNames, cNames, rgDispId);
}
HRESULT CDocument::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
(void) lcid;
if (riid != IID_NULL) {
return DISP_E_UNKNOWNINTERFACE;
}
return DispInvoke(this, m_pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}
HRESULT CDocument::get_Content(BSTR* retvalue)
{
*retvalue = SysAllocString(m_content);
return S_OK;
}
HRESULT CDocument::put_Content(BSTR newvalue)
{
SysFreeString(m_content);
m_content = SysAllocString(newvalue);
return S_OK;
}
HRESULT CDocument::Init(void)
{
ITypeLib* pTypeLib;
if (FAILED(LoadRegTypeLib(LIBID_ComTest, 1, 0, LANG_NEUTRAL, &pTypeLib))) {
return E_FAIL;
}
HRESULT hr = pTypeLib->GetTypeInfoOfGuid(IID_IDocument, &m_pTypeInfo);
if (FAILED(hr)) {
pTypeLib->Release();
return hr;
}
pTypeLib->Release();
return S_OK;
}
HRESULT CDocument::GetClassID(CLSID *pClassID)
{
*pClassID = CLSID_Document;
return S_OK;
}
HRESULT CDocument::IsDirty(void)
{
return m_dirty ? S_OK : S_FALSE;
}
HRESULT CDocument::Load(IStream *pStm)
{
ULONG read = 0;
ULONG cbSize;
if (FAILED(pStm->Read(&cbSize, sizeof cbSize, &read))) {
return S_FALSE;
}
if (!SysReAllocStringLen(&m_content, NULL, cbSize)) {
return S_FALSE;
}
if (FAILED(pStm->Read(m_content, cbSize, &read))) {
// we may have garbage in m_content, but don't mind
return S_FALSE;
}
m_dirty = FALSE;
return S_OK;
}
HRESULT CDocument::Save(IStream *pStm, BOOL fClearDirty)
{
ULONG written = 0;
ULONG cbSize = SysStringByteLen(m_content);
HRESULT hr;
if (FAILED(hr = pStm->Write(&cbSize, sizeof cbSize, &written))) {
return S_FALSE;
}
if (FAILED(hr = pStm->Write(m_content, cbSize, &written))) {
return S_FALSE;
}
if (fClearDirty) {
m_dirty = FALSE;
}
return S_OK;
}
HRESULT CDocument::GetSizeMax(ULARGE_INTEGER *pcbSize)
{
(*pcbSize).QuadPart = sizeof(ULONG) + SysStringByteLen(m_content);
return S_OK;
}
class CDocumentFactory : public IClassFactory
{
public:
// IUnknown
STDMETHODIMP_(ULONG) AddRef();
STDMETHODIMP_(ULONG) Release();
STDMETHODIMP QueryInterface(REFIID riid, void** ppv);
// IClassFactory
STDMETHODIMP CreateInstance(IUnknown* pUnknownOuter, REFIID riid, void** ppv);
STDMETHODIMP LockServer(BOOL bLock);
CDocumentFactory() : m_cRef(1) { g_cLocks++; }
~CDocumentFactory() { g_cLocks--; }
private:
ULONG m_cRef;
};
ULONG CDocumentFactory::AddRef()
{
return ++m_cRef;
}
ULONG CDocumentFactory::Release()
{
if (--m_cRef != 0) {
return m_cRef;
}
delete this;
return 0;
}
HRESULT CDocumentFactory::QueryInterface(REFIID riid, void** ppv)
{
if (riid == IID_IUnknown) {
*ppv = (IUnknown*)this;
} else if (riid == IID_IClassFactory) {
*ppv = (IClassFactory*)this;
} else {
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
HRESULT CDocumentFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
{
if (pUnknownOuter != NULL) {
return CLASS_E_NOAGGREGATION;
}
CDocument *pDocument = new CDocument;
if (pDocument == NULL) {
return E_OUTOFMEMORY;
}
HRESULT hr;
if (FAILED(hr = pDocument->Init())) {
return hr;
}
hr = pDocument->QueryInterface(riid, ppv);
pDocument->Release();
return hr;
}
HRESULT CDocumentFactory::LockServer(BOOL bLock)
{
if (bLock) {
g_cLocks++;
} else {
g_cLocks --;
}
return S_OK;
}
HRESULT __stdcall DllCanUnloadNow()
{
if (g_cLocks == 0) {
return S_OK;
} else {
return S_FALSE;
}
}
HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID riid, void** ppv)
{
if (clsid != CLSID_Document) {
return CLASS_E_CLASSNOTAVAILABLE;
}
CDocumentFactory* pFactory = new CDocumentFactory;
if (pFactory == NULL) {
return E_OUTOFMEMORY;
}
// riid is probably IID_IClassFactory.
HRESULT hr = pFactory->QueryInterface(riid, ppv);
pFactory->Release();
return hr;
}