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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
//+-------------------------------------------------------------------
//
// File: cact.cxx
//
// Contents: object activation test class
//
// Classes: CActTest
//
// Functions:
//
// History: 23-Nov-92 Ricksa Created
//
//--------------------------------------------------------------------
#include <pch.cxx>
#pragma hdrstop
#include <cact.hxx> // CTestAct
// We need a semaphore to synchronize loads and releases.
CMutexSem mxsLoadRelease;
SAFE_INTERFACE_PTR(XIStream, IStream)
#define XPOS OLESTR("XPOS")
#define YPOS OLESTR("YPOS")
HRESULT ReadPos(IStorage *pstg, LPOLESTR pwszStream, ULONG *pulPos)
{
HRESULT hr;
BEGIN_BLOCK
XIStream xstrm;
// Read the streams for xpos and ypos
hr = pstg->OpenStream(pwszStream, NULL,
STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, &xstrm);
if (FAILED(hr))
{
EXIT_BLOCK;
}
ULONG cb;
hr = xstrm->Read(pulPos, sizeof(*pulPos), &cb);
if (FAILED(hr))
{
EXIT_BLOCK;
}
hr = ResultFromScode(S_OK);
END_BLOCK
return hr;
}
HRESULT WritePos(IStorage *pstg, LPOLESTR pwszStream, ULONG ulPos)
{
HRESULT hr;
BEGIN_BLOCK
XIStream xstrm;
// Read the streams for xpos and ypos
hr = pstg->CreateStream(pwszStream,
STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE, NULL, NULL,
&xstrm);
if (FAILED(hr))
{
EXIT_BLOCK;
}
ULONG cb;
hr = xstrm->Write(&ulPos, sizeof(ulPos), &cb);
if (FAILED(hr))
{
EXIT_BLOCK;
}
hr = ResultFromScode(S_OK);
END_BLOCK
return hr;
}
CTestAct::CTestAct(REFCLSID rclsid)
: _rclsid(rclsid), _fDirty(FALSE), _xPos(0), _yPos(0),
_fSaveInprogress(FALSE), _pstg(NULL), _dwRegister(0), _cRefs(1)
{
// Use as a flag for whether a file name has been assigned
_awszCurFile[0] = 0;
GlobalRefs(TRUE);
}
CTestAct::~CTestAct(void)
{
if (_pstg != NULL)
{
// Release the storage because we are done with it
ULONG ulCnt = _pstg->Release();
#if 0
// this test is not valid when running stress
if (ulCnt != 0)
{
DebugBreak();
}
#endif
}
if (_dwRegister)
{
IRunningObjectTable *prot;
GetRunningObjectTable(NULL, &prot);
prot->Revoke(_dwRegister);
prot->Release();
}
GlobalRefs(FALSE);
}
STDMETHODIMP CTestAct::QueryInterface(REFIID iid, void **ppv)
{
HRESULT hr = ResultFromScode(S_OK);
// We support IUnknown, IPersistFile and IBalls
if (IsEqualIID(iid, IID_IUnknown))
{
*ppv = (IBalls *) this;
}
else if (IsEqualIID(iid, IID_IPersistFile))
{
*ppv = (IPersistFile *) this;
}
else if (IsEqualIID(iid, IID_IPersistStorage))
{
*ppv = (IPersistStorage *) this;
}
else if (IsEqualIID(iid, IID_IBalls))
{
*ppv = (IBalls *) this;
}
else
{
*ppv = NULL;
hr = ResultFromScode(E_NOINTERFACE);
}
if (SUCCEEDED(hr))
{
AddRef();
}
return hr;
}
STDMETHODIMP_(ULONG) CTestAct::AddRef(void)
{
InterlockedIncrement(&_cRefs);
return _cRefs;
}
STDMETHODIMP_(ULONG) CTestAct::Release(void)
{
CLock lck(mxsLoadRelease);
if (InterlockedDecrement(&_cRefs) == 0)
{
delete this;
return 0;
}
return _cRefs;
}
STDMETHODIMP CTestAct::GetClassID(LPCLSID lpClassID)
{
*lpClassID = _rclsid;
return ResultFromScode(S_OK);
}
STDMETHODIMP CTestAct::IsDirty()
{
return (_fDirty) ? ResultFromScode(S_OK) : ResultFromScode(S_FALSE);
}
STDMETHODIMP CTestAct::Load(LPCOLESTR lpszFileName, DWORD grfMode)
{
CLock lck(mxsLoadRelease);
HRESULT hr;
BEGIN_BLOCK
hr = StgOpenStorage(lpszFileName, NULL,
STGM_READWRITE | STGM_SHARE_EXCLUSIVE, NULL, NULL, &_pstg);
if (FAILED(hr))
{
#if 0
// this test is not valid when running stress
if (hr == STG_E_LOCKVIOLATION)
{
DebugBreak();
}
#endif
EXIT_BLOCK;
}
// Get the saved xposition
hr = GetData();
if (FAILED(hr))
{
EXIT_BLOCK;
}
// Since everything went Ok save the file name
olestrcpy(_awszCurFile, lpszFileName);
// Create a file moniker for the object.
// Cast to non-constant string.
IMoniker *pmk;
CreateFileMoniker((LPOLESTR)lpszFileName, &pmk);
// Register it in the running object table.
IRunningObjectTable *prot;
GetRunningObjectTable(NULL, &prot);
prot->Register(NULL, (IPersistFile *) this, pmk, &_dwRegister);
// Release the temporary objects
pmk->Release();
prot->Release();
END_BLOCK
return hr;
}
STDMETHODIMP CTestAct::Save(LPCOLESTR lpszFileName, BOOL fRemember)
{
HRESULT hr;
BEGIN_BLOCK
IStorage *pstgNew;
// Save the data
if (olestrcmp(lpszFileName, _awszCurFile) == 0)
{
pstgNew = _pstg;
_fDirty = FALSE;
}
else
{
hr = StgCreateDocfile(lpszFileName,
STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
NULL, &pstgNew);
}
if (FAILED(hr))
{
EXIT_BLOCK;
}
WriteClassStg(pstgNew, _rclsid);
hr = SaveData(pstgNew);
if (FAILED(hr))
{
EXIT_BLOCK;
}
if (fRemember)
{
// Save the file name
olestrcpy(_awszCurFile, lpszFileName);
// Replace the storage
if (_pstg && pstgNew != _pstg)
{
_pstg->Release();
}
_pstg = pstgNew;
_fDirty = FALSE;
}
else
{
pstgNew->Release();
}
_fSaveInprogress = TRUE;
hr = ResultFromScode(S_OK);
END_BLOCK;
return hr;
}
STDMETHODIMP CTestAct::SaveCompleted(LPCOLESTR lpszFileName)
{
_fSaveInprogress = FALSE;
return ResultFromScode(S_OK);
}
STDMETHODIMP CTestAct::GetCurFile(LPOLESTR FAR *lpszFileName)
{
// Allocate a buffer for the file and copy in the data
if (_awszCurFile[0] == 0)
{
return ResultFromScode(S_FALSE);
}
IMalloc *pIMalloc;
HRESULT hr = CoGetMalloc(MEMCTX_TASK, &pIMalloc);
if (SUCCEEDED(hr))
{
*lpszFileName = (OLECHAR *) pIMalloc->Alloc(
olestrlen((_awszCurFile) + 1) * sizeof(OLECHAR));
olestrcpy(*lpszFileName, _awszCurFile);
hr = ResultFromScode(S_OK);
}
return hr;
}
STDMETHODIMP CTestAct::MoveBall(ULONG xPos, ULONG yPos)
{
if (!_fSaveInprogress)
{
_fDirty = TRUE;
_xPos = xPos;
_yPos = yPos;
return S_OK;
}
// Can't change state because a save is still pending
return ResultFromScode(E_UNEXPECTED);
}
STDMETHODIMP CTestAct::GetBallPos(ULONG *xPos, ULONG *yPos)
{
*xPos = _xPos;
*yPos = _yPos;
return S_OK;
}
STDMETHODIMP CTestAct::IsOverLapped(IBalls *pIBall)
{
ULONG xPos;
ULONG yPos;
HRESULT hr = pIBall->GetBallPos(&xPos, &yPos);
if (SUCCEEDED(hr))
{
if ((xPos == _xPos) && (yPos == _yPos))
{
hr = ResultFromScode(S_OK);
}
else
{
hr = ResultFromScode(S_FALSE);
}
}
return hr;
}
STDMETHODIMP CTestAct::IsContainedIn(ICube *pICube)
{
ULONG xPos;
ULONG yPos;
HRESULT hr = pICube->GetCubePos(&xPos, &yPos);
if (SUCCEEDED(hr))
{
if ((xPos == _xPos) && (yPos == _yPos))
{
hr = ResultFromScode(S_OK);
}
else
{
hr = ResultFromScode(S_FALSE);
}
}
return hr;
}
STDMETHODIMP CTestAct::Clone(IBalls **ppIBall)
{
CTestAct *ptballs = new CTestAct(_rclsid);
ptballs->_xPos = _xPos;
ptballs->_yPos = _yPos;
ptballs->_fDirty = _fDirty;
_pstg->AddRef();
ptballs->_pstg = _pstg;
olestrcpy(ptballs->_awszCurFile, _awszCurFile);
return ResultFromScode(S_OK);
}
STDMETHODIMP CTestAct::Echo(IUnknown *punkIn, IUnknown**ppunkOut)
{
*ppunkOut = punkIn;
return S_OK;
}
STDMETHODIMP CTestAct::InitNew(LPSTORAGE pStg)
{
pStg->AddRef();
_pstg = pStg;
WriteClassStg(_pstg, _rclsid);
return ResultFromScode(S_OK);
}
STDMETHODIMP CTestAct::Load(LPSTORAGE pStg)
{
HRESULT hr;
_pstg = pStg;
hr = GetData();
if (SUCCEEDED(hr))
{
_pstg->AddRef();
}
else
{
_pstg = NULL;
}
return hr;
}
STDMETHODIMP CTestAct::Save(
LPSTORAGE pStgSave,
BOOL fSameAsLoad)
{
HRESULT hr;
if (!fSameAsLoad)
{
if (_pstg)
_pstg->Release();
_pstg = pStgSave;
pStgSave->AddRef();
}
else
{
pStgSave = _pstg;
}
WriteClassStg(pStgSave, _rclsid);
hr = SaveData(pStgSave);
_fSaveInprogress = TRUE;
return hr;
}
STDMETHODIMP CTestAct::SaveCompleted(LPSTORAGE pStgSaved)
{
_fSaveInprogress = FALSE;
return ResultFromScode(S_OK);
}
STDMETHODIMP CTestAct::HandsOffStorage(void)
{
// Figure out what to do here!
return ResultFromScode(E_UNEXPECTED);
}
HRESULT CTestAct::GetData(void)
{
HRESULT hr;
BEGIN_BLOCK
// Get the saved xposition
hr = ReadPos(_pstg, XPOS, &_xPos);
if (FAILED(hr))
{
EXIT_BLOCK;
}
// Get the saved yposition
hr = ReadPos(_pstg, YPOS, &_yPos);
END_BLOCK
return hr;
}
HRESULT CTestAct::SaveData(IStorage *pstg)
{
HRESULT hr;
BEGIN_BLOCK
// Get the saved xposition
hr = WritePos(pstg, XPOS, _xPos);
if (FAILED(hr))
{
EXIT_BLOCK;
}
// Get the saved yposition
hr = WritePos(pstg, YPOS, _yPos);
END_BLOCK
return hr;
}
|